memberPlumbing/doorUtil.py

62 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import requests
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
2018-08-16 12:52:25 -04:00
delXML = E_plain.VertXMessage(
*[E.Schedules({"action": "DD", "scheduleID": str(ii)})
for ii in range(1, 8)])
doXMLRequest(target_ip, 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"/>
2018-08-16 12:52:25 -04:00
el = E_plain.VertXMessage(
E.CardFormats({"action": "AD"},
E.CardFormat({"formatName": formatName,
"templateID": str(templateID)}
E.FixedField({"value": str(facilityCode)}))))
return doXMLRequest(targetIP, el)
def lockOrUnlockDoor(targetIP, lock=True):
2018-08-16 12:52:25 -04:00
el = E_plain.VertXMessage(
E.Doors({"action": "CM",
"command": "lockDoor" if lock else "unlockDoor"}))
return doXMLRequest(targetIP, 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"]))