From 874d67782c8c47d70a68aa14a5f3ed8c8f6e8c40 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Sun, 3 Nov 2019 17:30:09 -0500 Subject: [PATCH] WIP: Add automatic rebooting of doors via unifi PoE switches --- common.py | 8 +++++++- config.json | 39 +++++++++++++++++++++++++++++++++------ doorUpdater.py | 20 +++++++++++++++++++- hid/DoorController.py | 3 ++- 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/common.py b/common.py index b6e35c3..fcecef8 100644 --- a/common.py +++ b/common.py @@ -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", diff --git a/config.json b/config.json index 2afbb31..4ddf1e6 100644 --- a/config.json +++ b/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" } } diff --git a/doorUpdater.py b/doorUpdater.py index f3833a4..b0daa2b 100755 --- a/doorUpdater.py +++ b/doorUpdater.py @@ -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'], diff --git a/hid/DoorController.py b/hid/DoorController.py index 4011800..445e636 100644 --- a/hid/DoorController.py +++ b/hid/DoorController.py @@ -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"""