Skip to content

Commit

Permalink
Migrate from json to simplejson
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Hübner committed Jun 17, 2017
1 parent 9db63b5 commit 4caac51
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
11 changes: 2 additions & 9 deletions gateway/bc-gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import time
import logging
import argparse
import json
import simplejson as json
import platform
import decimal
import yaml
Expand All @@ -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'] + '+/+/+/+/+')
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 4 additions & 3 deletions gateway/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
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

0 comments on commit 4caac51

Please sign in to comment.