Skip to content

Commit

Permalink
Merge pull request #3 from bigclownlabs/decimal
Browse files Browse the repository at this point in the history
Fix decimal format
  • Loading branch information
blavka authored Jun 17, 2017
2 parents 85cdf21 + 4caac51 commit f5804d7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
19 changes: 2 additions & 17 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,21 +30,6 @@
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'] + '+/+/+/+/+')
Expand Down Expand Up @@ -88,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], default=decimal_default), 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 f5804d7

Please sign in to comment.