forked from binderth/mowas_mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mowas_mqtt.py
168 lines (149 loc) · 6.15 KB
/
mowas_mqtt.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*
import os # using the OS
import requests # requesting information
import json # working with JSON
import configparser # working with INI
from requests.exceptions import ConnectionError # errors in requests
import paho.mqtt.client as mqtt # using MQTT-client
# read settings from mowas_mqtt.ini
ApplicationDir = os.path.dirname(os.path.abspath(__file__))
ReadSettings = os.path.join(ApplicationDir, 'mowas_mqtt.ini')
Settings = configparser.ConfigParser()
Settings.read(ReadSettings)
# Log-Level
loglevel = Settings.get("General", "loglevel")
# MQTT-Settings
mqtt_ipaddress = Settings.get("MQTT", "Broker")
mqtt_user = Settings.get("MQTT", "User")
mqtt_pass = Settings.get("MQTT", "Password")
mqtt_topic = Settings.get("MQTT", "Topic")
mqtt_port = int(Settings.get("MQTT", "Port"))
mqtt_qos = int(Settings.get("MQTT", "QOS"))
mqtt_clientid = Settings.get("MQTT", "ClientID")
# Landkreis AGS
landkreis_ags=Settings.get("AGS", "AGScode")
# setting API information sort of from https://warnung.bund.de/bbk.config/config_rel.json
landkreis_status = 'https://warnung.bund.de/bbk.status/status_{}.json'.format(landkreis_ags)
json_URLs = {
'mowas': 'https://warnung.bund.de/bbk.mowas/gefahrendurchsagen.json',
'biwapp': 'https://warnung.bund.de/bbk.biwapp/warnmeldungen.json',
'katwarn': 'https://warnung.bund.de/bbk.katwarn/warnmeldungen.json',
'lhp': 'https://warnung.bund.de/bbk.lhp/hochwassermeldungen.json',
'dwd': 'https://warnung.bund.de/bbk.dwd/unwetter.json'
}
# start variables
buckets_live = {}
JSONreturn = {}
def on_connect(client, userdata, flags, rc):
# Connect to MQTT-Broker
print("Connected to broker with result code " + str(rc))
def send_mqtt(message, topic):
# send MQTT message
print ("Sending JSON with Broker '{}' to Topic '{}'".format(mqtt_ipaddress,topic))
if (loglevel == "DEBUG"):
print ("Sending message: " + message)
mqttclient = mqtt.Client(mqtt_clientid)
mqttclient.on_connect = on_connect
if mqtt_user != "":
mqttclient.username_pw_set(mqtt_user, mqtt_pass)
mqttclient.connect(mqtt_ipaddress, mqtt_port, 60)
mqttclient.loop_start()
mqttpub = mqttclient.publish(topic, mqtt_qos)
def get_json_as_dict(url):
# GET JSON from URL
global requestError
headers = {'Accept': '*/*',
'Content-Type': 'application/json; charset=utf-8',
'User-Agent': 'MOWAS-MQTT'}
# make request, gracefully with error
try:
if (loglevel == "DEBUG"):
print("request URL: {} - {}".format(str(url),str(headers)))
r = requests.get(url, headers=headers)
except ConnectionError as e:
requestError = True
print("Connection Error")
print(e)
return json.loads('{"data": {"Connection Error": 1, "URL": "'+url+'", "error": "'+e+'"}}')
# wenn HTTP-StatusCode not 200
if r.status_code != 200:
requestError = True
print("HTTP Error")
print(str(r.status_code))
return json.loads('{"data": {"HTTP Error": 1, "URL": "'+url+'", "error": "'+str(r.status_code)+'"}}')
# JSON to dict, gracefully with error
try:
data = json.loads(r.content.decode())
except ValueError as e:
requestError = True
print("JSON Error")
print(e)
return json.loads('{"data": {"JSON Error": 1, "URL": "'+url+'", "error": "'+e+'"}}')
requestError = False
return data
def search_in_dict (values, searchterm):
# find a term in a dict
for k in values:
print (values)
print (k)
for v in values[k]:
if searchterm in v:
return True
return False
if __name__ == "__main__":
# get JSON from Landkreis as dict
landkreis_meldungen = get_json_as_dict(landkreis_status)
# check, if there's an error with the JSON
if (requestError == True):
send_mqtt(json.dumps(landkreis_meldungen), mqtt_topic)
if loglevel == "DEBUG":
print(landkreis_meldungen)
quit('Error in URL: ' + landkreis_status)
# check, if there's announcements for the Landkreis
i = 0
for bucket in landkreis_meldungen:
# MOWAS JSON has multiple buckets, currently:
# 0: bkk.mowas
# 1: bbk.biwapp
# 3: bkk.katwarn
# 4: bkk.lhp
# 5: bkk.dwd
keys = bucket['bucketname'].split(".")
buckets_live[keys[1]] = {}
if (loglevel == "DEBUG"):
print("Find Buckets #" + str(i))
for ref in bucket['ref']:
# call the URL and find message
if (loglevel == "DEBUG"):
print("Add Bucket to JSON: " + ref)
buckets_live[keys[1]]['ref'+str(i)] = ref
i = i + 1
# get the announcements information
if (loglevel == "DEBUG"):
print("Add Bucket to JSON: " + ref)
i = 0
for buckets in buckets_live:
if (len(buckets_live[buckets]) > 0):
# There's announcements for that bucket!
if (loglevel == "DEBUG"):
print("open JSON for Bucket: " + json_URLs[buckets])
announcementJSON = get_json_as_dict(json_URLs[buckets])
if (loglevel == "DEBUG"):
print("Found Announcements in: " + json_URLs[buckets])
for meldung, meldewert in (buckets_live[buckets].items()):
j = 0
while (j < len(announcementJSON)):
if (loglevel == "DEBUG"):
print("search Announcement for: " + meldewert)
if (announcementJSON[j]["identifier"] == meldewert):
if (loglevel == "DEBUG"):
print("Added Announcement for: " + meldewert)
JSONreturn[i] = announcementJSON[j]
JSONreturn[i]["info"][0]["area"][0].pop("polygon")
j = j+1
i = i + 1
# Sending final JSON with MQTT
if loglevel == "DEBUG":
print ("Sending MQTT")
send_mqtt(json.dumps(JSONreturn), mqtt_topic)