From c39203da91f321c7a62cceff3f01a9c5d6a1d897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20H=C3=BCbner?= <pavel.hubner@bigclown.com> Date: Sat, 17 Jun 2017 09:26:24 +0200 Subject: [PATCH 1/3] Add Decimal support for JSON parsing / dump --- gateway/bc-gateway.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/gateway/bc-gateway.py b/gateway/bc-gateway.py index 3f7bfeb..217fb6c 100755 --- a/gateway/bc-gateway.py +++ b/gateway/bc-gateway.py @@ -7,6 +7,7 @@ import argparse import json import platform +import decimal import yaml import serial import serial.tools.list_ports @@ -40,6 +41,12 @@ def mqtt_on_message(client, userdata, message): userdata['serial'].write(b'["' + subtopic.encode('utf-8') + b'",' + payload + b']\n') +def decimal_default(obj): + if isinstance(obj, decimal.Decimal): + return float(obj) + raise TypeError + + def run(): base_topic = config['mqtt']['topic'].rstrip('/') + '/' @@ -68,12 +75,13 @@ def run(): if line: logging.debug(line) try: - talk = json.loads(line.decode()) + talk = json.loads(line.decode(), parse_float=decimal.Decimal) except Exception: logging.error('Invalid JSON message received from serial port: %s', line) try: - mqttc.publish(base_topic + talk[0], json.dumps(talk[1]), qos=1) + mqttc.publish(base_topic + talk[0], json.dumps(talk[1], default=decimal_default), qos=1) except Exception: + raise logging.error('Failed to publish MQTT message: %s', line) From e59cb7c972bd677f1a6f76d07b042607156a22e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20H=C3=BCbner?= <pavel.hubner@bigclown.com> Date: Sat, 17 Jun 2017 09:28:28 +0200 Subject: [PATCH 2/3] Cast MQTT port to integer --- gateway/bc-gateway.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gateway/bc-gateway.py b/gateway/bc-gateway.py index 217fb6c..4b32501 100755 --- a/gateway/bc-gateway.py +++ b/gateway/bc-gateway.py @@ -63,7 +63,7 @@ def run(): mqttc = paho.mqtt.client.Client(userdata={'serial': ser, 'base_topic': base_topic}) mqttc.on_connect = mqtt_on_connect mqttc.on_message = mqtt_on_message - mqttc.connect(config['mqtt']['host'], config['mqtt']['port'], keepalive=10) + mqttc.connect(config['mqtt']['host'], int(config['mqtt']['port']), keepalive=10) mqttc.loop_start() while True: From 85e49099d55463caf08159adfd4cb41cc53fe7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20H=C3=BCbner?= <pavel.hubner@bigclown.com> Date: Sat, 17 Jun 2017 10:20:01 +0200 Subject: [PATCH 3/3] Add FakeFloat class and fix reference --- gateway/bc-gateway.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/gateway/bc-gateway.py b/gateway/bc-gateway.py index 4b32501..779280d 100755 --- a/gateway/bc-gateway.py +++ b/gateway/bc-gateway.py @@ -30,6 +30,21 @@ 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 + + def mqtt_on_connect(client, userdata, flags, rc): logging.info('Connected to MQTT broker with code %s', rc) client.subscribe(userdata['base_topic'] + '+/+/+/+/+') @@ -37,16 +52,10 @@ def mqtt_on_connect(client, userdata, flags, rc): def mqtt_on_message(client, userdata, message): subtopic = message.topic[len(userdata['base_topic']):] - payload = message.payload if msg.payload else b'null' + payload = message.payload if message.payload else b'null' userdata['serial'].write(b'["' + subtopic.encode('utf-8') + b'",' + payload + b']\n') -def decimal_default(obj): - if isinstance(obj, decimal.Decimal): - return float(obj) - raise TypeError - - def run(): base_topic = config['mqtt']['topic'].rstrip('/') + '/' @@ -81,7 +90,6 @@ def run(): try: mqttc.publish(base_topic + talk[0], json.dumps(talk[1], default=decimal_default), qos=1) except Exception: - raise logging.error('Failed to publish MQTT message: %s', line)