Commit e8717535 authored by Peter-Bernd Otte's avatar Peter-Bernd Otte

Error handler for not JSON msgs added

parent f77e6035
......@@ -8,6 +8,7 @@ import parse
from http.server import HTTPServer, BaseHTTPRequestHandler
import logging, argparse
from urllib.parse import unquote # for converting url encoded string to unicode string
import traceback
logging.basicConfig(format="%(asctime)-15s %(levelname)-8s %(message)s")
logger = logging.getLogger("MQTT-HTTP-Server")
......@@ -41,17 +42,20 @@ def on_message(client, userdata, message):
global mlast
t = time.time()
m = message.payload.decode("utf-8")
# print("received:",str(m), message.topic)
# print("received:", message.topic, str(m))
# the lambda in loads is for converting the object name into a int, if appropiate
j = json.loads(m, object_hook=lambda d: {int(k) if k.lstrip('-').isdigit() else k: v for k, v in d.items()})
# print ("Topic:",message.topic,"JSON",j)
if type(j) is not dict: #anpassen, falls es keine Zeitinfo hat
j={'v':j,'time':t}
if t-j['time'] > 10: # corrects the time of too much in the future
j['time'] = t
mlast[message.topic] = j
try:
j = json.loads(m, object_hook=lambda d: {int(k) if k.lstrip('-').isdigit() else k: v for k, v in d.items()})
# print ("Topic:",message.topic,"JSON",j)
if type(j) is not dict: #anpassen, falls es keine Zeitinfo hat
j={'v':j,'time':t}
if t-j['time'] > 10: # corrects the time of too much in the future
j['time'] = t
mlast[message.topic] = j
except:
print("The following MQTT message was not processed:", message.topic, str(m))
client= paho.Client(args.mqtt_client_name)
client.on_message=on_message
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment