-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcr_to_mqtt.py
executable file
·286 lines (223 loc) · 8.93 KB
/
cr_to_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/python3
import paho.mqtt.client as mqtt
import time
import logging
import logging.handlers
import json
import pidfile
import datetime
import dateutil.parser
import os.path
import threading
from PyCampbellCR1000.pycampbellcr1000.exceptions import NoDeviceException
import sys
import traceback
logfile = os.path.splitext(sys.argv[0])[0] + ".log"
logging.basicConfig(level=logging.INFO,
handlers=(logging.StreamHandler(stream=sys.stdout),
logging.handlers.RotatingFileHandler(logfile,
maxBytes = 256*1024,
backupCount = 6), ),
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.getLogger('pycampbellcr1000').setLevel(logging.WARN)
logging.getLogger('pylink').setLevel(logging.WARN)
LOG = logging.getLogger(__name__)
mqLOG = logging.getLogger("MQTT")
mqLOG.setLevel(logging.DEBUG)
# mqtt logging levels to python levels
logmap = {mqtt.MQTT_LOG_INFO: logging.INFO,
mqtt.MQTT_LOG_NOTICE: logging.INFO,
mqtt.MQTT_LOG_WARNING : logging.WARN,
mqtt.MQTT_LOG_ERR : logging.ERROR,
mqtt.MQTT_LOG_DEBUG: logging.DEBUG}
TABLE_FILE = "tables.json"
MQHOST = "localhost"
MQPORT = 9999
mqconnect = threading.Event()
client = None
# pending mqtt messages
plock = threading.Lock()
pending = {}
from PyCampbellCR1000.pycampbellcr1000 import CR1000
def load_tables():
'load tables parsing datetimes'
if os.path.exists(TABLE_FILE):
with open(TABLE_FILE, 'r') as fp:
tables = json.load(fp)
return {k:dateutil.parser.parse(v) for k,v in tables.items()}
return None
def save_tables(tables):
'save tables fixing datatimes to strings'
# serialize first so if that breaks, we don't overwrite the file
data = json.dumps({k:v.isoformat() for k,v in tables.items()})
with open(TABLE_FILE, 'w') as fp:
fp.write(data)
def emit_record(topic, rec):
global client
if client== None:
client = get_connected_client()
p = 11
tries = 0
while p > 10:
p = 0
plock.acquire()
p = len(pending)
plock.release()
if p > 10:
tries += 1
if tries > 10:
LOG.info("restarting mqtt connection")
try:
if (client.connect(MQHOST, MQPORT, 60) != mqtt.MQTT_ERR_SUCCESS):
LOG.error("Failed to connect to MQTT server.")
return None
except ConnectionRefusedError:
LOG.error("MQTT server connection refused at {}:{} check the server.".format(MQHOST, MQPORT))
tries = 0
else:
LOG.info("wait for pending messages before queuing more [{}]".format(p))
time.sleep(1)
# make it serializable
d = rec['Datetime']
rec['Datetime'] = rec['Datetime'].isoformat()
result, mid = client.publish(topic,json.dumps(rec), qos=2)
if result != mqtt.MQTT_ERR_SUCCESS:
LOG.warn("MQTT publish failed with {}, restarting connection.".format(mqtt.error_string(result)))
try:
shutdown_client()
except:
pass
else:
LOG.info("emit [{}] to {}: {}".format(mid, topic, rec))
plock.acquire()
pending[mid] = datetime.datetime.now()
plock.release()
rec['Datetime'] = d
#for x,v in rec.items():
#print (x,v)
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
LOG.info("MQTT Connected with result code "+str(rc))
mqconnect.set()
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
LOG.info("rcv: "+msg.topic+" "+str(msg.payload))
def on_log(client, userdata, level, buf):
# called when the client has log information. Define
# to allow debugging. The level variable gives the severity of the message
# and will be one of MQTT_LOG_INFO, MQTT_LOG_NOTICE, MQTT_LOG_WARNING,
#MQTT_LOG_ERR, and MQTT_LOG_DEBUG. The message itself is in buf.
mqLOG.log(logmap[level], buf)
def on_publish(client, userdata, mid):
# message hit the broker
n = datetime.datetime.now()
plock.acquire()
tx = pending[mid]
del pending[mid]
plock.release()
LOG.info("Pub [{}] took: {}".format(mid, n-tx))
def get_connected_client():
global client
if client != None:
return client
LOG.info("connecting MQTT client")
client = mqtt.Client()
try:
if (client.connect(MQHOST, MQPORT, 60) != mqtt.MQTT_ERR_SUCCESS):
LOG.error("Failed to connect to MQTT server.")
return None
except ConnectionRefusedError:
LOG.error("MQTT server connection refused at {}:{} check the server.".format(MQHOST, MQPORT))
return None
client.on_connect = on_connect
client.on_message = on_message
client.on_log = on_log
client.on_publish = on_publish
client.loop_start()
if not mqconnect.wait(timeout=30):
LOG.fatal("Failed to connect to MQ server")
return None
return client
def shutdown_client():
global client
if client != None:
LOG.info("disconnecting MQTT client")
p = 1
tries = 0
while p > 0:
p = 0
plock.acquire()
p = len(pending)
plock.release()
if p > 0:
tries += 1
if tries > 10:
LOG.info("restarting mqtt connection")
try:
if (client.connect(MQHOST, MQPORT, 60) != mqtt.MQTT_ERR_SUCCESS):
LOG.error("Failed to connect to MQTT server.")
return None
except ConnectionRefusedError:
LOG.error("MQTT server connection refused at {}:{} check the server.".format(MQHOST, MQPORT))
tries = 0
else:
LOG.info("wait for pending messages to shutdown [{}]".format(p))
time.sleep(1)
client.disconnect()
time.sleep(1)
client.loop_stop()
client.on_publish = None
client.on_connect = None
client.on_message = None
client.on_log = None
client = None
def connect_and_download():
LOG.debug("creating device.")
tables = load_tables()
try:
device = CR1000.from_url('serial:/dev/ttyACM0:57600',
src_addr=4004,
#src_addr=4003,
dest_addr=1235,
#dest_addr=1234,
timeout=1)
LOG.debug ('have device: {}'.format(device))
LOG.info ("device time: {}".format(device.gettime()))
if tables == None or len(tables) == 0:
tlist = device.list_tables()
tables ={x: datetime.datetime.now() for x in tlist if not x in ['Status', 'Public']}
save_tables(tables)
mqroot = 'CR6/{}'.format(device.serialNo)
#make the mqtt connection if needed
client = get_connected_client()
if client == None:
return False
for tablename, lastcollect in tables.items():
#if tablename != 'WO209060_PBM':
#continue
LOG.info("Download {} from {}".format(tablename, lastcollect))
for items in device.get_data_generator(tablename,
start_date = lastcollect):
for record in items:
LOG.debug("got record: {}".format(record))
emit_record(mqroot+'/'+tablename, record)
time.sleep(0.1)
tables[tablename] = items[-1]['Datetime'] + datetime.timedelta(seconds=1)
return True
finally:
shutdown_client()
save_tables(tables)
LOG.info("Collection complete.")
return False
if __name__ == "__main__":
pidfile.write()
while True:
try:
if connect_and_download():
time.sleep(27)
except NoDeviceException:
LOG.critical("No response from datalogger.")
except Exception as x:
LOG.critical("Failed with: {}".format(x))
LOG.critical(traceback.format_exc())
time.sleep(3)