WIP: Add automatic rebooting of doors via unifi PoE switches
This commit is contained in:
parent
96c34c95f3
commit
874d67782c
@ -6,6 +6,7 @@ import sys
|
||||
from io import StringIO
|
||||
import requests
|
||||
from hid.DoorController import DoorController
|
||||
from pyunifi.controller import Controller as UnifiController
|
||||
|
||||
from passwords import *
|
||||
|
||||
@ -20,9 +21,14 @@ except NameError:
|
||||
|
||||
doors = {doorName: DoorController(doorData['ip'],
|
||||
DOOR_USERNAME, DOOR_PASSWORD,
|
||||
name=doorName, access=doorData['access'])
|
||||
name=doorName, access=doorData['access'],
|
||||
mac=doorData['mac'])
|
||||
for doorName, doorData in config["doors"].items()}
|
||||
|
||||
unifiController = UnifiController(config['unifi-controller']['host'],
|
||||
UNIFI_USERNAME, UNIFI_PASSWORD,
|
||||
ssl_verify = False)
|
||||
|
||||
# mapping of member levels to schedules
|
||||
memberLevels = {"CMS Staff": "7x24",
|
||||
"CMS Weekends Only": "Weekends Only",
|
||||
|
39
config.json
39
config.json
@ -1,10 +1,37 @@
|
||||
{
|
||||
"doors": {
|
||||
"Studio Space": {"ip": "172.18.51.11", "access": "Studio Space"},
|
||||
"Front Door": {"ip": "172.18.51.12", "access": "Front Door"},
|
||||
"Metal Shop": {"ip": "172.18.51.13", "access": "Metal Shop"},
|
||||
"Wood Shop": {"ip": "172.18.51.14", "access": "Wood Shop"},
|
||||
"Wood Shop Rear": {"ip": "172.18.51.15", "access": "Wood Shop"},
|
||||
"Storage Closet": {"ip": "172.18.51.16", "access": "Storage Closet"}
|
||||
"Studio Space": {
|
||||
"access": "Studio Space",
|
||||
"ip": "172.18.51.11",
|
||||
"mac": "00:06:8e:41:8d:16"
|
||||
},
|
||||
"Front Door": {
|
||||
"access": "Front Door",
|
||||
"ip": "172.18.51.12",
|
||||
"mac": "00:06:8e:41:4a:8e"
|
||||
},
|
||||
"Metal Shop": {
|
||||
"access": "Metal Shop",
|
||||
"ip": "172.18.51.13",
|
||||
"mac": "00:06:8e:41:4a:89"
|
||||
},
|
||||
"Wood Shop": {
|
||||
"access": "Wood Shop",
|
||||
"ip": "172.18.51.14",
|
||||
"mac": "00:06:8e:41:50:2e"
|
||||
},
|
||||
"Wood Shop Rear Door": {
|
||||
"access": "Wood Shop",
|
||||
"ip": "172.18.51.15",
|
||||
"mac": "00:06:8e:41:4a:5e"
|
||||
},
|
||||
"Storage Closet": {
|
||||
"access": "Storage Closet",
|
||||
"ip": "172.18.51.16",
|
||||
"mac": "00:06:8e:41:4a:6c"
|
||||
}
|
||||
},
|
||||
"unifi-controller": {
|
||||
"host": "172.18.1.7"
|
||||
}
|
||||
}
|
||||
|
@ -59,11 +59,29 @@ def makeDoor(door, members, hashes):
|
||||
else:
|
||||
print("Door", door.name, "changed, trying to update")
|
||||
hashes[door.name] = doorHash
|
||||
door.doCSVImport(outString)
|
||||
try:
|
||||
door.doCSVImport(outString)
|
||||
except (requests.ReadTimeout, requests.ConnectionError):
|
||||
print(f"Import failed on door {door.name}, trying to power cycle it")
|
||||
cycleDoor(door)
|
||||
# write out hash if we sucessfully updated this door
|
||||
with open('/tmp/doorUpdaterLastHash', 'w') as f:
|
||||
json.dump(hashes, f)
|
||||
|
||||
def cycleDoor(door):
|
||||
if door.mac is None:
|
||||
print(f"Cannot power cycle to door {door.name}, no mac address defined")
|
||||
return
|
||||
|
||||
doorControllerClient = unifiController.get_client(door.mac)
|
||||
print(doorControllerClient)
|
||||
|
||||
unifiController._api_write(
|
||||
'cmd/devmgr',
|
||||
{"cmd": "power-cycle",
|
||||
"mac": doorControllerClient["sw_mac"],
|
||||
"port_idx": doorControllerClient["sw_port"]})
|
||||
|
||||
def main():
|
||||
members = getMembershipworksData(
|
||||
['members', 'staff', 'misc'],
|
||||
|
@ -20,12 +20,13 @@ class RemoteError(Exception):
|
||||
.format(r.status_code, r.reason, r.text))
|
||||
|
||||
class DoorController():
|
||||
def __init__(self, ip, username, password, name="", access=""):
|
||||
def __init__(self, ip, username, password, name="", access="", mac=""):
|
||||
self.ip = ip
|
||||
self.username = username
|
||||
self.password = password
|
||||
self.name = name
|
||||
self.access = access
|
||||
self.mac = mac
|
||||
|
||||
def doImportRequest(self, params=None, files=None):
|
||||
"""Send a request to the door control import script"""
|
||||
|
Reference in New Issue
Block a user