Skip to content

Commit f5804d7

Browse files
authored
Merge pull request #3 from bigclownlabs/decimal
Fix decimal format
2 parents 85cdf21 + 4caac51 commit f5804d7

2 files changed

Lines changed: 6 additions & 20 deletions

File tree

gateway/bc-gateway.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
import logging
77
import argparse
8-
import json
8+
import simplejson as json
99
import platform
1010
import decimal
1111
import yaml
@@ -30,21 +30,6 @@
3030
LOG_FORMAT = '%(asctime)s %(levelname)s: %(message)s'
3131

3232

33-
class FakeFloat(float):
34-
35-
def __init__(self, value):
36-
self._value = value
37-
38-
def __repr__(self):
39-
return str(self._value)
40-
41-
42-
def decimal_default(obj):
43-
if isinstance(obj, decimal.Decimal):
44-
return FakeFloat(obj)
45-
raise TypeError
46-
47-
4833
def mqtt_on_connect(client, userdata, flags, rc):
4934
logging.info('Connected to MQTT broker with code %s', rc)
5035
client.subscribe(userdata['base_topic'] + '+/+/+/+/+')
@@ -88,7 +73,7 @@ def run():
8873
except Exception:
8974
logging.error('Invalid JSON message received from serial port: %s', line)
9075
try:
91-
mqttc.publish(base_topic + talk[0], json.dumps(talk[1], default=decimal_default), qos=1)
76+
mqttc.publish(base_topic + talk[0], json.dumps(talk[1], use_decimal=True), qos=1)
9277
except Exception:
9378
logging.error('Failed to publish MQTT message: %s', line)
9479

gateway/requirements.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
paho-mqtt>=1.0 # deb:python3-paho-mqtt>=1.0
2-
pyserial>=2.6 # deb:python3-serial>=2.6
3-
PyYAML>=3.12 # deb:python3-yaml>=3.12
1+
paho-mqtt>=1.0 # deb:python3-paho-mqtt>=1.0
2+
pyserial>=2.6 # deb:python3-serial>=2.6
3+
PyYAML>=3.12 # deb:python3-yaml>=3.12
4+
simplejson>=3.10.0 # deb:python3-simplejson>=3.10.0

0 commit comments

Comments
 (0)