first commit
This commit is contained in:
79
support/mqtt_ad/README.md
Normal file
79
support/mqtt_ad/README.md
Normal file
@@ -0,0 +1,79 @@
|
||||
|
||||
# MQTT AutoDiscovery tools
|
||||
|
||||
## How to supply an MQTT AD messages dump to the developers
|
||||
|
||||
When you are asked to supply a copy of all MQTT messages for a device used by MQTT Autodiscovery, you can follow the following steps:
|
||||
|
||||
1. go to subdirecory domoticz/scripts/support/mqtt_ad
|
||||
2. Run the script as shown in the examples below with the appropriate parameters. This will capture all MQTT packages in scope of the defined parametres.
|
||||
3. Provide us with the **mqtt_ad_record_*XXX*.log** file containing all records.
|
||||
4. Press Ctrl+c when you want to stop the capture process before the timer ends.
|
||||
|
||||
## mqtt_ad_record.sh
|
||||
|
||||
Script that helps capturing MQTT messages for Homeassistant AD into a file, so they can be shared easily with others for debugging.
|
||||
|
||||
### usage mqtt_ad_record.sh
|
||||
|
||||
```text
|
||||
bash mqtt_ad_record.sh [-h hostname/ip] [-p port] [-s searchstring] [-t capturetime]
|
||||
-h Hostname or Ipaddres of MQTT deamon. default is 127.0.0.1
|
||||
-p port for the MQTT deamon. default is 1883
|
||||
-u Userid for MQTT deamon
|
||||
-P Password for MQTT deamon
|
||||
-s only records MQTT messages that contain this string in the TOPIC or PAYLOAD. default is all messages
|
||||
-t caputure time in seconds. default is 600
|
||||
```
|
||||
|
||||
### examples mqtt_ad_record.sh
|
||||
|
||||
```bash
|
||||
# Records all MQTT Messages containing "/config", "_state" or "/state" for 10 minutes
|
||||
bash mqtt_ad_record.sh
|
||||
|
||||
# specify userid and password to access the MQTT deamon.
|
||||
bash mqtt_ad_record.sh -u UserId -P Password
|
||||
|
||||
#Records all MQTT Messages containing "/config", "_state" or "/state" for 30 Seconds
|
||||
bash mqtt_ad_record.sh -t 30
|
||||
|
||||
#Records all MQTT Messages containing "TASMOTA_A1" for 10 minutes
|
||||
bash mqtt_ad_record.sh -s TASMOTA_A1
|
||||
|
||||
#Records all MQTT Messages containing "TASMOTA_A1" for 30 seconds
|
||||
bash mqtt_ad_record.sh -s TASMOTA_A1 -t 30
|
||||
```
|
||||
|
||||
The above command will create an **mqtt_ad_record_*XXX*.log** file in the current directory.
|
||||
|
||||
- where ***XXX*** will be ***all*** for the first 2 examples and will be ***TASMOTA_A1*** for the last 2 examples.
|
||||
|
||||
## mqtt_ad_send.sh
|
||||
|
||||
Script that sends the captured mqtt records again to a server recorded in HA_Discovery_mqtt.log in dezelfde directory.
|
||||
|
||||
### usage mqtt_ad_send.sh
|
||||
|
||||
```text
|
||||
bash mqtt_ad_send.sh [-h hostname/ip] [-p port] [-s searchstring] [-i inputfile]
|
||||
-h Hostname or Ipaddres of MQTT deamon. default is 127.0.0.1
|
||||
-p port for the MQTT deamon. default is 1883
|
||||
-s only send MQTT messages that contain this string in the TOPIC or PAYLOAD. default is all messages
|
||||
-i inputfile. default is mqtt_ad_record_all.log
|
||||
-r override retain (y/n). defaults to the retain flag in the input records
|
||||
-d When Dry-run when switch is provided, no MQTT messages will be send, just the logging.
|
||||
```
|
||||
|
||||
### examples mqtt_ad_send.sh
|
||||
|
||||
```bash
|
||||
#ALL records
|
||||
bash mqtt_ad_send.sh
|
||||
|
||||
#ALL records from a specific file
|
||||
bash mqtt_ad_send.sh -i mqtt_ad_record_testDevice.log
|
||||
|
||||
#Send selected records from log file:
|
||||
bash mqtt_ad_send.sh PARTIAL_DEVICE_NAME
|
||||
```
|
||||
102
support/mqtt_ad/mqtt_ad_record.sh
Normal file
102
support/mqtt_ad/mqtt_ad_record.sh
Normal file
@@ -0,0 +1,102 @@
|
||||
#!/bin/bash
|
||||
sver="20220124-01"
|
||||
: '
|
||||
# Scriptname mqtt_ad_record.sh
|
||||
# Created by JvdZ
|
||||
|
||||
Script that helps capturing MQTT messages for Homeassistant AD into a file so they can be shared easily with others for debugging.
|
||||
|
||||
usage:
|
||||
bash mqtt_ad_record.sh [-h hostname/ip] [-p port] [-s searchstring] [-t*max capture time seconds*]
|
||||
-h Hostname or Ipaddres of MQTT deamon. default is 127.0.0.1
|
||||
-p port for the MQTT deamon. default is 1883
|
||||
-u Userid for MQTT deamon
|
||||
-P Password for MQTT deamon
|
||||
-s only records MQTT messages that contain this string in the TOPIC or PAYLOAD. default is all messages
|
||||
-t caputure time in seconds. default is 600
|
||||
|
||||
examples:
|
||||
# Records all MQTT Messages containing "/config", "_state" or "/state" for 10 minutes.
|
||||
bash mqtt_ad_record.sh
|
||||
|
||||
# specify userid and password to access the MQTT deamon.
|
||||
bash mqtt_ad_record.sh -u UserId -P Password
|
||||
|
||||
#Records all MQTT Messages containing "/config", "_state" or "/state" for 30 Seconds.
|
||||
bash mqtt_ad_record.sh -t 30
|
||||
|
||||
#Records all MQTT Messages containing "TASMOTA_A1" for 10 minutes.
|
||||
bash mqtt_ad_record.sh -s TASMOTA_A1
|
||||
|
||||
#Records all MQTT Messages containing "TASMOTA_A1" for 30 seconds.
|
||||
bash mqtt_ad_record.sh -s TASMOTA_A1 -t 30
|
||||
|
||||
Output file name:
|
||||
The above commands will create file mqtt_ad_record_XXX.log in the current directory,
|
||||
where XXX will be all for the first 2 examples and will be TASMOTA_A1 for the last 2 examples.
|
||||
'
|
||||
|
||||
MQTT_IP="127.0.0.1" # Define MQTT Server
|
||||
MQTT_PORT="1883" # Define port
|
||||
rtime=600 # Define default Capture time for MQTT messages in seconds.
|
||||
# You can always interrupt the capture process at anytime withCtrl+Break pr Ctrl+C
|
||||
MQTT_Param="" # used for extram mosquitto_sub parameters
|
||||
|
||||
# Check if mosquitto_sub is installed
|
||||
if ! command -v mosquitto_sub &> /dev/null; then
|
||||
echo "================================================================================================================="
|
||||
echo "This script can be used to capture MQTT messages for a particular device."
|
||||
echo "Current MQT Server $MQTT_IP port $MQTT_PORT, edit script to change these."
|
||||
echo "!!!! program mosquitto_sub not found, please install that first."
|
||||
echo "RPI install:sudo apt-get install mosquitto-clients"
|
||||
echo "================================================================================================================="
|
||||
exit
|
||||
fi
|
||||
# process parameters
|
||||
while getopts h:p:u:P:s:t: flag
|
||||
do
|
||||
case "${flag}" in
|
||||
h) MQTT_IP=${OPTARG};;
|
||||
p) MQTT_PORT=${OPTARG};;
|
||||
u) MQTT_Param=${MQTT_Param}' -u '${OPTARG}'';;
|
||||
P) MQTT_Param=${MQTT_Param}' -P '${OPTARG}'';;
|
||||
s) sdev=${OPTARG};;
|
||||
t) rtime=${OPTARG};;
|
||||
esac
|
||||
done
|
||||
|
||||
# Set case insensitive flag for the PARTIAL_DEVICE_NAME tests
|
||||
shopt -s nocasematch
|
||||
|
||||
# trap ctrl-c and call ctrl_c()
|
||||
trap message INT
|
||||
|
||||
function message() {
|
||||
echo "** CTRL-C pressed."
|
||||
# command for clean up e.g. rm and so on goes below
|
||||
}
|
||||
|
||||
echo "== $sver =============================================================================================================="
|
||||
echo "MQTT_IP : '$MQTT_IP'";
|
||||
echo "MQTT_PORT : '$MQTT_PORT'";
|
||||
echo "MQTT_Param: '$MQTT_Param'";
|
||||
echo "Recordtime: '$rtime'";
|
||||
echo "Search For: '$sdev'";
|
||||
|
||||
logfile="mqtt_ad_record_all.log"
|
||||
# Start Capture
|
||||
if [[ -z $sdev ]]; then
|
||||
echo "Start Capture for $rtime seconds of all MQTT messages to Console and file: $logfile"
|
||||
mosquitto_sub $MQTT_Param -h $MQTT_IP -p $MQTT_PORT -t "#" -v -W $rtime -F "%I\t%r\t%t\t%p"| stdbuf -i0 -o0 grep -i -e "\/config\|[_\/]state" | stdbuf -i0 -o0 tee "$logfile"
|
||||
else
|
||||
# remove characters in filename that aren't allowed
|
||||
logfile="mqtt_ad_record_"${sdev//[^A-Za-z0-9-_]/}".log"
|
||||
echo "Start Capture for $rtime seconds of MQTT messages containing $sdev to Console and file: $logfile"
|
||||
mosquitto_sub $MQTT_Param -h $MQTT_IP -p $MQTT_PORT -t "#" -v -W $rtime -F "%I\t%r\t%t\t%p"| stdbuf -i0 -o0 grep -i "$sdev" | stdbuf -i0 -o0 tee "$logfile"
|
||||
fi
|
||||
# Capture Ended
|
||||
if [ "$?" -eq "0" ] ; then
|
||||
echo "Capture ended, check file: $logfile"
|
||||
else
|
||||
echo "Capture interrupted, check file: $logfile"
|
||||
fi
|
||||
102
support/mqtt_ad/mqtt_ad_send.sh
Normal file
102
support/mqtt_ad/mqtt_ad_send.sh
Normal file
@@ -0,0 +1,102 @@
|
||||
#!/bin/bash
|
||||
sver="20220124-01"
|
||||
: '
|
||||
# Scriptname mqtt_ad_send.sh
|
||||
# Created by JvdZ
|
||||
|
||||
Script that sends the captured mqtt records again to a server recorded in HA_Discovery_mqtt.log in dezelfde directory.
|
||||
|
||||
usage:
|
||||
bash mqtt_ad_send.sh [-h hostname/ip] [-p port] [-s searchstring] [-i inputfile]
|
||||
-h Hostname or Ipaddres of MQTT deamon. default is 127.0.0.1
|
||||
-p port for the MQTT deamon. default is 1883
|
||||
-u Userid for MQTT deamon
|
||||
-P Password for MQTT deamon
|
||||
-s only send MQTT messages that contain this string in the TOPIC or PAYLOAD. default is all messages
|
||||
-i inputfile. default is mqtt_ad_record_all.log
|
||||
-r override retain (y/n). defaults to the retain flag in the record
|
||||
-d When Dry-run when switch is provided, no MQTT messages will be send, just the logging.
|
||||
|
||||
example:
|
||||
ALL records : bash mqtt_ad_send.sh
|
||||
Selected records: bash mqtt_ad_send.sh PARTIAL_DEVICE_NAME
|
||||
'
|
||||
|
||||
MQTT_IP="127.0.0.1"
|
||||
MQTT_PORT="1883"
|
||||
MQTT_Param=""
|
||||
input="mqtt_ad_record_all.log"
|
||||
retain=""
|
||||
dryrun=""
|
||||
|
||||
# process parameters
|
||||
while getopts h:p:u:P:s:i:r:d flag
|
||||
do
|
||||
case "${flag}" in
|
||||
h) MQTT_IP=${OPTARG};;
|
||||
p) MQTT_PORT=${OPTARG};;
|
||||
u) MQTT_Param=${MQTT_Param}' -u '${OPTARG};;
|
||||
P) MQTT_Param=${MQTT_Param}' -P '${OPTARG};;
|
||||
s) sdev=${OPTARG};;
|
||||
i) input=${OPTARG};;
|
||||
r) retain=${OPTARG};;
|
||||
d) dryrun='y';;
|
||||
esac
|
||||
done
|
||||
|
||||
# Set case insensitive flag for the PARTIAL_DEVICE_NAME tests
|
||||
shopt -s nocasematch
|
||||
|
||||
echo "== $sver =============================================================================================================="
|
||||
echo "MQTT_IP : '$MQTT_IP'";
|
||||
echo "MQTT_PORT : '$MQTT_PORT'";
|
||||
echo "MQTT_Param: '$MQTT_Param'";
|
||||
echo "inputfile : '$input'";
|
||||
if [[ ! -z "$retain" ]] ; then
|
||||
echo "Retain override with '$retain'";
|
||||
fi
|
||||
if [[ ! -z "$dryrun" ]] ; then
|
||||
echo "### Dry-Run so no MQTT messages are send, just showing the log of the selected messages.###";
|
||||
fi
|
||||
|
||||
srec=0;
|
||||
|
||||
#Get rid of CR and only leave LF and read all records
|
||||
# -r option read is to leave backslaches as in the file
|
||||
cat "$input" | tr -d "\r" | while IFS=$'\t' read -r itime iretain itopic ipayload;
|
||||
do
|
||||
#echo -e "###\nT=$itopic\nP=$ipayload"
|
||||
#skip Comment lines starting with #
|
||||
[[ itime =~ ^#.* ]] && { continue; }
|
||||
|
||||
#skip empty lines or only one param is provided
|
||||
[[ -z "$ipayload" ]] && { continue; }
|
||||
|
||||
#skip lines that do not contain the selected dev
|
||||
[[ ! $itopic =~ $sdev ]] && {
|
||||
[[ ! $ipayload =~ $sdev ]] && { continue; }
|
||||
}
|
||||
|
||||
## process the selected record
|
||||
srec=$((srec+1))
|
||||
# Set MQTT Options
|
||||
MQTT_Opts=""
|
||||
# Add Retain option when 1 is specified in input or in the override param -r
|
||||
if [[ -z "$retain" ]] ; then
|
||||
[[ "$iretain" == "1" ]] && { MQTT_Opts=" -r";}
|
||||
else
|
||||
[[ "$retain" == "y" ]] && { MQTT_Opts=" -r";}
|
||||
fi
|
||||
if [[ -z "$dryrun" ]] ; then
|
||||
echo -e "== $srec > $MQTT_Opts\nT=$itopic\nP=$ipayload"
|
||||
mosquitto_pub $MQTT_Param $MQTT_Opts -h $MQTT_IP -p $MQTT_PORT -t "$itopic" -m "$ipayload"
|
||||
else
|
||||
echo -e "-- $srec: $MQTT_Opts\nT=$itopic\nP=$ipayload"
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ ! -z "$dryrun" ]] ; then
|
||||
echo "###============================================###";
|
||||
echo "### Dry-Run ended,so no MQTT messages are send.###";
|
||||
echo "###============================================###";
|
||||
fi
|
||||
Reference in New Issue
Block a user