From 0fb1b7481b58a62a24ca704c5e70903adabea971 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 14 May 2018 22:53:26 -0400 Subject: [PATCH] event: move message formatting into a function, use defaultdict --- events.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/events.py b/events.py index 1179b53..d64f9e2 100755 --- a/events.py +++ b/events.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +from collections import defaultdict from xml.etree import ElementTree as ET import re import requests @@ -45,9 +46,17 @@ eventStrings = { 4045: 'Battery Failure', } -# - -# +def formatMessage(event): + att = defaultdict(str, event.attrib) + 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? # 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]: ET.dump(event) - att = event.attrib - 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")) + print(formatMessage(event))