-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrgpio_monitor
executable file
·147 lines (130 loc) · 6.76 KB
/
rgpio_monitor
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh
### BEGIN INIT INFO
# Short-Description: Monitors RemoteGPIO dbus Service
# Description: Allow stoping and starting RemoteGPIO service from victron GUI
# Description: It also manage the reboot of the Dingtian devices
# Description: Also keep units.conf up to date so it creates the gpio at boot time
## END INIT INFO
exec 2>&1
get_setting()
{
dbus-send --print-reply=literal --system --type=method_call --dest=com.victronenergy.settings $1 com.victronenergy.BusItem.GetValue | awk '/int32/ { print $3 }'
}
set_setting()
{
dbus-send --print-reply=literal --system --type=method_call --dest=com.victronenergy.settings $1 com.victronenergy.BusItem.SetValue $2
}
get_IP()
{
dbus-send --print-reply=literal --system --type=method_call --dest=com.victronenergy.settings $1 com.victronenergy.BusItem.GetValue | awk '/variant/ { print $2 }'
}
latch=0
timer=$(date +%s)
while true
do
service=$(get_setting /Settings/Services/RemoteGPIO)
case $service in
0)
##
## Stop Service
###################
if [ $service != $latch ]
then
svc -d /service/rgpio_driver
echo "rgpio_driver: Stopping RemoteGPIO driver" >> /var/log/RemoteGPIO/current
#set_setting /Settings/RemoteGPIO/NumberUnits variant:int32:0
/etc/rcS.d/S90rgpio_pins.sh
#svc -t /service/dbus-systemcalc-py
#svc -t /service/dbus-digitalinputs/
fi
latch=$((service))
;;
1)
##
## Start Service
###########################
if [ $service != $latch ]
then
svc -u /service/rgpio_driver
echo "rgpio_driver: Starting RemoteGPIO driver" >> /var/log/RemoteGPIO/current
/etc/rcS.d/S90rgpio_pins.sh
fi
latch=$((service))
;;
2)
##
## Reboot device
###########################
if [ $service != $latch ]
then
Reboot_Unit1=$(get_setting /Settings/RemoteGPIO/Unit1/Reboot)
Reboot_Unit2=$(get_setting /Settings/RemoteGPIO/Unit2/Reboot)
Reboot_Unit3=$(get_setting /Settings/RemoteGPIO/Unit3/Reboot)
if [ $Reboot_Unit1 = 1 ]
##
## Reboot Unit1
#####################
then
IP_Unit1=$(get_IP /Settings/RemoteGPIO/Unit1/IP)
wget http://$IP_Unit1/reboot.cgi
# Clear reboot flag and put RemoteGPIO service to 1
set_setting /Settings/RemoteGPIO/Unit1/Reboot variant:int32:0
set_setting /Settings/Services/RemoteGPIO variant:int32:1
fi
if [ $Reboot_Unit2 = 1 ]
##
## Reboot Unit 2
####################
then
IP_Unit2=$(get_IP /Settings/RemoteGPIO/Unit2/IP)
wget http://$IP_Unit2/reboot.cgi
# Clear reboot flag and put RemoteGPIO service to 1
set_setting /Settings/RemoteGPIO/Unit2/Reboot variant:int32:0
set_setting /Settings/Services/RemoteGPIO variant:int32:1
fi
if [ $Reboot_Unit3 = 1 ]
##
## Reboot Unit 3
####################
then
IP_Unit3=$(get_IP /Settings/RemoteGPIO/Unit3/IP)
wget http://$IP_Unit3/reboot.cgi
# Clear reboot flag and put RemoteGPIO service to 1
set_setting /Settings/RemoteGPIO/Unit3/Reboot variant:int32:0
set_setting /Settings/Services/RemoteGPIO variant:int32:1
fi
fi
#latch=$((service))
;;
3)
##
## Restart Service
###########################
if [ $service != $latch ]
then
svc -t /service/rgpio_driver
echo "rgpio_driver: Restarting RemoteGPIO driver" >> /var/log/RemoteGPIO/current
/etc/rcS.d/S90rgpio_pins.sh
set_setting /Settings/Services/RemoteGPIO variant:int32:1
set_setting /Settings/RemoteGPIO/Restart variant:int32:0
fi
#latch=$((service))
;;
esac
##
## Watchdog
################################
if (( (timer + 10) < $(date +%s) ))
then
watchdog=$(get_setting /Settings/Watchdog/RemoteGPIO)
timer=$(date +%s)
if (( (watchdog + 10) < timer && service == 1 ))
then
echo "rgpio_driver: RemoteGPIO driver restart triggered by watchdog" >> /var/log/RemoteGPIO/current
svc -d /service/rgpio_driver ; svc -u /service/rgpio_driver
fi
echo "Heartbeat = "$(date -d@$timer)
fi
#Reduce CPU load
sleep 2
done