This repository has been archived on 2024-02-23. You can view files and clone it, but cannot push or open issues or pull requests.
memberPlumbing/doorUpdater.py

85 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import sys
2018-05-07 18:02:04 -04:00
import requests
import csv
from collections import OrderedDict
2018-05-07 18:02:04 -04:00
from io import StringIO
from hashlib import md5
import os
2018-05-07 18:02:04 -04:00
from common import *
from hid.DoorController import fieldnames
2018-05-07 18:02:04 -04:00
def makeMember(member, doorAuth):
2018-05-07 18:25:14 -04:00
"""Create an output CSV row for the member"""
2018-05-07 18:02:04 -04:00
if member["Access Card Number"] == "":
#print(member["First Name"], member["Last Name"], " has no card number, ignoring")
return
out = {"Forename": member["First Name"],
2018-05-07 18:25:14 -04:00
"Surname": member["Last Name"],
"Initial": "",
"CardNumber": member["Access Card Number"],
2018-05-08 20:35:33 -04:00
"CardFormat": "A901146A-" + member["Access Card Facility Code"],
2018-05-07 18:25:14 -04:00
"PinRequired": "0",
"ExtendedAccess": "0",
"ExpiryDate": "",
"Email": member["Email"],
"Phone": member["Phone"]}
2018-05-07 18:02:04 -04:00
2018-05-08 20:27:32 -04:00
if member[doorAuth] == "Y" \
and not member["Account on Hold"] == "Account on Hold":
levels = OrderedDict(sorted([(k, v) for k, v in memberLevels.items() if member[k] == k]))
out["Custom1"] = "|".join(levels.keys()).replace("&", "and")
for index, schedule in enumerate(levels.values(), 1):
#TODO: error if people have more than 8?
out["Schedule" + str(index)] = schedule
2018-05-07 18:02:04 -04:00
return out
2018-08-16 14:01:42 -04:00
def makeDoor(door, members, hashes):
2018-05-07 18:25:14 -04:00
"""Create a CSV for the given door"""
2018-05-07 18:02:04 -04:00
outString = StringIO()
writer = csv.DictWriter(outString, fieldnames)
writer.writeheader()
for member in members:
2018-08-16 14:01:42 -04:00
member = makeMember(member, "Access " + door.access + "?")
2018-05-07 18:02:04 -04:00
if member is not None:
writer.writerow(member)
2018-06-12 01:42:17 -04:00
import datetime as DT
timestamp = DT.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2018-08-16 14:01:42 -04:00
with open("/tmp/" + door.name + timestamp + ".csv", "w") as f:
2018-06-12 01:42:17 -04:00
f.write(outString.getvalue())
2018-05-27 17:17:36 -04:00
outString.seek(0)
doorHash = md5(bytes(outString.getvalue(), 'utf8')).hexdigest()
2018-08-16 14:01:42 -04:00
if doorHash == hashes.get(door.name):
print("Door", door.name, "not changed, not updating")
else:
2018-08-16 14:01:42 -04:00
print("Door", door.name, "changed, trying to update")
hashes[door.name] = doorHash
door.doCSVImport(outString)
# write out hash if we sucessfully updated this door
with open('/tmp/doorUpdaterLastHash', 'w') as f:
json.dump(hashes, f)
2018-05-07 18:02:04 -04:00
def main():
members = getMembershipworksData(
['members', 'staff', 'misc'],
"lvl,xws,xms,xsc,xas,xfd,xac,phn,eml,lbl,xcf,nam")
members.sort(key=lambda x: x['Last Name'])
2018-05-07 18:02:04 -04:00
if os.path.exists('/tmp/doorUpdaterLastHash'):
with open('/tmp/doorUpdaterLastHash', 'r') as f:
hashes = json.load(f)
else:
hashes = {}
2018-08-16 14:01:42 -04:00
for door in doors.values():
print(door.name, door.ip)
makeDoor(door, members, hashes)
2018-05-07 18:02:04 -04:00
if __name__ == '__main__':
main()