Idk what this is

This commit is contained in:
Tanner
2026-06-13 11:30:45 -06:00
parent a0e0651703
commit 8a8997750e
2 changed files with 56 additions and 4 deletions
+33 -2
View File
@@ -28,6 +28,8 @@ RELAY_OFF = True
cooldown_time = time.time()
FAIL_COUNT = 0
def set_relay(pin, state):
if IS_PI: GPIO.output(pin, state)
logging.info('Set relay on pin %s to %s', pin, 'ON' if state == RELAY_ON else 'OFF')
@@ -48,12 +50,20 @@ def ring_bell(camera):
try:
doorbell = settings.DOORBELLS[camera]
logging.info('Ringing doorbell: %s', doorbell['name'])
pulse_relay(doorbell['gpio'])
except KeyError:
logging.error('Doorbell %s not found!', camera)
async def process_message(msg):
global FAIL_COUNT
if msg == 'CONNECTED':
logging.info('Connected to websocket. Listening for messages...')
FAIL_COUNT = 0
return
if msg.get('type', '') != 'ring':
return
@@ -62,12 +72,15 @@ async def process_message(msg):
ring_bell(msg['camera'])
async def main():
global FAIL_COUNT
while True:
try:
async for msg in unifi.connect():
await process_message(msg)
except BaseException as e:
logging.exception('Error connecting to Unifi Protect: %s. Trying again...', str(e))
FAIL_COUNT += 1
logging.error('Problem connecting to Unifi Protect: %s - %s, fail count: %s', e.__class__.__name__, e, FAIL_COUNT)
await asyncio.sleep(5)
@@ -93,6 +106,23 @@ def init():
signal(sig, disable_relays_on_exit)
logging.info('Signals initialized')
async def watchdog():
global FAIL_COUNT
logging.info('Starting watchdog...')
while True:
await asyncio.sleep(1)
if FAIL_COUNT >= 10:
logging.info('Too many failures, starving watchdog...')
continue
with open('/dev/watchdog', 'w') as wdt:
wdt.write('1')
if __name__ == '__main__':
logging.info('')
logging.info('======================================')
@@ -100,5 +130,6 @@ if __name__ == '__main__':
init()
loop = asyncio.get_event_loop()
if not DEBUG:
a = loop.create_task(watchdog())
loop.run_until_complete(main())
loop.close()