-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqparse.py
588 lines (503 loc) · 21.2 KB
/
amqparse.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# -*- coding: utf-8 -*-
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Created on 20 Apr 2018
#
# @author: rhamilton
"""One line description of module.
Further description.
"""
from __future__ import division, print_function, absolute_import
import os
import collections
import datetime as dt
from dateutil import tz
import xmltodict as xmld
import xmlschema as xmls
from stomp.listener import ConnectionListener
from ligmos import utils
class DCTConsumer(ConnectionListener):
def __init__(self, dbconn=None):
"""
This will really be stuffed into a
utils.amq.amqHelper class, so all the connections stuff is
really over there in that class. This is just to route the
DCT-specific messages to the right parsers
"""
# Adding an extra argument to the subclass
self.dbconn = dbconn
# Grab all the schemas that are in the ligmos library
self.schemaDict = utils.amq.schemaDicter()
def on_message(self, headers, body):
"""
Basically subclassing stomp.listener.ConnectionListener
"""
badMsg = False
tname = headers['destination'].split('/')[-1].strip()
# Manually turn the bytestring into a string
try:
body = body.decode("utf-8")
badMsg = False
except UnicodeDecodeError as err:
print(str(err))
print("Badness 10000")
print(body)
badMsg = True
if badMsg is False:
try:
xml = xmld.parse(body)
# If we want to have the XML as a string:
# res = {tname: [headers, dumpPacket(xml)]}
# If we want to have the XML as an object:
res = {tname: [headers, xml]}
except xmld.expat.ExpatError:
# This means that XML wasn't found, so it's just a string
# packet with little/no structure. Attach the sub name
# as a tag so someone else can deal with the thing
res = {tname: [headers, body]}
except Exception as err:
# This means that there was some kind of transport error
# or it couldn't figure out the encoding for some reason.
# Scream into the log but keep moving
print("="*42)
print(headers)
print(body)
print(str(err))
print("="*42)
badMsg = True
# Now send the packet to the right place for processing.
# These need special parsing because they're just straight text
if badMsg is False:
try:
if tname == 'joePduResult':
parserPDU(headers, body, db=self.dbconn)
elif tname == 'lightPathInformation':
parserLPI(headers, body, db=self.dbconn)
elif tname.endswith("loisLog"):
parserLOlogs(headers, body, db=self.dbconn)
elif tname in ['tcs.loisTelemetry',
'AOS.AOSPubDataSV.AOSDataPacket',
'WRS.WRSPubDataSV.WRSDataPacket',
'TCS.TCSSharedVariables.TCSHighLevelStatusSV.TCSTcsStatusSV',
'Ryans.DCTWeatherStream']:
# TODO: Wrap this in a proper try...except
# As of right now, it'll be caught in the "WTF!!!"
schema = self.schemaDict[tname]
parserFlatPacket(headers, body,
schema=schema, db=self.dbconn)
elif tname in ['AOS.AOSSubDataSV.RelativeFocusOffset',
'AOS.AOSSubDataSV.AbsoluteFocusOffset',
'MTS.MTSPubDataSV.MountTemperature']:
parserSimpleFloat(headers, body, db=self.dbconn)
else:
# Intended to be the endpoint of the auto-XML publisher
# so I can catch most of them rather than explicitly
# check in the if/elif block above
print("Orphan topic: %s" % (tname))
print(headers)
print(body)
print(res)
except Exception as err:
# This is a catch-all to help find parsing errors that need
# to be fixed since they're not caught in a parser* func.
print("="*11)
print("WTF!!!")
print(str(err))
print(headers)
print(body)
print("="*11)
def packetVintage(ts, nowUTC):
"""
Given an XML packet's timestamp, return how old it is compared to when
it was queried/grabbed.
Does some datetime/timezone shenanigans to account for UTC offset of
the timestamp as reported.
"""
# Fun with datetime...stupid colon in the UTC offset! Kill it with fire.
if ts[-3:-2] == ":":
ts = ts[:-3]+ts[-2:]
# Now parse the timestamp normally
dtts = dt.datetime.strptime(ts, "%Y-%m-%dT%H:%M:%S.%f%z")
dtts_utc = dtts.astimezone(tz.tzutc())
# Now trim out the tzinfo so we can just subtract. MUST assign again here!
dtts_utc = dtts_utc.replace(tzinfo=None)
vintage = ((nowUTC - dtts_utc).total_seconds())
return vintage
def dumpPacket(pxml):
"""
Given a packet parsed by xmltodict, unparse it and spit it out to the
console so you can actually see what's in the thing.
Assumes that the first element of the parsed XML packet is a descriptive
name of whatever it actually is. UPDATE: It definitely isn't.
"""
# Actually unparse it and then spit it out
pretty = xmld.unparse(pxml, pretty=True)
return pretty
def flatten(d, parent_key='', sep='_'):
"""
Thankfully StackOverflow exists because I'm too tired to write out this
logic myself and now I can just use this:
https://stackoverflow.com/a/6027615
With thanks to:
https://stackoverflow.com/users/1897/imran
https://stackoverflow.com/users/1645874/mythsmith
"""
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
def parserFlatPacket(hed, msg, schema=None, db=None, debug=False):
"""
"""
# print(msg)
# This is really the topic name, so we'll make that the measurement name
# for the sake of clarity. It NEEDS to be a list until I fix packetizer!
meas = [os.path.basename(hed['destination'])]
# Bail if there's a schema not found; needs expansion here
if schema is None:
print("No schema found for topic %s!" % (meas[0]))
return None
# In this house, we only store valid packets!
good = schema.is_valid(msg)
# A DIRTY DIRTY HACK
try:
xmlp = schema.to_dict(msg, decimal_type=float, validation='lax')
# print(xmlp)
good = True
except xmls.XMLSchemaValidationError:
good = False
if good is True:
if debug is True:
print("Packet good!")
try:
xmlp = schema.to_dict(msg, decimal_type=float, validation='lax')
# I HATE THIS
if isinstance(xmlp, tuple):
xmlp = xmlp[0]
# Back to normal.
keys = xmlp.keys()
fields = {}
# Store each key:value pairing
for each in keys:
val = xmlp[each]
# TESTING
if isinstance(val, dict):
flatVals = flatten(val, parent_key=each)
fields.update(flatVals)
else:
fields.update({each: val})
if fields is not None:
# Note: passing ts=None lets python Influx do the timestamp
packet = utils.packetizer.makeInfluxPacket(meas=meas,
ts=None,
tags=None,
fields=fields)
# print(packet)
# Actually commit the packet. singleCommit opens it,
# writes the packet, and then optionally closes it.
if db is not None:
db.singleCommit(packet, table=db.tablename,
close=True)
except xmls.XMLSchemaDecodeError as err:
print(err.message.strip())
print(err.reason.strip())
# Added for itteratively testing parsed packets outside of the
# usual operational mode (like in toymodels/PacketSchemer)
if debug is True:
print(fields)
else:
if debug is True:
print("Packet was bad!?")
def parserLOlogs(hed, msg, db=None, badFWHM=100.):
"""
'22:26:55 Level_4:CCD Temp:-110.06 18.54 Setpoints:-109.95 0.00 '
'22:26:55 Level_4:Telescope threads have been reactivated'
"""
# ts = hed['timestamp']
topic = os.path.basename(hed['destination'])
# print(ts, msg)
# Some time shenanigans; the LOIS log doesn't include date but
# we can assume it's referencing UT time on the same day.
# I suppose that there could be some ambiguity right at UT midnight
# ... but oh well.
now = dt.datetime.utcnow()
ltime = msg[0:8].split(":")
# Bail early since this indicates it's not really a log line but
# some other type of message (like a LOIS startup or something)
if len(ltime) != 3:
print("Unknown log line!")
print(msg)
return
now = now.replace(hour=int(ltime[0]), minute=int(ltime[1]),
second=int(ltime[2]), microsecond=0)
# lts = now.strftime("%Y-%m-%dT%H:%M:%S.%f")
# Get just the log level
loglevel = msg.split(" ")[1].split(":")[0]
# Now get the message, putting back together anything split by ":"
# this is so we can operate fully on the full message string
logmsg = " ".join(msg.split(":")[3:]).strip()
# print("%s from %s:" % (lts, topic))
# Set the stage for our eventual influxdb packet
meas = ['InstrumentTelemetry']
tags = {'name': topic.split(".")[1]}
if loglevel in ("Level_5", "Level_4"):
fields = None
if logmsg.startswith("CCD sensor adus"):
# print("Parsing: %s" % (logmsg))
# CCD sensor adus temp1 2248 temp2 3329 set1 2249 heat1 2016'
adutemp1 = int(logmsg.split(" ")[4])
adutemp2 = int(logmsg.split(" ")[6])
aduset1 = int(logmsg.split(" ")[8])
aduheat1 = int(logmsg.split(" ")[10])
fields = {"aduT1": adutemp1}
fields.update({"aduT2": adutemp2})
fields.update({"aduT2": adutemp2})
fields.update({"aduS1": aduset1})
fields.update({"aduH1": aduheat1})
# print(adutemp1, adutemp2, aduset1, aduheat1)
elif logmsg.startswith("CCD Heater"):
# NOTE! This one will have had a ":" removed by the
# logmsg creation line above, so you can just split normally
# print("Parsing: %s" % (logmsg))
# CCD Heater Values:1.21 0.00
heat1 = float(logmsg.split(" ")[3])
heat2 = float(logmsg.split(" ")[4])
fields = {"H1": heat1}
fields.update({"H2": heat2})
# print(heat1, heat2)
elif logmsg.startswith("CCD Temp"):
# Same as "CCD Heater" in that ":" have been removed by this point
# print("Parsing: %s" % (logmsg))
# CCD Temp -110.06 18.54 Setpoints -109.95 0.00 '
temp1 = float(logmsg.split(" ")[2])
temp2 = float(logmsg.split(" ")[3])
set1 = float(logmsg.split(" ")[5])
set2 = float(logmsg.split(" ")[6])
fields = {"T1": temp1}
fields.update({"T2": temp2})
fields.update({"S1": set1})
fields.update({"S2": set2})
fields.update({"T1S1delta": temp1-set1})
# print(temp1, temp2, set1, set2)
else:
fields = None
# print(loglevel, logmsg)
elif loglevel == "Level_2":
fields = None
if logmsg.startswith("SendAnalysis"):
apts = logmsg.split()
if apts[3].startswith("PHOT"):
fnum, subfno = apts[3].split("(")[1].split(")")[0].split(",")
centx = float(apts[4])
centy = float(apts[5])
fwhm = float(apts[6])
instmag = float(apts[7])
instmagerr = float(apts[8])
skymean = float(apts[9])
tags.update({"frame": fnum})
tags.update({"subframenum": subfno})
fields = {"centx": centx}
fields.update({"centy": centy})
fields.update({"fwhm": fwhm})
fields.update({"instmag": instmag})
fields.update({"instmagerr": instmagerr})
fields.update({"skymean": skymean})
# Final check for goodness.
# NOTE: The badFWHM value is hardcoded into LOIS when the
# centroid or profile fit fails. So we'll skip those.
if fwhm == badFWHM:
# Nuke the entire site from orbit...
fields = None
tags = None
else:
# Need this otherwise we'll get an exception error for all
# unparsed log lines! fields won't be defined right below here
# and it'll trigger a NameError for 'fields'
fields = None
# Make the InfluxDB packet and store it, skipping if fields is None
if fields is not None:
# Note: passing ts=None lets python Influx do the timestamp for you
packet = utils.packetizer.makeInfluxPacket(meas=meas,
ts=None,
tags=tags,
fields=fields)
# print(packet)
# Actually commit the packet. singleCommit opens it,
# writes the packet, and then optionally closes it.
if db is not None:
db.singleCommit(packet, table=db.tablename, close=True)
def parserLPI(_, msg, db=None):
"""
'mirrorCoverMode=Open'
'instrumentCoverState=OPEN'
'instrumentCoverStageCoordindate=-19.85'
'foldMirrorsState=HOME,HOME,HOME,HOME'
'foldMirrorsStageCoordindates=+0.00,+0.00,+0.00,+0.00'
"""
# ts = hed['timestamp']
key = msg.split("=")[0]
value = msg.split("=")[1]
covers, coords = False, False
skip = False
if key.lower() == 'mirrorcovermode' or \
key.lower() == 'instrumentcoverstate':
if value.lower() == "open":
value = 1
else:
value = 0
# Reformat the key to be nicer
if key.lower() == 'mirrorcovermode':
key = "MirrorCover"
else:
key = "InstrumentCover"
# Cheaty flag for later
covers = True
elif key.lower() == "foldmirrorsstagecoordindates":
f1, f2, f3, f4 = value.split(",")
f1 = float(f1)
f2 = float(f2)
f3 = float(f3)
f4 = float(f4)
coords = True
# Logic to suss out the active cube port.
# I tried to do this all in Grafana but it's too much
# of a stupid hassle since I can't really combine metrics.
# There's 4 fold mirrors, so 5 possible ports
# As of 2018 08, mirror 1 is a NIR dichroic so that means
# technically two ports are active or available at least.
inthresh = 100
# Default port status;
# port[0] is the bottom/thru port
# ports[1:] are directly mapped to the fold mirrors
# To match other terminology:
# Ports A, B, C, D == Mirror 1, 2, 3, 4
port0, port1, port2, port3, port4 = 0, 0, 0, 0, 0
if f1 > inthresh:
port0, port1, port2, port3, port4 = 0, 1, 0, 0, 0
elif f2 > inthresh:
port0, port1, port2, port3, port4 = 0, 0, 1, 0, 0
elif f3 > inthresh:
port0, port1, port2, port3, port4 = 0, 0, 0, 1, 0
elif f4 > inthresh:
port0, port1, port2, port3, port4 = 1, 0, 0, 0, 1
else:
port0, port1, port2, port3, port4 = 1, 0, 0, 0, 0
elif key.lower() == "instrumentcoverstagecoordindate":
i1 = float(value)
coords = True
else:
# If it's not one of these, just cheat and pass
# (to skip "foldMirrorsState=..." for example)
skip = True
if skip is False:
meas = ["LightPath"]
if covers is True:
tags = {"Covers": key}
fields = {"State": value}
elif coords is True:
# Figure out which coordinates we're storing
if 'i1' in vars():
fields = {"CoverCoord": i1}
tags = {"Coordinates": "InstCover"}
else:
fields = {"Mirror1": f1}
fields.update({"Mirror2": f2})
fields.update({"Mirror3": f3})
fields.update({"Mirror4": f4})
fields.update({"Port0State": port0})
fields.update({"Port1State": port1})
fields.update({"Port2State": port2})
fields.update({"Port3State": port3})
fields.update({"Port4State": port4})
tags = {"Coordinates": "CubeMirrors"}
# Note: passing ts=None lets python Influx do the timestamp for you.
packet = utils.packetizer.makeInfluxPacket(meas=meas,
ts=None,
tags=tags,
fields=fields)
# print(packet)
# Actually commit the packet. singleCommit opens it,
# writes the packet, and then optionally closes it.
if db is not None:
db.singleCommit(packet, table=db.tablename, close=True)
def parserPDU(_, msg, db=None):
"""
'gwavespdu2.lowell.edu:23 IPC ONLINE!'
'gwavespdu2.lowell.edu:23 OUTLET 2 ON ( UNIT#0 J2 )NIH-TEMP'
"""
# ts = hed['timestamp']
# Cut the hostname down to something more managable
hostname = msg.split(" ")[0]
host = hostname.split(":")[0].split(".")[0]
message = msg.split(" ")[1:]
if message[0] == "IPC":
# I don't give two shits about the IPC status messages.
# If I'm getting messages of course the IPC is online.
pass
elif message[0] == "OUTLET":
# Ok, now we're talking. Snag stuff out of here.
outnumb = message[1]
# Turn the status in to a simple binary (on = 1, off = 0)
if message[2] == "ON":
outstat = 1
else:
outstat = 0
# This is the shorthand label/tag at the very end
label = message[-1].strip("(").strip(")")
# Make the InfluxDB style packet
meas = ["PDUStates"]
# The "tag" is the pdu's hostname since there are multiple
tag = {"Name": host, "OutletNumber": int(outnumb),
"Label": label}
fields = {"State": outstat}
fields.update({"Label": label})
# Make and store the influx packet
# Note: passing ts=None lets python Influx do the timestamp for you
packet = utils.packetizer.makeInfluxPacket(meas=meas,
ts=None,
tags=tag,
fields=fields)
# print(packet)
# Actually commit the packet. singleCommit opens it,
# writes the packet, and then optionally closes it.
if db is not None:
db.singleCommit(packet, table=db.tablename, close=True)
def parserSimpleFloat(hed, msg, db=None):
"""
"""
# print("Parsing a simple float message: %s" % msg)
topic = os.path.basename(hed['destination'])
try:
val = float(msg)
except ValueError as err:
print(str(err))
val = -9999.
# Make the InfluxDB style packet
meas = [topic]
tag = {}
fields = {"value": val}
# Make and store the influx packet
# Note: passing ts=None lets python Influx do the timestamp for you
packet = utils.packetizer.makeInfluxPacket(meas=meas,
ts=None,
tags=tag,
fields=fields)
# print(packet)
# Actually commit the packet. singleCommit opens it,
# writes the packet, and then optionally closes it.
if db is not None:
db.singleCommit(packet, table=db.tablename, close=True)
def parserSimpleBool(hed, msg, db=None):
"""
"""
# topic = os.path.basename(hed['destination'])
# TODO: Finish this one, maybe add an option to store it as an actual
# bool value or convert it to a simple int flagging (0 = True, 1 = False)
raise NotImplementedError