-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarntg.py
300 lines (225 loc) · 8.35 KB
/
warntg.py
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
#!/usr/bin/env python3
# copyright (c) 2019-2024 by Rainer Fiedler DO6UK
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import json, sys, requests, time, urllib.parse
## ** variables **
VERSION='20190610'
## file to save last state
STATEFILE='./warntg.state'
## name for provider of this service (used for telegram-message)
SERVICE='*your*name*'
## set user-agent for http-requests
USER_AGENT='curl/7.61.0'
## url and jsonp-prefix/suffix to fetch dwd-warnings
DWD_URL='https://www.dwd.de/DWD/warnungen/warnapp/json/warnings.json'
DWD_PREFIX='warnWetter.loadWarnings('
DWD_SUFFIX=');'
## url to access brandmeister.network-api
BM_GETURL='https://api.brandmeister.network/v1.0/repeater/?action=profile&q=%BM_DMRID%'
BM_SETURL='https://api.brandmeister.network/v1.0/repeater/talkgroup/?action=%ACTION%&id=%BM_DMRID%'
## ApiKey used brandmeister.network-api
bm_apikey='*your*bmapikey*'
## url to send message via WebALARM_BOT
TG_WA_URL='https://wachalarm.ralsu.de/webalarm_bot/webalarm_bot.php?json={"command":"api_message","apikey":"%TG_API%","message":"%TG_MSG%","title":"%TG_TITEL%","sender": "'+SERVICE+'","gruppe":"%TG_GRP%"}'
## list of WebALARM_BOT-Keys to send telegram-message
tg_wa_api=['*your*webalarmkey*']
## url to send DAPNET-message
DN_URL='http://www.hampager.de:8080/calls'
## auth to send DAPNET-message
dn_auth=('*your*call*','*your*dapnetkey*')
## replace chars in DAPNET-message
dn_replace={'Ä':'a', 'ä':'A', 'Ö':'o', 'ö':'O', 'Ü':'u', 'ü':'U', 'ß':'S'}
## list of DAPNET-callsigns to send messages
dn_callsigns=['*you*call*','*your*friendcall*']
## list of DAPNET-TX-groups
dn_txgrp = ['dl-ni']
## dict with DMRIds and list of tuples which TGs will be removed on warning and restored after
## usage: bm_warn_tgs = {'dmrid': [(tg,ts),(tg,ts)]}
bm_warn_tgs={'*your*repeaterid*':[(*your*tg1*,*slot*),(*your*tg2*,*your*slot*)]}
## list of WarnCellIDs to be monitored !! '*' in warncellids overrides warncellid-check
## usage: warncellids = [warncellid1, warncellid2]
warncellids = ['*your*warncellid1*','*your*warncellid2*']
# warncellid-prefix
# 1 = Landkreis
# 2 = Seen
# 4 = Seegebiete
# 5 = Küstengebiete
# 6 = Objekte
# 7 = Stadtteile
# 8 = Gemeinden
# 9 = Bundesländer
#
# known warncell-ids
# 903000000 = Niedersachsen
# 103155000 = Landkreis Northeim
# 803155012 = Uslar
# 803155002 = Bodenfelde
# 806633027 = Wahlsburg (Lippoldsberg/Vernawahlshausen)
## list of warntypes and levels to be monitored
## usage: warntypelevel = {type: minlevel, type: [level2, level3]}
warntypelevel = {0:[1,3,4,5],1:4,2:[1,3,4,5]}
# warnlevels
# 1 = Vorabinfo
# 2 = Warnung
# 3 = markantes Wetter
# 4 = Unwetter
# 5 = Extremes Unwetter
# 10 = Hitzewarnung
# warntypes
# 0 = Gewitter
# 1 = Wind/Sturm/Orkan
# 2 = Stark oder Dauerregen
# 3 = Schnee
# 4 = Nebel
# 5 = Frost
# 6 = Glätte
# 7 = Tauwetter
# 8 = Hitze
# 9 = UV
# 10 = Küstenwetter
# 11 = Binnenwetter
## !! below this line there is no need to change anything !!
## ** functions **
def switch_bm_tgs(warn=False,state=True):
# read tg-config from brandmeister.network via api
# switch tg if warning true an state changed
for dmrid in bm_warn_tgs:
print('lese statische TG von',dmrid,'...')
bm_response = requests.get(BM_GETURL.replace('%BM_DMRID%',dmrid), headers={'User-Agent': USER_AGENT})
bm_state = json.loads(bm_response.text)
bm_static = bm_state['staticSubscriptions']
bm_current_tgs = {0:[],1:[],2:[]}
temp_log = 'DMRId: %s '%dmrid
dn_log = 'DMRId:%s '%dmrid
for static_tg in bm_static:
bm_current_tgs[static_tg['slot']].append(static_tg['talkgroup'])
for tg,slot in bm_warn_tgs[dmrid]:
if tg in bm_current_tgs[slot]:
if warn:
print('>> TG',tg,'aus slot',slot,'entfernt')
temp_log += 'TS %i TG %i : dynamisch '%(slot,tg)
dn_log += 'TS%i,TG%i:dyn '%(slot,tg)
set_bm_tg(dmrid,tg,slot,False)
else:
print('>> TG',tg,'in slot',slot,'bereits gebucht')
else:
if warn:
print('>> TG',tg,'in slot',slot,'nicht gebucht')
else:
print('>> TG',tg,'in slot',slot,'hinzugefügt')
temp_log += 'TS %i TG %i : statisch '%(slot,tg)
dn_log += 'TS%i,TG%i:stat '%(slot,tg)
set_bm_tg(dmrid,tg,slot,True)
if warn and state != warn:
send_tg_msg(temp_log,dmrid,event)
send_dn_msg(event+' '+dn_log,dn_callsigns)
elif not warn and state != warn:
send_tg_msg(temp_log,dmrid,'Unwetter-Modus AUS')
send_dn_msg('NORMALBETRIEB '+dn_log,dn_callsigns)
def send_tg_msg(msg,grp = '',titel = ''):
# send message to telegram by WebALARM_BOT
tg_url = TG_WA_URL.replace('%TG_MSG%', msg)
tg_url = tg_url.replace('%TG_GRP%', grp)
tg_url = tg_url.replace('%TG_TITEL%', titel)
tg_data = {}
tg_header = {'User-Agent': USER_AGENT}
for apikey in tg_wa_api:
temp_tg_url = tg_url.replace('%TG_API%',apikey)
#print(temp_tg_url)
tg_response = requests.post(temp_tg_url, headers=tg_header)
#print(tg_response.text)
def set_bm_tg(dmrid,tg,slot,settg):
# set brandmeister.network tg by api
if settg:
action='ADD'
else:
action='DEL'
bm_url = BM_SETURL.replace('%BM_DMRID%',dmrid)
bm_url = bm_url.replace('%ACTION%',action)
bm_auth = (bm_apikey,'')
bm_data = {'talkgroup': str(tg), 'timeslot': str(slot)}
bm_header = {'User-Agent': USER_AGENT}
bm_response = requests.post(bm_url, data=bm_data, auth=bm_auth, headers=bm_header)
def replace_multi(string,repl_dict):
for key in repl_dict:
string = string.replace(key,repl_dict[key])
return string
def send_dn_msg(msg,callsigns):
dn_header = {'user-agent': USER_AGENT, 'content-type': 'application/json'}
dn_msg = SERVICE+': '+replace_multi(msg,dn_replace)
for callsign in callsigns:
if (len(dn_msg) > 80):
dn_msg = dn_msg[:77]+'...'
dn_data = {'text': dn_msg, 'callSignNames': [callsign], 'transmitterGroupNames': dn_txgrp}
dn_response = requests.post(DN_URL, data=json.dumps(dn_data), auth=dn_auth, headers=dn_header)
## ** main thread **
if __name__ == "__main__":
try:
with open(STATEFILE,"r") as f:
warnung_status = bool(int(f.read().strip()))
except:
warnung_status = false
warnlage_response = requests.get(DWD_URL, headers={'User-Agent': USER_AGENT})
warnlage_jsonp = warnlage_response.text
## using static dump of json for debugging
#with open('dwd.jsonp',"r") as f:
# warnlage_jsonp = f.read()
warnlage_jsonp = warnlage_jsonp.strip(DWD_PREFIX)
warnlage_json = warnlage_jsonp.strip(DWD_SUFFIX)
warnlage = json.loads(warnlage_json)
print('Warnlage von',time.ctime(warnlage['time']/1000),'\n')
try:
warnungen = {**warnlage['warnings'],**warnlage['vorabInformation']}
except:
warnungen = warnlage['warnings']
warnung_aktiv = False
warnung_event = ''
for warncell in warnungen:
if warncell in warncellids or '*' in warncellids:
for warnung in warnungen[warncell]:
if warnung['type'] in warntypelevel:
if type(warntypelevel[warnung['type']]) is int:
check = (warnung['level'] >= warntypelevel[warnung['type']])
else:
check = (warnung['level'] in warntypelevel[warnung['type']])
if check:
print('AKTIVE WARNUNG: ',warnung['headline'])
print(' > ','Level',warnung['level'], 'Type',warnung['type'])
print(' > ','Ort: ',warnung['regionName'])
print(' > ','ausgegeben: ',time.ctime(warnung['start']/1000))
if warnung['end'] == None:
print(' > ','gültig bis: ','Widerruf')
else:
print(' > ','gültig bis: ',time.ctime(warnung['end']/1000))
warnung_aktiv = True
warnung_event = warnung['event']
break
try:
warnung_aktiv = bool(int(sys.argv[1]))
print('override warnung',warnung_aktiv)
warnung_event = 'TESTMODUS'
except:
pass
if warnung_aktiv:
print('\n','>>> WARNLAGE: %s <<<'%(warnung_event),'\n')
else:
print('\n','* NORMALBETRIEB *','\n')
if warnung_status == warnung_aktiv:
print('keine Änderungen ...')
pass
else:
print('!! Lageänderung !!')
with open(STATEFILE,"w") as f:
f.write(str(int(warnung_aktiv)))
print('update brandmeister.network ...')
switch_bm_tgs(warnung_aktiv, warnung_status)
print('... fertig!')