add local broker and republisher

This commit is contained in:
Wolfgang Hottgenroth
2016-07-03 22:27:58 +02:00
parent 6c6f6c7e62
commit d3aa892f37
3 changed files with 82 additions and 22 deletions

40
RePublisher.py Normal file
View File

@ -0,0 +1,40 @@
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)
client.loop_start()
while True:
try:
msg = self.queue.get()
dataBlock = msg['data']
if 'decodedTelegram' in dataBlock:
del dataBlock['decodedTelegram']
if 'telegram' in dataBlock:
del dataBlock['telegram']
client.publish("IoT/ParsedData", json.dumps(msg, cls=MyEncoder))
Logger.log("RePublisher has sent data")
except Exception, e:
Logger.log("Unexcepted exception %s in RePublisher: %s" % (e.__class__.__name__, str(e)))