2016-07-03 22:27:58 +02:00
|
|
|
import threading
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
from logger import Logger
|
|
|
|
import json
|
|
|
|
import datetime
|
|
|
|
from time import mktime
|
|
|
|
|
|
|
|
|
|
|
|
class MyEncoder(json.JSONEncoder):
|
|
|
|
|
|
|
|
def default(self, obj):
|
|
|
|
if isinstance(obj, datetime.datetime):
|
|
|
|
return str(obj)
|
|
|
|
|
|
|
|
return json.JSONEncoder.default(self, obj)
|
|
|
|
|
|
|
|
class RePublisher(threading.Thread):
|
|
|
|
def __init__(self, queue, broker):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.queue = queue
|
|
|
|
self.broker = broker
|
|
|
|
self.setDaemon(True)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
client = mqtt.Client()
|
|
|
|
client.connect(self.broker, 1883, 60)
|
2017-07-18 21:23:19 +02:00
|
|
|
client.loop_start()
|
2016-07-03 22:27:58 +02:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
msg = self.queue.get()
|
|
|
|
dataBlock = msg['data']
|
2017-07-18 21:23:19 +02:00
|
|
|
if 'decodedTelegram' in dataBlock:
|
|
|
|
del dataBlock['decodedTelegram']
|
|
|
|
if 'telegram' in dataBlock:
|
|
|
|
del dataBlock['telegram']
|
2016-07-03 22:40:56 +02:00
|
|
|
metadataBlock = msg['metadata']
|
2017-07-18 21:23:19 +02:00
|
|
|
if 'Slave' in metadataBlock:
|
|
|
|
if metadataBlock['Slave'] == 'Thermometer' and metadataBlock['device'] == 'ModbusHub':
|
|
|
|
metadataBlock['name'] = 'FridgeThermometer'
|
|
|
|
topic = "IoT/ParsedData/%s" % metadataBlock['name']
|
2016-07-03 22:40:56 +02:00
|
|
|
client.publish(topic, json.dumps(msg, cls=MyEncoder))
|
2017-07-18 21:23:19 +02:00
|
|
|
Logger.log("RePublisher has sent data")
|
2016-07-03 22:27:58 +02:00
|
|
|
except Exception, e:
|
|
|
|
Logger.log("Unexcepted exception %s in RePublisher: %s" % (e.__class__.__name__, str(e)))
|