events: Make it work on multiple doors

This commit is contained in:
Adam Goldsmith 2018-06-01 13:56:46 -04:00
parent d62f0c16ab
commit b1cf735bf0

View File

@ -7,11 +7,9 @@ import requests
from common import * from common import *
TARGET_IP = '172.18.51.11' def getStrings(targetIP):
def getStrings():
"""Parses out the message strings from source.""" """Parses out the message strings from source."""
r = requests.get('https://' + TARGET_IP + '/html/en_EN/en_EN.js', r = requests.get('https://' + targetIP + '/html/en_EN/en_EN.js',
auth=requests.auth.HTTPDigestAuth(DOOR_USERNAME, DOOR_PASSWORD), auth=requests.auth.HTTPDigestAuth(DOOR_USERNAME, DOOR_PASSWORD),
verify=False) verify=False)
regex = re.compile(r'([0-9]+)="([^"]*)') regex = re.compile(r'([0-9]+)="([^"]*)')
@ -59,24 +57,28 @@ def formatMessage(event):
att['newTime'], att['newTime'],
" IN" if att['readerAddress'] == '0' else " OUT") " IN" if att['readerAddress'] == '0' else " OUT")
def main(): def getMessages(doorName, doorIP):
# 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
parXMLIn = ET.Element("VertXMessage") parXMLIn = ET.Element("VertXMessage")
ET.SubElement(parXMLIn, "hid:EventMessages", attrib={"action": "LR"}) ET.SubElement(parXMLIn, "hid:EventMessages", attrib={"action": "LR"})
parXMLOut = doXMLRequest(TARGET_IP, ET.tostring(parXMLIn)) parXMLOut = doXMLRequest(doorIP, ET.tostring(parXMLIn))
ET.dump(parXMLOut)
root = None if os.path.exists("logs/" + doorName + ".xml"):
if os.path.exists("log.xml"):
# read last log # read last log
tree = ET.ElementTree(None, "log.xml") tree = ET.ElementTree(None, "logs/" + doorName + ".xml")
root = tree.getroot() root = tree.getroot()
recordCount = int(parXMLOut[0].attrib["historyRecordMarker"]) - \ recordCount = int(parXMLOut[0].attrib["historyRecordMarker"]) - \
int(root[0][0].attrib["recordMarker"]) int(root[0][0].attrib["recordMarker"])
else: else:
# first run for this door # first run for this door
root = None
recordCount = 1000 recordCount = 1000
if recordCount == 0:
print("No records to get!")
return
print("Getting", recordCount, "records") print("Getting", recordCount, "records")
# get the actual messages # get the actual messages
eventsXMLIn = ET.Element("VertXMessage") eventsXMLIn = ET.Element("VertXMessage")
@ -85,7 +87,7 @@ def main():
"recordCount": str(recordCount), "recordCount": str(recordCount),
"historyRecordMarker": parXMLOut[0].attrib["historyRecordMarker"], "historyRecordMarker": parXMLOut[0].attrib["historyRecordMarker"],
"historyTimestamp": parXMLOut[0].attrib["historyTimestamp"]}) "historyTimestamp": parXMLOut[0].attrib["historyTimestamp"]})
eventsXMLOut = doXMLRequest(TARGET_IP, ET.tostring(eventsXMLIn)) eventsXMLOut = doXMLRequest(doorIP, ET.tostring(eventsXMLIn))
#TODO: handle modeRecords=true #TODO: handle modeRecords=true
for index, event in enumerate(eventsXMLOut[0]): for index, event in enumerate(eventsXMLOut[0]):
@ -97,7 +99,11 @@ def main():
else: else:
for event in reversed(eventsXMLOut[0]): for event in reversed(eventsXMLOut[0]):
root[0].insert(0, event) root[0].insert(0, event)
tree.write("log.xml") tree.write("logs/" + doorName + ".xml")
def main():
for doorName, doorData in config["doors"].items():
getMessages(doorName, doorData["ip"])
if __name__ == '__main__': if __name__ == '__main__':
main() main()