From 357906fcd1cdc0bb85805a0fc40e1872c9eedae7 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Sun, 11 Aug 2019 01:13:03 +0200 Subject: [PATCH] Load json for mqtt callbacks --- mqtt_sensord/sensorlib/base.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mqtt_sensord/sensorlib/base.py b/mqtt_sensord/sensorlib/base.py index 77687d6..cb8ddec 100644 --- a/mqtt_sensord/sensorlib/base.py +++ b/mqtt_sensord/sensorlib/base.py @@ -1,3 +1,9 @@ +try: + import json +except ImportError: + import ujson as json + + def sensor(cls): Sensor.register(cls) @@ -60,6 +66,11 @@ class Sensor(object): @classmethod def run_mqtt_callback(cls, mqtt_topic, data): + try: + data = json.loads(data) + except ValueError: + print("Could not parse data {}".format(data)) + if mqtt_topic in cls._mqtt_callbacks: cls._mqtt_callbacks[mqtt_topic](data) else: