forked from dncodes/mpower-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqsub.sh
66 lines (59 loc) · 1.77 KB
/
mqsub.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh
log() {
logger -s -t "mqtt" "$*"
}
source $BIN_PATH/client/led.cfg
# initially set the LED. If configured it can be switched along with the relay later
if [ -n "$afterboot" ];
then
echo $afterboot > /proc/led/status
fi
# it should not blink
echo 0 > /proc/led/freq
log "MQTT listening..."
$BIN_PATH/mosquitto_sub -I $clientID -h $mqtthost $auth -v -t $topic/+/+/set \
--will-topic $topic/online --will-retain --will-qos 1 --will-payload 'false' \
| while read line; do
rxtopic=`echo $line| cut -d" " -f1`
inputVal=`echo $line| cut -d" " -f2`
port=`echo $rxtopic | sed 's|.*/port\([1-8]\)/[a-z]*/set$|\1|'`
property=`echo $rxtopic | sed 's|.*/port[1-8]/\([a-z]*\)/set$|\1|'`
if [ "$property" == "lock" ] || [ "$property" == "relay" ]
then
if [ "$inputVal" == "1" ]
then
val=1
# led handling
if [ -n "$relay_on" ]
then
echo $relay_on > /proc/led/status
fi
elif [ "$inputVal" == "0" ]
then
val=0
else
continue
fi
log "MQTT request received. $property control for port" $port "with value" $inputVal
`echo $val > /proc/power/$property$port`
echo 5 > $tmpfile
# led handling for relay_off
if [ -n "$relay_off" ]
then
all_relay_val=0
for i in $(seq $PORTS)
do
relay_val=`cat /proc/power/relay$((i))`
if [ $relay_val -eq 1 ]
then
all_relay_val=1
fi
done
# only set LED if all ports are OFF
if [ $all_relay_val -eq 0 ]
then
echo $relay_off > /proc/led/status
fi
fi
fi
done