-
Notifications
You must be signed in to change notification settings - Fork 16
/
weatherscript.sh
354 lines (306 loc) · 14.1 KB
/
weatherscript.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/bin/sh
#######################################################
### Autor: Nico Hartung <nicohartung1@googlemail.com> #
#######################################################
###########
### Install
## mkdir /mnt/us/scripts
## chmod 700 /mnt/us/scripts/weatherscript.sh
## mntroot rw
## cp /mnt/us/scripts/weather.conf /etc/upstart/
###########
# Variables
NAME=weatherscript
SCRIPTDIR="/mnt/us/scripts"
LOG="${SCRIPTDIR}/${NAME}.log"
SUSPENDFOR=900 # Default, flexibel by F5INTWORKDAY and F5INTWEEKEND
NET="wlan0"
LIMG="${SCRIPTDIR}/weatherdata.png"
LIMGBATT="${SCRIPTDIR}/weatherbattery.png"
LIMGERR="${SCRIPTDIR}/weathererror_image.png"
LIMGERRWLAN="${SCRIPTDIR}/weathererror_wlan.png"
LIMGERRNET="${SCRIPTDIR}/weathererror_network.png"
LIMGERRHOST="${SCRIPTDIR}/weathererror_hostname.png"
RSRV="192.168.1.10"
RIMG="${RSRV}/kindle-weather/weatherdata.png"
RSH="${RSRV}/kindle-weather/${NAME}.sh"
RPATH="/var/www/html/kindle-weather"
ROUTERIP="192.168.1.1" # Workaround, forget default gateway after STR
#F5INTWORKDAY="\
#06,07,08,15,16,17,18,19|900
#05,09,10,11,12,13,14,20,21|1800
#22,23,00,01,02,03,04|3600" # Refreshintervall for workdays = 57 Refreshes per workday
#F5INTWEEKEND="\
#06,07,08,15,16,17,18,19|900
#05,09,10,11,12,13,14,20,21|1800
#22,23,00,01,02,03,04|3600" # Refreshintervall for weekends = 57 Refreshes per weekend day
F5INTWORKDAY="\
06,07,08,14,15,16,17,18,19,20,21|900
05,09,10,11,12,13,22|1800
00,01,02,03,04,23|3600" # Refreshintervall for workdays = 44+14+6 = 64 Refreshes per workday
F5INTWEEKEND="\
06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21|900
05,22|1800
00,01,02,03,04,23|3600" # Refreshintervall for weekends = 64+4+6 = 74 Refreshes per weekend day
SMSACTIV=1
PLAYSMSUSER="admin"
PLAYSMSPW="00998877665544332211ffeeddccbbaa"
PLAYSMSURL="http://192.168.1.10/playsms/index.php"
CONTACTPAGERS="\
0049123456789|Nico
0049987654321|Michele"
##############
### Funktionen
kill_kindle() {
initctl stop framework > /dev/null 2>&1 # "powerd_test -p" doesnt work, other command found
initctl stop cmd > /dev/null 2>&1
initctl stop phd > /dev/null 2>&1
initctl stop volumd > /dev/null 2>&1
initctl stop tmd > /dev/null 2>&1
initctl stop webreader > /dev/null 2>&1
killall lipc-wait-event > /dev/null 2>&1
#initctl stop powerd > /dev/null 2>&1 # battery state doesnt work
#initctl stop lab126 > /dev/null 2>&1 # wlan interface doesnt work
#initctl stop browserd > /dev/null 2>&1 # not exist 5.9.4
#initctl stop pmond > /dev/null 2>&1 # not exist 5.9.4
}
customize_kindle() {
#mkdir /mnt/us/update.bin.tmp.partial # no auto update from kindle firmware, new way at 5.12 and above - https://www.mobileread.com/forums/showthread.php?t=327879 & https://www.mobileread.com/forums/showpost.php?p=4037105&postcount=19
touch /mnt/us/WIFI_NO_NET_PROBE # no wlan test for internet
}
debug_network() {
echo "" >> ${LOG} 2>&1
echo "" >> ${LOG} 2>&1
echo "## DEBUG BEGIN" >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | DEBUG ifconfig > `ifconfig ${NET}`" >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | DEBUG wifid cmState > `lipc-get-prop com.lab126.wifid cmState`" >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | DEBUG wifid signalStrength > `lipc-get-prop com.lab126.wifid signalStrength`" >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | DEBUG wpa_cli status > `wpa_cli status verbose`" >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | DEBUG ping ${ROUTERIP} > `ping ${ROUTERIP} -c4`" >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | DEBUG ping ${RSRV} > `ping ${RSRV} -c4`" >> ${LOG} 2>&1
echo "## DEBUG END" >> ${LOG} 2>&1
echo "" >> ${LOG} 2>&1
echo "" >> ${LOG} 2>&1
}
wait_wlan() {
return `lipc-get-prop com.lab126.wifid cmState | grep CONNECTED | wc -l`
}
send_sms () {
for LINE in ${CONTACTPAGERS}; do
CONTACTPAGER=`echo ${LINE} | awk -F\| '{print $1}'`
CONTACTPAGERNAME=`echo ${LINE} | awk -F\| '{print $2}'`
SMSTEST=`echo ${MESSAGE} | sed 's/ /%20/g'`
#curl --silent --insecure "${PLAYSMSURL}?app=ws&u=${PLAYSMSUSER}&h=${PLAYSMSPW}&op=pv&to=${CONTACTPAGER}&msg=${SMSTEST}" > /dev/null
curl --silent "${PLAYSMSURL}?app=ws&u=${PLAYSMSUSER}&h=${PLAYSMSPW}&op=pv&to=${CONTACTPAGER}&msg=${SMSTEST}" > /dev/null
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | SMS an ${CONTACTPAGERNAME} versendet!" >> ${LOG} 2>&1
done
}
map_ip_hostname () {
IP=`ifconfig ${NET} | grep "inet addr" | cut -d':' -f2 | awk '{print $1}'`
#HOSTNAME=`nslookup ${IP} | grep Address | grep ${IP} | awk '{print $4}' | awk -F. '{print $1}'`
if [ ${IP} == "192.168.1.70" ]; then
HOSTNAME="kindle-kt3-schwarz"
RIMG="${RSRV}/kindle-weather/weatherdata-bad.png"
IO_DP_BATTERY="0_userdata.0.kindle.varBatteryBad"
elif [ ${IP} == "192.168.1.71" ]; then
HOSTNAME="kindle-kt3-weiss"
RIMG="${RSRV}/kindle-weather/weatherdata-wohnzimmer.png"
IO_DP_BATTERY="0_userdata.0.kindle.varBatteryWohnzimmer"
else
HOSTNAME="failed_map_ip_hostname"
eips -f -g "${LIMGERRHOST}"
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Mappging der IP zum HOSTNAME fehlgeschlagen." >> ${LOG} 2>&1
debug_network
fi
}
##########
### Skript
### Variables for IFs
NOTIFYBATTERY=0
REFRESHCOUNTER=0
### IP > HOSTNAME
map_ip_hostname
### Kill Kindle processes
kill_kindle
### Customize Kindle
customize_kindle
### Loop
while true; do
### Start
echo "================================================" >> ${LOG} 2>&1
### Enable CPU Powersave
CHECKCPUMODE=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor | grep -i "powersave"`
if [ ${CHECKCPUMODE} -eq 0 ]; then
echo powersave > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | CPU runtergetaktet." >> ${LOG} 2>&1
fi
### Disable Screensaver, no energysaving by powerd
# powerd buggy since 5.4.5 - https://www.mobileread.com/forums/showthread.php?t=235821
CHECKSAVER=`lipc-get-prop com.lab126.powerd status | grep -i "prevent_screen_saver:0"`
if [ ${CHECKSAVER} -eq 0 ]; then
lipc-set-prop com.lab126.powerd preventScreenSaver 1 >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Standard Energiesparmodus deaktiviert." >> ${LOG} 2>&1
fi
### Check Batterystate
CHECKBATTERY=`gasgauge-info -s`
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Batteriezustand: ${CHECKBATTERY}%" >> ${LOG} 2>&1
if [ ${CHECKBATTERY} -gt 80 ]; then
NOTIFYBATTERY=0
fi
if [ ${CHECKBATTERY} -le 1 ]; then
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Batteriezustand 1%, statisches Batteriezustandsbild gesetzt, WLAN deaktivert, Ruhezustand!" >> ${LOG} 2>&1
eips -f -g "${LIMGBATT}"
lipc-set-prop com.lab126.wifid enable 0
echo 0 > /sys/class/rtc/rtc0/wakealarm
echo "mem" > /sys/power/state
fi
### Set SUSPENDFOR
# no regex in if with /bin/sh
DAYOFWEEK=`date +%u` # 1=Monday
HOURNOW=`date +%H` # Hour
# Workdays
if [ ${DAYOFWEEK} -ge 1 ] && [ ${DAYOFWEEK} -le 5 ]; then
for LINE in ${F5INTWORKDAY}; do
HOURS=`echo ${LINE} | awk -F\| '{print $1}'`
echo "${HOURS}" | grep ${HOURNOW} > /dev/null 2>&1
if [ $? -eq 0 ]; then
SUSPENDFOR=`echo ${LINE} | awk -F\| '{print $2}'`
echo "${SUSPENDFOR}"
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Aufwachintervall für den nächsten Ruhezustand auf ${SUSPENDFOR} gesetzt." >> ${LOG} 2>&1
fi
done
fi
# Weekend
if [ ${DAYOFWEEK} -ge 6 ] && [ ${DAYOFWEEK} -le 7 ]; then
for LINE in ${F5INTWEEKEND}; do
HOURS=`echo ${LINE} | awk -F\| '{print $1}'`
echo "${HOURS}" | grep ${HOURNOW} > /dev/null 2>&1
if [ $? -eq 0 ]; then
SUSPENDFOR=`echo ${LINE} | awk -F\| '{print $2}'`
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Aufwachintervall für den nächsten Ruhezustand auf ${SUSPENDFOR} gesetzt." >> ${LOG} 2>&1
fi
done
fi
### Calculation WAKEUPTIMER
WAKEUPTIMER=$(( `date +%s` + ${SUSPENDFOR} ))
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Aufwachzeitpunkt für den nächsten Ruhezustand `date -d @${WAKEUPTIMER} '+%Y-%m-%d_%H:%M:%S'`." >> ${LOG} 2>&1
### Reassociate WLAN - 2020-11-09
# https://www.mobileread.com/forums/showthread.php?t=312150
wpa_cli -i wlan0 reassociate >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | WLAN reassociate." >> ${LOG} 2>&1
### Wait on WLAN
WLANNOTCONNECTED=0
WLANCOUNTER=0
while wait_wlan; do
if [ ${WLANCOUNTER} -eq 10 ] || [ ${WLANCOUNTER} -eq 30 ] || [ ${WLANCOUNTER} -eq 50 ] || [ ${WLANCOUNTER} -eq 60 ]; then
debug_network
fi
if [ ${WLANCOUNTER} -eq 10 ]; then
wpa_cli -i wlan0 disconnect >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | WLAN disconnect." >> ${LOG} 2>&1
wpa_cli -i wlan0 reconnect >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | WLAN reconnect." >> ${LOG} 2>&1
fi
if [ ${WLANCOUNTER} -eq 30 ]; then
lipc-set-prop com.lab126.wifid enable 0 >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | WLAN deaktivieren." >> ${LOG} 2>&1
lipc-set-prop com.lab126.wifid enable 1 >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | WLAN aktivieren." >> ${LOG} 2>&1
fi
if [ ${WLANCOUNTER} -eq 50 ]; then
wpa_cli -i wlan0 reassociate >> ${LOG} 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | WLAN reassociate." >> ${LOG} 2>&1
fi
if [ ${WLANCOUNTER} -eq 60 ]; then
eips -f -g "${LIMGERRWLAN}"
WLANNOTCONNECTED=1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Leider keine erfolgreiche Verbindung mit einem WLAN hergestellt." >> ${LOG} 2>&1
break
fi
let WLANCOUNTER=WLANCOUNTER+1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Warte auf WLAN (Versuch ${WLANCOUNTER})." >> ${LOG} 2>&1
sleep 1
done
### Connected with WLAN?
if [ ${WLANNOTCONNECTED} -eq 0 ]; then
### IP > HOSTNAME
map_ip_hostname
### Workaround Default Gateway after STR
GATEWAY=`ip route | grep default | grep ${NET} | awk '{print $3}'`
if [ -z "${GATEWAY}" ]; then
route add default gw ${ROUTERIP} >> ${LOG} 2>&1
fi
### Battery @ ioBroker
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Batteriezustand an ioBroker soll übergeben werden!" >> ${LOG} 2>&1
curl --silent "http://${RSRV}:8087/set/${IO_DP_BATTERY}?value=${CHECKBATTERY}" --connect-timeout 2 > /dev/null 2>&1
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Batteriezustand an ioBroker übergeben!" >> ${LOG} 2>&1
### Batterystate critical? SMS!
if [ ${CHECKBATTERY} -le 5 ] && [ ${NOTIFYBATTERY} -eq 0 ]; then
NOTIFYBATTERY=1
if [ ${SMSACTIV} -eq 1 ]; then
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Batteriezustand kritisch, SMS werden verschickt!" >> ${LOG} 2>&1
MESSAGE="Der Batteriezustand von ${HOSTNAME} ist kritisch (${CHECKBATTERY}%) - bitte laden!"
send_sms
else
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Batteriezustand kritisch." >> ${LOG} 2>&1
fi
fi
### Check new Script
# wget (-N) can't https
RSTATUSSH=`curl --silent --head "http://${RSH}" --connect-timeout 2 | head -n 1 | cut -d$' ' -f2`
if [ ${RSTATUSSH} -eq 200 ]; then
LMTIMESH=`stat -c %Y "${SCRIPTDIR}/${NAME}.sh"`
curl --silent --time-cond "${SCRIPTDIR}/${NAME}.sh" --output "${SCRIPTDIR}/${NAME}.sh" "http://${RSH}"
RMTIMESH=`stat -c %Y "${SCRIPTDIR}/${NAME}.sh"`
if [ ${RMTIMESH} -gt ${LMTIMESH} ]; then
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Skript aktualisiert, Neustart durchführen." >> ${LOG} 2>&1
chmod 777 "${SCRIPTDIR}/${NAME}.sh"
reboot
exit
fi
else
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Skript nicht gefunden (HTTP-Status ${RSTATUSSH})." >> ${LOG} 2>&1
fi
### Get new Weatherdata
if [ ${HOSTNAME} != "failed_map_ip_hostname" ]; then
let REFRESHCOUNTER=REFRESHCOUNTER+1
RSTATUSIMG=`curl --silent --head "http://${RIMG}" --connect-timeout 2 | head -n 1 | cut -d$' ' -f2`
if [ ${RSTATUSIMG} -eq 200 ]; then
curl --silent --output "$LIMG" "http://${RIMG}" --connect-timeout 2
if [ ${REFRESHCOUNTER} -le 5 ]; then
eips -g "$LIMG"
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Wetterbild aktualisiert." >> ${LOG} 2>&1
else
eips -f -g "$LIMG"
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Wetterbild und E-Ink aktualisiert." >> ${LOG} 2>&1
REFRESHCOUNTER=0
fi
elif [ -z "${RSTATUSIMG}" ]; then
eips -f -g "$LIMGERRNET"
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Webserver reagiert nicht. Webserver läuft? Server erreichbar? Kindle mit dem WLAN verbunden?" >> ${LOG} 2>&1
debug_network
else
eips -f -g "$LIMGERR"
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Wetterbild auf Webserver nicht gefunden (HTTP-Status ${RSTATUSSH})." >> ${LOG} 2>&1
debug_network
fi
else
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Hostname nicht bekannt, Wetterbild konnte nicht aktualisiert werden." >> ${LOG} 2>&1
debug_network
fi
fi
### Copy log by ssh
cat ${LOG} | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=1 -o ConnectionAttempts=1 -i /mnt/us/scripts/id_rsa_kindle -l kindle ${RSRV} "cat >> ${RPATH}/${NAME}_${HOSTNAME}.log" > /dev/null 2>&1
if [ $? -eq 0 ]; then
rm ${LOG}
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Log per SSH an Remote-Server übergeben und lokal gelöscht." >> ${LOG} 2>&1
else
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Log konnte nicht an den Remote-Server übergeben werden." >> ${LOG} 2>&1
fi
### Set wakealarm
echo 0 > /sys/class/rtc/rtc0/wakealarm
echo ${WAKEUPTIMER} > /sys/class/rtc/rtc0/wakealarm
### Go into Suspend to Memory (STR)
echo "`date '+%Y-%m-%d_%H:%M:%S'` | ${HOSTNAME} | Ruhezustand starten." >> ${LOG} 2>&1
echo "mem" > /sys/power/state
done