-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (65 loc) · 2.09 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import serial, json, traceback, os, multiprocessing, time, datetime
import paho.mqtt.client as mqtt
import paho.mqtt.publish as MQTTPublish
def processMessages(ttyName, mqttHost, mqttPort, timeout, retryCount, reconnectSeconds = 600):
startTime = datetime.datetime.now()
reconnectDelay = datetime.timedelta(seconds=reconnectSeconds)
for i in range(1, retryCount):
try:
device = serial.Serial('/dev/' + ttyName, 9600, timeout=timeout)
except:
traceback.print_exc()
time.sleep(5)
break
for i in range(1, retryCount):
try:
mqttClient = mqtt.Client()
mqttClient.connect(mqttHost, mqttPort, timeout)
except:
traceback.print_exc()
time.sleep(5)
break
while True:
currentTime = datetime.datetime.now()
if currentTime - startTime > reconnectDelay:
mqttClient.reinitialise()
startTime = datetime.datetime.now()
mqttClient.connect(mqttHost, mqttPort, timeout)
line = None
parsedLine = None
try:
line = device.readline().decode('utf-8')
except:
traceback.print_exc()
time.sleep(5)
try:
if line:
parsedLine = json.loads(line)
except:
traceback.print_exc()
time.sleep(1)
try:
if parsedLine:
for topic in parsedLine.keys():
mqttClient.publish(topic, parsedLine[topic])
except:
traceback.print_exc()
scriptPath = os.path.dirname(os.path.realpath(__file__))
configPath = os.path.join(scriptPath, 'config.json')
config = open(configPath).read()
config = json.loads(config)
if 'delay' in config:
time.sleep(config['delay'])
if config['scan']:
ttyName = []
processes = []
for i in os.listdir('/dev/'):
if config['ttyName'] in i:
ttyName.append(i)
pool = multiprocessing.Pool(len(ttyName))
for path in ttyName:
processes.append(pool.apply_async(processMessages, [path, config['mqtt']['host'], config['mqtt']['port'], config['timeout'], config['retryCount']]))
pool.close()
pool.join()
else:
processMessages(config['ttyName'], config['mqtt']['host'], config['mqtt']['port'], config['timeout'], config['retryCount'])