From d62f0c16ab7bab115add1e62dff964f0101ea3c9 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Fri, 1 Jun 2018 13:51:50 -0400 Subject: [PATCH] events: Move into main() method --- events.py | 72 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/events.py b/events.py index 5548f21..6a56b0f 100755 --- a/events.py +++ b/events.py @@ -59,41 +59,45 @@ def formatMessage(event): 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 -parXMLIn = ET.Element("VertXMessage") -ET.SubElement(parXMLIn, "hid:EventMessages", attrib={"action": "LR"}) -parXMLOut = doXMLRequest(TARGET_IP, ET.tostring(parXMLIn)) +def main(): + # get parameters for messages to get? + # honestly not really sure why this is required, their API is confusing + parXMLIn = ET.Element("VertXMessage") + ET.SubElement(parXMLIn, "hid:EventMessages", attrib={"action": "LR"}) + parXMLOut = doXMLRequest(TARGET_IP, ET.tostring(parXMLIn)) -root = None -if os.path.exists("log.xml"): - # read last log - tree = ET.ElementTree(None, "log.xml") - root = tree.getroot() - recordCount = int(parXMLOut[0].attrib["historyRecordMarker"]) - \ - int(root[0][0].attrib["recordMarker"]) -else: - # first run for this door - recordCount = 1000 + root = None + if os.path.exists("log.xml"): + # read last log + tree = ET.ElementTree(None, "log.xml") + root = tree.getroot() + recordCount = int(parXMLOut[0].attrib["historyRecordMarker"]) - \ + int(root[0][0].attrib["recordMarker"]) + else: + # first run for this door + recordCount = 1000 -print("Getting", recordCount, "records") -# get the actual messages -eventsXMLIn = ET.Element("VertXMessage") -ET.SubElement(eventsXMLIn, "hid:EventMessages", - attrib={"action": "LR", - "recordCount": str(recordCount), - "historyRecordMarker": parXMLOut[0].attrib["historyRecordMarker"], - "historyTimestamp": parXMLOut[0].attrib["historyTimestamp"]}) -eventsXMLOut = doXMLRequest(TARGET_IP, ET.tostring(eventsXMLIn)) -#TODO: handle modeRecords=true + print("Getting", recordCount, "records") + # get the actual messages + eventsXMLIn = ET.Element("VertXMessage") + ET.SubElement(eventsXMLIn, "hid:EventMessages", + attrib={"action": "LR", + "recordCount": str(recordCount), + "historyRecordMarker": parXMLOut[0].attrib["historyRecordMarker"], + "historyTimestamp": parXMLOut[0].attrib["historyTimestamp"]}) + eventsXMLOut = doXMLRequest(TARGET_IP, ET.tostring(eventsXMLIn)) + #TODO: handle modeRecords=true -for index, event in enumerate(eventsXMLOut[0]): - event.attrib["recordMarker"] = str(int(parXMLOut[0].attrib["historyRecordMarker"]) - index) -# print(formatMessage(event)) + for index, event in enumerate(eventsXMLOut[0]): + event.attrib["recordMarker"] = str(int(parXMLOut[0].attrib["historyRecordMarker"]) - index) + # print(formatMessage(event)) -if root is None: - tree = ET.ElementTree(eventsXMLOut) -else: - for event in reversed(eventsXMLOut[0]): - root[0].insert(0, event) -tree.write("log.xml") + if root is None: + tree = ET.ElementTree(eventsXMLOut) + else: + for event in reversed(eventsXMLOut[0]): + root[0].insert(0, event) + tree.write("log.xml") + +if __name__ == '__main__': + main()