diff --git a/hid/DoorController.py b/hid/DoorController.py index 2b72b89..2f7faf5 100644 --- a/hid/DoorController.py +++ b/hid/DoorController.py @@ -1,3 +1,5 @@ +import csv +from io import StringIO from lxml import etree from lxml.builder import ElementMaker import requests @@ -5,6 +7,8 @@ import requests E_plain = ElementMaker(nsmap={"hid": "http://www.hidglobal.com/VertX"}) E = ElementMaker(namespace="http://www.hidglobal.com/VertX", nsmap={"hid": "http://www.hidglobal.com/VertX"}) +E_corp = ElementMaker(namespace="http://www.hidcorp.com/VertX", #stupid + nsmap={"hid": "http://www.hidcorp.com/VertX"}) ROOT = E_plain.VertXMessage fieldnames = "CardNumber,CardFormat,PinRequired,PinCode,ExtendedAccess,ExpiryDate,Forename,Initial,Surname,Email,Phone,Custom1,Custom2,Schedule1,Schedule2,Schedule3,Schedule4,Schedule5,Schedule6,Schedule7,Schedule8".split(",") @@ -57,7 +61,7 @@ class DoorController(): raise RemoteError(r) return resp_xml - def sendSchedules(self): + def sendSchedules(self, schedules): # clear all people outString = StringIO() writer = csv.DictWriter(outString, fieldnames) @@ -70,14 +74,30 @@ class DoorController(): delXML = ROOT( *[E.Schedules({"action": "DD", "scheduleID": str(ii)}) for ii in range(1, 8)]) - self.doXMLRequest(delXML) + try: + self.doXMLRequest(delXML) + except RemoteError: + # don't care about failure to delete, they probably just didn't exist + pass # load new schedules - with open("schedules.xml", "rb") as f: - self.doXMLRequest(f.read()) + self.doXMLRequest(schedules) + + def getSchedules(self): + # TODO: might be able to do in one request + schedules = self.doXMLRequest(ROOT( + E.Schedules({"action": "LR"}))) + etree.dump(schedules) + + data = self.doXMLRequest(ROOT( + *[E.Schedules({"action": "LR", + "scheduleID": schedule.attrib["scheduleID"]}) + for schedule in schedules[0]])) + return ROOT(E_corp.Schedules({"action": "AD"}, + *[s[0] for s in data])) def sendCardFormat(self, formatName, templateID, facilityCode): - # TODO: add delete formats + # TODO: add ability to delete formats # delete example: el = ROOT(