-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_service.sh
executable file
·204 lines (169 loc) · 5.53 KB
/
run_service.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash -
#===============================================================================
#
# FILE: run_service.sh
#
# USAGE: ./run_service.sh
#
# DESCRIPTION: Script called by systemd to run a sequence at boot time # #
# OPTIONS: ---
# AUTHOR: Alexandre CORIZZI, [email protected]
# ORGANIZATION: Laboratoire d'oceanographie de Villefranche-sur-mer (LOV)
# CREATED: 22/10/2020 18:19
# REVISION: ---
#===============================================================================
set -o nounset # Treat unset variables as an error
set -euo pipefail # Bash Strict Mode
if [[ ${PWD##*/} != "hypernets_tools" ]]; then
echo "This script must be run from hypernets_tools folder" 1>&2
echo "Use : ./utils/${0##*/} instead"
exit 1
fi
source utils/configparser.sh
# Hypstar Configuration:
baudrate=$(parse_config "baudrate" config_dynamic.ini)
hypstarPort=$(parse_config "'hypstar_port" config_dynamic.ini)
bypassYocto=$(parse_config "bypass_yocto" config_static.ini)
loglevel=$(parse_config "loglevel" config_dynamic.ini)
bootTimeout=$(parse_config "boot_timeout" config_dynamic.ini)
swirTec=$(parse_config "swir_tec" config_dynamic.ini)
verbosity=$(parse_config "verbosity" config_dynamic.ini)
# Starting Conditions:
sequence_file=$(parse_config "sequence_file" config_dynamic.ini)
checkWakeUpReason=$(parse_config "check_wakeup_reason" config_dynamic.ini)
startSequence=$(parse_config "start_sequence" config_dynamic.ini)
keepPc=$(parse_config "keep_pc" config_dynamic.ini)
shutdown_sequence() {
if [[ "$bypassYocto" == "no" ]] && [[ "$startSequence" == "yes" ]] ; then
echo "[INFO] Set relays #2 and #3 to OFF."
python -m hypernets.yocto.relay -soff -n2 -n3
fi
if [[ "$keepPc" == "off" ]]; then
echo "[INFO] Option : Keep PC OFF"
echo "[INFO] Send Yoctopuce To sleep (or not)"
set +e
python -m hypernets.yocto.sleep_monitor
yocto_sleep=$?
set -e
echo "[DEBUG] Yoctosleep status : $yocto_sleep"
if [[ ! $yocto_sleep -eq 0 ]]; then
echo "[CRITICAL] Yocto unreachable !!"
fi
exit 0
else
# Cause service exit 1 and doesnt execute SuccessAction=poweroff
echo "[INFO] Option : Keep PC ON"
exit 1
fi
}
if [[ ! "$bypassYocto" == "yes" ]] ; then
# Ensure Yocto is online
yoctopuceIP=$(parse_config "yoctopuce_ip" config_static.ini)
if [[ ! "$yoctopuceIP" == "usb" ]] ; then
# We ping it if there is an IP address
echo "[INFO] Waiting for yoctopuce..."
while ! timeout 2 ping -c 1 -n $yoctopuceIP &>/dev/null
do
echo -n '.'
done
echo "[INFO] Ok !"
else
# Else check if VirtualHub is running
set +e
systemctl is-active yvirtualhub.service > /dev/null
set -e
if [[ $? -eq 0 ]] ; then
echo "[INFO] VirtualHub is running."
else
echo "[INFO] Starting VirtualHub..."
/usr/bin/VirtualHub
sleep 2
echo "[INFO] ok"
fi
fi
if [[ "$checkWakeUpReason" == "yes" ]] ; then
echo "[INFO] Check Wake up reason..."
set +e
wakeupreason=$(python -m hypernets.yocto.wakeupreason)
set -e
echo "[DEBUG] Wake up reason is : $wakeupreason."
if [[ ! "$wakeupreason" == "schedule1" ]]; then
echo "[WARNING] $wakeupreason is not a reason to start the sequence."
startSequence="no"
if [[ ! "$keepPc" == "on" ]]; then
echo "[DEBUG] Security sleep 2 minutes..."
sleep 120
fi
shutdown_sequence;
fi
fi
fi
if [[ "$startSequence" == "no" ]] ; then
echo "[INFO] Start sequence = no"
if [[ ! "$keepPc" == "on" ]]; then
echo "[INFO] 5 minutes sleep..."
sleep 300
fi
shutdown_sequence;
fi
extra_args=""
if [[ -n $hypstarPort ]] ; then
extra_args="$extra_args -p $hypstarPort"
fi
if [[ -n $baudrate ]] ; then
extra_args="$extra_args -b $baudrate"
fi
if [[ -n $loglevel ]] ; then
extra_args="$extra_args -l $loglevel"
fi
if [[ -n $bootTimeout ]] ; then
extra_args="$extra_args -t $bootTimeout"
fi
if [[ -n $swirTec ]] ; then
extra_args="$extra_args -T $swirTec"
fi
if [[ -n $verbosity ]] ; then
extra_args="$extra_args -v $verbosity"
fi
if [[ ! "$bypassYocto" == "yes" ]] ; then
echo "[INFO] Set relays #2 and #3 to ON."
python -m hypernets.yocto.relay -son -n2 -n3
else
echo "[INFO] Bypassing Yocto"
extra_args="$extra_args --noyocto"
fi
exit_actions() {
return_value=$?
if [ $return_value -eq 0 ] ; then
python /home/hypernets/TEST/vrmapi/main.py -a local -p /home/hypernets/TEST/vrmapi/log -seq END -v
echo "[INFO] Success"
else
echo "[INFO] Hysptar scheduled job exited with code $return_value";
# FIXME : sudo issue
# if [ $return_value -eq 27 ]; then
# echo "[INFO] Trying to reload and trigger USB rules..."
# sudo udevadm control --reload
# echo $?
# sudo udevadm trigger
# echo $?
# fi
sleep 1
echo "[INFO] Second try : "
set +e
python3 -m hypernets.open_sequence -f $sequence_file $extra_args
set -e
fi
shutdown_sequence;
}
python3 main.py -a checkth -c config_th.ini
pv_status=$?
if [ $pv_status -gt 0 ] ; then
echo "[INFO] Cerbo GX readings show poor light conditions. Exiting without starting sequence"
python /home/hypernets/TEST/vrmapi/main.py -a local -p /home/hypernets/TEST/vrmapi/log -seq NOSEQ -v
#shutdown_sequence
else
echo "[INFO] Cerbo GX reading show good light conditions. Starting sequence..."
#trap "exit_actions" EXIT
#python3 /home/hypernets/TEST/vrmapi/main.py -a local -p /home/hypernets/TEST/vrmapi/log -seq START -v
#python3 -m hypernets.open_sequence -f $sequence_file $extra_args
fi