Skip to content

Commit

Permalink
Add FakeFloat class and fix reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Hübner committed Jun 17, 2017
1 parent e59cb7c commit 85e4909
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions gateway/bc-gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,32 @@
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')


def decimal_default(obj):
if isinstance(obj, decimal.Decimal):
return float(obj)
raise TypeError


def run():
base_topic = config['mqtt']['topic'].rstrip('/') + '/'

Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 85e4909

Please sign in to comment.