Today I used dzvents script to ask to run a script from "docker side" via mosquito
Code: Select all
function RunScript(DZ,s)
-- s : command to execute
-- add " on each side !
local CmdStr ='mosquitto_pub -h 192.168.1.4 -t xGate/cmd -m "'..s..'"'
DZ.executeShellCommand({
command = CmdStr,
callback = 'Retour_MqttScript',
timeout = 3 ,
})Code: Select all
#!/bin/sh
# https://gist.github.com/David-Lor/63fb0be80b67359c8de6230c6b1dafa2
while true # Keep an infinite loop to reconnect when connection lost/broker unavailable
do
mosquitto_sub -h localhost -t xGate/cmd/\# -F "%t %p" | while read -r payload
do
echo "$payload"
# Here is the callback to execute whenever you receive a message:
topic=$(echo "$payload" | cut -d ' ' -f 1)
msg=$(echo "$payload" | cut -d ' ' -f 2-)
# echo "Rx MQTT: $topic: ${payload}"
echo "${msg}"
# p=$(echo "$msg" | jq '.property')
# echo "Extracted property: $p for $topic"
eval "$msg" &
done
sleep 10 # Wait 10 seconds until reconnection
done # & # Discomment the & to run in background (but you should rather run THIS script in background)
Thanks in advance for answers ...