memberPlumbing/doorUtil.py

67 lines
2.2 KiB
Python

#!/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: <hid:CardFormats action="DD" formatID="7-1-244"/>
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 lockOrUnlockDoor(targetIP, lock=True):
el = ET.Element("VertXMessage")
ET.SubElement(el, "hid:Doors",
attrib={"action": "CM",
"command": "lockDoor" if lock else "unlockDoor"})
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"]))