new
This commit is contained in:
parent
887b4f4abf
commit
814871b481
55
MonitorPublisher.py
Normal file
55
MonitorPublisher.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import threading
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
from logger import Logger
|
||||||
|
import json
|
||||||
|
import datetime
|
||||||
|
from time import mktime, localtime
|
||||||
|
|
||||||
|
|
||||||
|
class MyEncoder(json.JSONEncoder):
|
||||||
|
|
||||||
|
def default(self, obj):
|
||||||
|
if isinstance(obj, datetime.datetime):
|
||||||
|
return str(obj)
|
||||||
|
|
||||||
|
return json.JSONEncoder.default(self, obj)
|
||||||
|
|
||||||
|
class MonitorPublisher(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()
|
||||||
|
|
||||||
|
lastEvent = 0.0
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
msg = self.queue.get()
|
||||||
|
|
||||||
|
monitorMessages = [
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1'],
|
||||||
|
['header1', 'body1']
|
||||||
|
]
|
||||||
|
currentTime = mktime(localtime())
|
||||||
|
if (currentTime > lastEvent + 60):
|
||||||
|
lastEvent = currentTime
|
||||||
|
client.publish("IoT/Message/Monitor", monitorMsg)
|
||||||
|
Logger.log("MonitorPublisher has sent data")
|
||||||
|
except Exception, e:
|
||||||
|
Logger.log("Unexcepted exception %s in MonitorPublisher: %s" % (e.__class__.__name__, str(e)))
|
Loading…
x
Reference in New Issue
Block a user