Skip to content

Commit

Permalink
Merge pull request #2 from bigclownlabs/decimal
Browse files Browse the repository at this point in the history
Decimal
  • Loading branch information
blavka authored Jun 17, 2017
2 parents a9a48b0 + 85e4909 commit 85cdf21
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions gateway/bc-gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import argparse
import json
import platform
import decimal
import yaml
import serial
import serial.tools.list_ports
Expand All @@ -29,14 +30,29 @@
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'] + '+/+/+/+/+')


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')


Expand All @@ -56,7 +72,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:
Expand All @@ -68,11 +84,11 @@ 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:
logging.error('Failed to publish MQTT message: %s', line)

Expand Down

0 comments on commit 85cdf21

Please sign in to comment.