2019-11-04 01:25:17 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-02-06 17:48:09 -05:00
|
|
|
from common import doors, membershipworks, memberLevels, doorSpecificSchedules
|
2019-11-08 15:51:44 -05:00
|
|
|
from hid.Credential import Credential
|
2019-11-22 16:39:31 -05:00
|
|
|
from hid.DoorController import ROOT, E
|
2019-11-04 01:25:17 -05:00
|
|
|
|
|
|
|
class Member():
|
2019-12-18 18:47:36 -05:00
|
|
|
def __init__(self, forename="", surname="", membershipWorksID="",
|
2019-11-04 01:25:17 -05:00
|
|
|
middleName="", email="", phone="",
|
|
|
|
cardholderID=None, doorAccess=[], onHold=False,
|
2019-12-18 18:47:36 -05:00
|
|
|
credentials=set(), levels=[], extraLevels=[], schedules=[]):
|
2019-11-04 01:25:17 -05:00
|
|
|
self.forename = forename
|
|
|
|
self.surname = surname
|
|
|
|
self.membershipWorksID = membershipWorksID
|
|
|
|
self.middleName = middleName
|
|
|
|
self.email = email
|
|
|
|
self.phone = phone
|
|
|
|
self.cardholderID = cardholderID
|
|
|
|
self.doorAccess = doorAccess
|
|
|
|
self.onHold = onHold
|
|
|
|
self.credentials = credentials
|
|
|
|
self.levels = levels
|
|
|
|
self.schedules = schedules
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"""Name: {self.forename} | {self.middleName} | {self.surname}
|
|
|
|
MembershipWorks ID: {self.membershipWorksID}
|
|
|
|
Email: {self.email}
|
|
|
|
Phone: {self.phone}
|
|
|
|
Cardholder ID: {self.cardholderID}
|
|
|
|
doorAccess: {self.doorAccess}
|
|
|
|
On Hold? {self.onHold}
|
|
|
|
Credentials: {self.credentials}
|
|
|
|
Levels: {self.levels}
|
|
|
|
Schedules: {self.schedules}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-12-18 18:47:36 -05:00
|
|
|
class MembershipworksMember(Member):
|
|
|
|
def __init__(self, data):
|
|
|
|
super().__init__(data["First Name"],
|
|
|
|
data["Last Name"],
|
|
|
|
membershipWorksID=data["Account ID"],
|
|
|
|
email=data["Email"],
|
|
|
|
phone=data["Phone"],
|
|
|
|
onHold=data["Account on Hold"] != "")
|
|
|
|
|
2019-11-04 01:25:17 -05:00
|
|
|
if data["Access Card Number"] != "":
|
2019-12-18 18:47:36 -05:00
|
|
|
self.credentials = set([Credential(
|
2019-11-08 15:51:44 -05:00
|
|
|
code=(data["Access Card Facility Code"],
|
|
|
|
data["Access Card Number"]))])
|
2019-11-04 01:25:17 -05:00
|
|
|
else:
|
2019-12-18 18:47:36 -05:00
|
|
|
self.credentials = set()
|
2019-11-04 01:25:17 -05:00
|
|
|
|
|
|
|
levels = {k: v for k, v in memberLevels.items() if data[k] == k}
|
2019-12-18 18:47:36 -05:00
|
|
|
self.levels = list(levels.keys())
|
|
|
|
self.schedules = list(levels.values())
|
|
|
|
|
|
|
|
self.extraLevels = {
|
|
|
|
schedule: sum((doors for prop, doors in props.items()
|
|
|
|
if data[prop] == "Y"),
|
|
|
|
[])
|
|
|
|
for schedule, props in doorSpecificSchedules.items()
|
|
|
|
}
|
|
|
|
|
|
|
|
self.doorAccess = [
|
|
|
|
door for door, doorData in doors.items()
|
|
|
|
if data["Access " + doorData.access + "?"] == "Y"
|
|
|
|
]
|
|
|
|
|
|
|
|
def to_DoorMember(self, door):
|
|
|
|
doorLevels = [k for k, v in self.extraLevels.items() if door.name in v]
|
|
|
|
|
|
|
|
if door.name not in self.doorAccess or self.onHold:
|
|
|
|
schedules = []
|
|
|
|
else:
|
|
|
|
schedules = self.schedules + doorLevels
|
|
|
|
|
|
|
|
dm = DoorMember(door, **self.__dict__) # this is probably terrible
|
|
|
|
dm.levels = self.levels + doorLevels
|
|
|
|
dm.schedules = schedules
|
|
|
|
|
|
|
|
return dm
|
|
|
|
|
|
|
|
|
|
|
|
class DoorMember(Member):
|
|
|
|
def __init__(self, door, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.door = door
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_cardholder(cls, data, door):
|
|
|
|
ch = cls(door=door,
|
|
|
|
forename=data.get('forename', ""),
|
|
|
|
surname=data.get('surname', ""),
|
|
|
|
membershipWorksID=data.attrib.get('custom2', ""),
|
|
|
|
middleName=data.attrib.get('middleName', ""),
|
|
|
|
email=data.attrib.get('email', ""),
|
|
|
|
phone=data.attrib.get('phone', ""),
|
|
|
|
cardholderID=data.attrib['cardholderID'])
|
|
|
|
|
|
|
|
ch.credentials = set(
|
|
|
|
Credential(hex=(c.attrib['rawCardNumber']))
|
|
|
|
for c in data.findall('{*}Credential'))
|
|
|
|
|
|
|
|
ch.levels = data.attrib.get('custom1', "").split('|')
|
|
|
|
ch.schedules = [r.attrib['scheduleName']
|
|
|
|
for r in data.findall('{*}Role')]
|
|
|
|
|
|
|
|
return ch
|
2019-11-04 01:25:17 -05:00
|
|
|
|
|
|
|
def attribs(self):
|
2019-11-07 15:22:50 -05:00
|
|
|
return {"forename": self.forename,
|
|
|
|
"surname": self.surname,
|
|
|
|
"middleName": self.middleName,
|
|
|
|
"email": self.email,
|
|
|
|
"phone": self.phone,
|
|
|
|
"custom1": "|".join(self.levels).replace("&", "and"),
|
|
|
|
"custom2": self.membershipWorksID}
|
|
|
|
|
2019-12-18 18:47:36 -05:00
|
|
|
def make_schedules(self, schedulesMap):
|
2019-11-07 15:22:50 -05:00
|
|
|
roles = [
|
|
|
|
E.Role({"roleID": self.cardholderID,
|
|
|
|
"scheduleID": schedulesMap[schedule],
|
|
|
|
"resourceID": "0"})
|
2019-12-18 18:47:36 -05:00
|
|
|
for schedule in self.schedules]
|
2019-11-04 01:25:17 -05:00
|
|
|
|
2019-11-07 15:22:50 -05:00
|
|
|
return E.RoleSet({"action": "UD", "roleSetID": self.cardholderID},
|
|
|
|
E.Roles(*roles))
|
2019-11-04 01:25:17 -05:00
|
|
|
|
2019-11-08 15:51:44 -05:00
|
|
|
def make_credentials(self, newCredentials, cardFormats):
|
|
|
|
out = [
|
2019-11-07 15:22:50 -05:00
|
|
|
E.Credential(
|
2019-11-08 15:51:44 -05:00
|
|
|
{"formatName": str(credential.code[0]),
|
|
|
|
"cardNumber": str(credential.code[1]),
|
|
|
|
"formatID": cardFormats[str(credential.code[0])],
|
2019-11-04 01:25:17 -05:00
|
|
|
"isCard": "true",
|
|
|
|
"cardholderID": self.cardholderID})
|
2019-11-08 15:51:44 -05:00
|
|
|
for credential in newCredentials]
|
2019-11-04 01:25:17 -05:00
|
|
|
|
2019-11-08 15:51:44 -05:00
|
|
|
return E.Credentials({"action": "AD"}, *out)
|
2019-11-04 01:25:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
def update_door(door, members):
|
2019-11-07 15:22:50 -05:00
|
|
|
cardFormats = door.get_cardFormats()
|
|
|
|
cardholders = {member.membershipWorksID: member
|
2019-12-18 18:47:36 -05:00
|
|
|
for member in [DoorMember.from_cardholder(ch, door)
|
2019-11-07 15:22:50 -05:00
|
|
|
for ch in door.get_cardholders()]}
|
|
|
|
schedulesMap = door.get_scheduleMap()
|
2019-11-08 15:51:44 -05:00
|
|
|
allCredentials = set(Credential(hex=c.attrib['rawCardNumber'])
|
|
|
|
for c in door.get_credentials())
|
2019-11-04 01:25:17 -05:00
|
|
|
|
2019-11-08 16:07:26 -05:00
|
|
|
# TODO: can I combine requests?
|
2019-12-18 18:47:36 -05:00
|
|
|
for membershipworksMember in members:
|
|
|
|
member = membershipworksMember.to_DoorMember(door)
|
2019-11-08 16:07:26 -05:00
|
|
|
# cardholder exists, compare contents
|
|
|
|
if member.membershipWorksID in cardholders:
|
2019-11-04 01:25:17 -05:00
|
|
|
ch = cardholders.pop(member.membershipWorksID)
|
|
|
|
member.cardholderID = ch.cardholderID
|
2019-11-07 15:22:50 -05:00
|
|
|
if member.attribs() != ch.attribs(): # update cardholder attributes
|
2019-11-04 01:25:17 -05:00
|
|
|
print(f"- Updating profile for {member.forename} {member.surname}")
|
|
|
|
print(f" - Old: {ch.attribs()}")
|
|
|
|
print(f" - New: {member.attribs()}")
|
|
|
|
door.doXMLRequest(ROOT(
|
|
|
|
E.Cardholders(
|
|
|
|
{"action": "UD", "cardholderID": member.cardholderID},
|
|
|
|
E.CardHolder(member.attribs()))))
|
2019-11-07 15:22:50 -05:00
|
|
|
|
2019-11-04 01:25:17 -05:00
|
|
|
if member.credentials != ch.credentials:
|
|
|
|
print(f"- Updating card for {member.forename} {member.surname}")
|
|
|
|
print(f" - {ch.credentials} -> {member.credentials}")
|
2019-11-08 15:51:44 -05:00
|
|
|
|
|
|
|
oldCards = ch.credentials
|
|
|
|
newCards = member.credentials
|
|
|
|
|
2019-12-18 18:47:36 -05:00
|
|
|
allNewCards = set(
|
|
|
|
card for m in members if m != membershipworksMember
|
|
|
|
for card in m.credentials)
|
2019-11-08 15:51:44 -05:00
|
|
|
|
|
|
|
# cards removed, and won't be reassigned to someone else
|
|
|
|
for card in (oldCards - newCards) - allNewCards:
|
|
|
|
door.doXMLRequest(ROOT(E.Credentials(
|
|
|
|
{"action": "UD",
|
|
|
|
"rawCardNumber": card.hex,
|
|
|
|
"isCard": "true"},
|
|
|
|
E.Credential({"cardholderID": ""}))))
|
|
|
|
|
|
|
|
if newCards - oldCards: # cards added
|
|
|
|
for card in newCards & allNewCards: # new card exists in another member
|
2019-12-18 18:47:36 -05:00
|
|
|
print([m for m in members
|
|
|
|
for card in m.credentials
|
|
|
|
if card in newCards])
|
2019-11-08 15:51:44 -05:00
|
|
|
raise Exception(f"Duplicate Card in input data! {card}")
|
|
|
|
|
|
|
|
# card existed in door, and needs to be reassigned
|
|
|
|
for card in newCards & allCredentials:
|
|
|
|
door.doXMLRequest(ROOT(E.Credentials(
|
|
|
|
{"action": "UD",
|
|
|
|
"rawCardNumber": card.hex,
|
|
|
|
"isCard": "true"},
|
|
|
|
E.Credential({"cardholderID": member.cardholderID}))))
|
|
|
|
|
|
|
|
# cards that never existed, and need to be created
|
|
|
|
if newCards - allCredentials:
|
|
|
|
door.doXMLRequest(ROOT(member.make_credentials(
|
|
|
|
newCards - allCredentials, cardFormats)))
|
|
|
|
|
2019-12-18 18:47:36 -05:00
|
|
|
if member.schedules != ch.schedules:
|
2019-11-04 01:25:17 -05:00
|
|
|
print("- Updating schedule for" +
|
|
|
|
f" {member.forename} {member.surname}:" +
|
2019-12-18 18:47:36 -05:00
|
|
|
f" {ch.schedules} -> {member.schedules}")
|
2019-11-08 16:07:26 -05:00
|
|
|
door.doXMLRequest(ROOT(
|
2019-12-18 18:47:36 -05:00
|
|
|
member.make_schedules(schedulesMap)))
|
2019-11-07 15:22:50 -05:00
|
|
|
else: # cardholder did not exist, so add them
|
2019-11-04 01:25:17 -05:00
|
|
|
print(f"- Adding Member:")
|
|
|
|
print(member)
|
|
|
|
resp = door.doXMLRequest(ROOT(
|
2019-11-08 16:07:26 -05:00
|
|
|
E.Cardholders(
|
|
|
|
{"action": "AD"},
|
2019-11-04 01:25:17 -05:00
|
|
|
E.Cardholder(member.attribs()))))
|
|
|
|
member.cardholderID = resp.find('{*}Cardholders/{*}Cardholder') \
|
|
|
|
.attrib["cardholderID"]
|
2019-11-08 16:07:26 -05:00
|
|
|
door.doXMLRequest(ROOT(
|
|
|
|
member.make_credentials(member.credentials, cardFormats),
|
2019-12-18 18:47:36 -05:00
|
|
|
member.make_schedules(schedulesMap)))
|
2019-11-04 01:25:17 -05:00
|
|
|
|
|
|
|
# TODO: delete cardholders that are no longer members?
|
|
|
|
|
2019-11-08 16:07:26 -05:00
|
|
|
|
2019-11-04 01:25:17 -05:00
|
|
|
def main():
|
2020-02-06 17:48:09 -05:00
|
|
|
memberData = membershipworks.get_members(
|
2019-11-04 01:25:17 -05:00
|
|
|
['members', 'staff', 'misc'],
|
2019-12-18 18:47:36 -05:00
|
|
|
"_id,nam,phn,eml,lvl,lbl,xws,xms,xsc,xas,xfd,xac,xcf,xeh,xse")
|
2019-11-04 01:25:17 -05:00
|
|
|
|
2019-12-18 18:47:36 -05:00
|
|
|
members = [MembershipworksMember(m) for m in memberData]
|
2019-11-04 01:25:17 -05:00
|
|
|
|
|
|
|
for door in doors.values():
|
|
|
|
print(door.name, door.ip)
|
|
|
|
update_door(door, members)
|
|
|
|
|
2019-11-08 16:07:26 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|