forked from Iture/max-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Max!-MQTT.py
59 lines (47 loc) · 1.34 KB
/
Max!-MQTT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
import json
import logging
import multiprocessing
import time
import tornado.gen
import tornado.ioloop
import tornado.websocket
from tornado.options import options
import MQTTClient
import MaxWorker
logger = logging.getLogger('Max!-MQTT')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(funcName)s - %(levelname)s - %(message)s')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('Max!-MQTT.log')
fh.setLevel(logging.ERROR)
fh.setFormatter(formatter)
logger.addHandler(fh)
ch = logging.StreamHandler()
ch.setFormatter(formatter)
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
def main():
# messages read from device
messageQ = multiprocessing.Queue()
# messages written to device
commandQ = multiprocessing.Queue()
config = {}
try:
with open('config.json') as json_data:
config = json.load(json_data)
except Exception as e:
logger.error("Config load failed")
exit(1)
mw = MaxWorker.MaxWorker(messageQ, commandQ, config)
mw.daemon = True
mw.start()
mqtt = MQTTClient.MQTTClient(messageQ, commandQ, config)
mqtt.daemon = True
mqtt.start()
# wait a second before sending first task
time.sleep(1)
options.parse_command_line()
mainLoop = tornado.ioloop.IOLoop.instance()
mainLoop.start()
if __name__ == "__main__":
main()