diff --git a/doorUtil.py b/doorUtil.py new file mode 100644 index 0000000..08fba9f --- /dev/null +++ b/doorUtil.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +import requests +from xml.etree import ElementTree as ET +import csv +from io import StringIO + +from common import * + +def hexToCode(hex): + b = bin(int(hex, 16))[2:] + facility = int(b[0:8], 2) + code = int(b[9:24], 2) + return((facility, code)) + +def codeToHex(facility, code): + return "{:08X}".format(int(bin(facility)[2:] + "0" + bin(code)[2:] + "1", 2)) + +# hexToCode("01E29DA1") <-> codeToHex(241, 20176) + +def sendSchedule(target_ip): + # clear all people + outString = StringIO() + writer = csv.DictWriter(outString, fieldnames) + writer.writeheader() + writer.writerow({}) + outString.seek(0) + doCSVImport(target_ip, outString) + + # clear all schedules + delXML = ET.Element("VertXMessage") + for ii in range(1, 8): + ET.SubElement(delXML, "hid:Schedules", + attrib={"action": "DD", "scheduleID": str(ii)}) + doXMLRequest(target_ip, ET.tostring(delXML)) + + # load new schedules + with open("schedules.xml", "rb") as f: + doXMLRequest(target_ip, f.read()) + +def sendCardFormat(targetIP, formatName, templateID, facilityCode): + # TODO: add delete formats + # delete example: + + el = ET.Element("VertXMessage") + formats = ET.SubElement(el, "hid:CardFormats", attrib={"action": "AD"}) + fmt = ET.SubElement(formats, "hid:CardFormat", + attrib={"formatName": formatName, + "templateID": str(templateID)}) + ET.SubElement(fmt, "hid:FixedField", + attrib={"value": str(facilityCode)}) + return doXMLRequest(targetIP, ET.tostring(el)) + +def forEachDoor(fxn): + for doorName, doorData in config["doors"].items(): + print(doorName) + fxn(doorName, doorData) + +#forEachDoor(lambda name, data: sendCardFormat(data["ip"], "A901146A-244", 1, 244)) +#forEachDoor(lambda name, data: sendSchedule(data["ip"])) diff --git a/sendSchedule.py b/sendSchedule.py deleted file mode 100755 index e6fd24b..0000000 --- a/sendSchedule.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python3 -import requests -from xml.etree import ElementTree as ET -import csv -from io import StringIO - -from common import * - -TARGET_IP = '172.18.51.15' - -# clear all people -outString = StringIO() -writer = csv.DictWriter(outString, fieldnames) -writer.writeheader() -writer.writerow({}) -outString.seek(0) -doCSVImport(TARGET_IP, outString) - -# clear all schedules -delXML = ET.Element("VertXMessage") -for ii in range(1, 8): - ET.SubElement(delXML, "hid:Schedules", - attrib={"action": "DD", "scheduleID": str(ii)}) -doXMLRequest(TARGET_IP, ET.tostring(delXML)) - -# load new schedules -with open("schedules.xml", "rb") as f: - doXMLRequest(TARGET_IP, f.read())