-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmqpub.sh
110 lines (97 loc) · 2.63 KB
/
mqpub.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/sh
log() {
logger -s -t "mqtt" "$*"
}
# read config file
source $BIN_PATH/client/mpower-pub.cfg
export PUBBIN=$BIN_PATH/mosquitto_pub
log "Found $((PORTS)) ports."
log "Publishing to $mqtthost with topic $topic"
REFRESHCOUNTER=$refresh
FASTUPDATE=0
export relay=$relay
export power=$power
export energy=$energy
export voltage=$voltage
export lock=$lock
$BIN_PATH/client/mqpub-static.sh
while sleep 1;
do
# refresh logic: either we need fast updates, or we count down until it's time
TMPFASTUPDATE=`cat $tmpfile`
#echo "TMPFILE = " $TMPFASTUPDATE
if [ -n "${TMPFASTUPDATE}" ]
then
FASTUPDATE=$TMPFASTUPDATE
: > $tmpfile
fi
if [ $FASTUPDATE -ne 0 ]
then
# fast update required, we do updates every second until the requested number of fast updates is done
FASTUPDATE=$((FASTUPDATE-1))
else
# normal updates, decrement refresh counter until it is time
if [ $REFRESHCOUNTER -ne 0 ]
then
# not yet, keep counting
REFRESHCOUNTER=$((REFRESHCOUNTER-1))
continue
else
# time to update
REFRESHCOUNTER=$refresh
fi
fi
if [ $relay -eq 1 ]
then
# relay state
for i in $(seq $PORTS)
do
relay_val=`cat /proc/power/relay$((i))`
if [ $relay_val -ne 1 ]
then
relay_val=0
fi
$PUBBIN -h $mqtthost $auth -t $topic/port$i/relay -m "$relay_val" -r
done
fi
if [ $power -eq 1 ]
then
# current power
for i in $(seq $PORTS)
do
power_val=`cat /proc/power/active_pwr$((i))`
power_val=`printf "%.1f" $power_val`
$PUBBIN -h $mqtthost $auth -t $topic/port$i/power -m "$power_val" -r
done
fi
if [ $energy -eq 1 ]
then
# energy consumption
for i in $(seq $PORTS)
do
energy_val=`cat /proc/power/cf_count$((i))`
energy_val=$(awk -vn1="$energy_val" -vn2="0.3125" 'BEGIN{print n1*n2}')
energy_val=`printf "%.0f" $energy_val`
$PUBBIN -h $mqtthost $auth -t $topic/port$i/energy -m "$energy_val" -r
done
fi
if [ $voltage -eq 1 ]
then
# energy consumption
for i in $(seq $PORTS)
do
voltage_val=`cat /proc/power/v_rms$((i))`
voltage_val=`printf "%.1f" $voltage_val`
$PUBBIN -h $mqtthost $auth -t $topic/port$i/voltage -m "$voltage_val" -r
done
fi
if [ $lock -eq 1 ]
then
# lock
for i in $(seq $PORTS)
do
port_val=`cat /proc/power/lock$((i))`
$PUBBIN -h $mqtthost $auth -t $topic/port$i/lock -m "$port_val" -r
done
fi
done