event: move message formatting into a function, use defaultdict

This commit is contained in:
Adam Goldsmith 2018-05-14 22:53:26 -04:00
parent fc29f1a2e6
commit 0fb1b7481b

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from collections import defaultdict
from xml.etree import ElementTree as ET from xml.etree import ElementTree as ET
import re import re
import requests import requests
@ -45,9 +46,17 @@ eventStrings = {
4045: 'Battery Failure', 4045: 'Battery Failure',
} }
#<?xml version="1.0" encoding="UTF-8"?> <VertXMessage> <hid:EventMessages action="LR" recordCount="100" historyRecordMarker="463" historyTimestamp="1525824431"/> </VertXMessage> def formatMessage(event):
att = defaultdict(str, event.attrib)
#<?xml version="1.0" encoding="UTF-8"?><VertXMessage xmlns:hid="http://www.hidcorp.com/VertX"><hid:EventMessages action="RL" historyRecordMarker="463" historyTimestamp="1525824431" currentRecordMarker="463" currentTimestamp="1525824431" /></VertXMessage> eventType = int(att["eventType"])
return att["timestamp"], eventStrings[eventType].format(
'ios-' + att['ioState'],
'status-' + att['commandStatus'],
att['forename'] + " " + att['surname'],
att['rawCardNumber'],
att['oldTime'],
att['newTime'],
" IN" if att['readerAddress'] == '0' else " OUT")
# get parameters for messages to get? # get parameters for messages to get?
# honestly not really sure why this is required, their API is confusing # honestly not really sure why this is required, their API is confusing
@ -66,13 +75,4 @@ eventsXMLOut = doXMLRequest(TARGET_IP, ET.tostring(eventsXMLIn))
for event in eventsXMLOut[0]: for event in eventsXMLOut[0]:
ET.dump(event) ET.dump(event)
att = event.attrib print(formatMessage(event))
eventType = int(att["eventType"])
print(att["timestamp"], eventStrings[eventType].format(
'ios-' + att.get('ioState', ''),
'status-' + att.get('commandStatus', ''),
att.get('forename', '') + " " + att.get('surname', ''),
att.get('rawCardNumber', ''),
att.get('oldTime', ''),
att.get('newTime', ''),
"IN" if att.get("readerAddress", '') == '0' else "OUT"))