From 9db63b57c3ee7c6a604cfd6f8903f21d4613f448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20H=C3=BCbner?= Date: Sat, 17 Jun 2017 14:58:20 +0200 Subject: [PATCH 1/2] Fix decimal format --- gateway/bc-gateway.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/gateway/bc-gateway.py b/gateway/bc-gateway.py index 779280d..673c641 100755 --- a/gateway/bc-gateway.py +++ b/gateway/bc-gateway.py @@ -30,19 +30,11 @@ LOG_FORMAT = '%(asctime)s %(levelname)s: %(message)s' -class FakeFloat(float): - - def __init__(self, value): - self._value = value - - def __repr__(self): - return str(self._value) - - -def decimal_default(obj): - if isinstance(obj, decimal.Decimal): - return FakeFloat(obj) - raise TypeError +class DecimalEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, decimal.Decimal): + return eval(str(obj)) + return json.JSONEncoder.default(self, obj) def mqtt_on_connect(client, userdata, flags, rc): @@ -88,7 +80,7 @@ def run(): except Exception: logging.error('Invalid JSON message received from serial port: %s', line) try: - mqttc.publish(base_topic + talk[0], json.dumps(talk[1], default=decimal_default), qos=1) + mqttc.publish(base_topic + talk[0], json.dumps(talk[1], cls=DecimalEncoder), qos=1) except Exception: logging.error('Failed to publish MQTT message: %s', line) From 4caac5192a0a1d5159c5aaa2b59a35c7d1aa50dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20H=C3=BCbner?= Date: Sat, 17 Jun 2017 15:55:23 +0200 Subject: [PATCH 2/2] Migrate from json to simplejson --- gateway/bc-gateway.py | 11 ++--------- gateway/requirements.txt | 7 ++++--- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/gateway/bc-gateway.py b/gateway/bc-gateway.py index 673c641..a356d62 100755 --- a/gateway/bc-gateway.py +++ b/gateway/bc-gateway.py @@ -5,7 +5,7 @@ import time import logging import argparse -import json +import simplejson as json import platform import decimal import yaml @@ -30,13 +30,6 @@ LOG_FORMAT = '%(asctime)s %(levelname)s: %(message)s' -class DecimalEncoder(json.JSONEncoder): - def default(self, obj): - if isinstance(obj, decimal.Decimal): - return eval(str(obj)) - return json.JSONEncoder.default(self, obj) - - def mqtt_on_connect(client, userdata, flags, rc): logging.info('Connected to MQTT broker with code %s', rc) client.subscribe(userdata['base_topic'] + '+/+/+/+/+') @@ -80,7 +73,7 @@ def run(): except Exception: logging.error('Invalid JSON message received from serial port: %s', line) try: - mqttc.publish(base_topic + talk[0], json.dumps(talk[1], cls=DecimalEncoder), qos=1) + mqttc.publish(base_topic + talk[0], json.dumps(talk[1], use_decimal=True), qos=1) except Exception: logging.error('Failed to publish MQTT message: %s', line) diff --git a/gateway/requirements.txt b/gateway/requirements.txt index 5389839..22bbf60 100644 --- a/gateway/requirements.txt +++ b/gateway/requirements.txt @@ -1,3 +1,4 @@ -paho-mqtt>=1.0 # deb:python3-paho-mqtt>=1.0 -pyserial>=2.6 # deb:python3-serial>=2.6 -PyYAML>=3.12 # deb:python3-yaml>=3.12 \ No newline at end of file +paho-mqtt>=1.0 # deb:python3-paho-mqtt>=1.0 +pyserial>=2.6 # deb:python3-serial>=2.6 +PyYAML>=3.12 # deb:python3-yaml>=3.12 +simplejson>=3.10.0 # deb:python3-simplejson>=3.10.0 \ No newline at end of file