Files
doorbelldingdongringringdoo…/main.py
T

136 lines
3.3 KiB
Python
Raw Normal View History

2021-10-15 15:43:27 -06:00
import os, logging
DEBUG = os.environ.get('DEBUG')
2021-10-15 15:18:27 -06:00
logging.basicConfig(
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
2021-10-15 15:43:27 -06:00
level=logging.DEBUG if DEBUG else logging.INFO)
logging.getLogger('aiohttp').setLevel(logging.DEBUG if DEBUG else logging.WARNING)
2021-10-15 15:18:27 -06:00
2021-12-19 05:29:10 +00:00
import json
2021-10-15 14:31:17 -06:00
import os
import sys
import asyncio
try:
import RPi.GPIO as GPIO
IS_PI = True
except ModuleNotFoundError:
2022-04-01 17:53:37 -06:00
logging.info('RPi.GPIO not found, running without GPIO.')
IS_PI = False
2021-10-15 15:07:28 -06:00
import time
2021-10-16 01:33:57 +01:00
from signal import *
2021-10-15 14:31:17 -06:00
import unifi
2021-10-15 19:47:23 +00:00
import settings
2021-10-15 15:07:28 -06:00
RELAY_ON = False
RELAY_OFF = True
2021-12-19 05:29:10 +00:00
cooldown_time = time.time()
2026-06-13 11:30:45 -06:00
FAIL_COUNT = 0
2021-10-16 01:33:57 +01:00
def set_relay(pin, state):
if IS_PI: GPIO.output(pin, state)
2021-10-16 01:33:57 +01:00
logging.info('Set relay on pin %s to %s', pin, 'ON' if state == RELAY_ON else 'OFF')
def pulse_relay(pin):
set_relay(pin, RELAY_ON)
2022-04-01 17:53:37 -06:00
time.sleep(0.25) # atomic
2021-10-16 01:33:57 +01:00
set_relay(pin, RELAY_OFF)
def ring_bell(camera):
2022-04-01 17:35:52 -06:00
global cooldown_time
2021-10-16 01:33:57 +01:00
2021-12-19 05:29:10 +00:00
if time.time() - cooldown_time < 2:
logging.info('Cooldown skipping.')
return
cooldown_time = time.time()
2021-10-15 15:07:28 -06:00
try:
doorbell = settings.DOORBELLS[camera]
2026-06-13 11:30:45 -06:00
logging.info('Ringing doorbell: %s', doorbell['name'])
2021-10-16 01:33:57 +01:00
pulse_relay(doorbell['gpio'])
2021-10-15 15:07:28 -06:00
except KeyError:
logging.error('Doorbell %s not found!', camera)
2021-10-15 14:31:17 -06:00
async def process_message(msg):
2026-06-13 11:30:45 -06:00
global FAIL_COUNT
if msg == 'CONNECTED':
logging.info('Connected to websocket. Listening for messages...')
FAIL_COUNT = 0
return
if msg.get('type', '') != 'ring':
return
2021-11-29 04:49:17 +00:00
logging.info('Ring message: %s', msg)
2021-11-29 04:49:17 +00:00
ring_bell(msg['camera'])
2021-11-29 04:49:17 +00:00
async def main():
2026-06-13 11:30:45 -06:00
global FAIL_COUNT
2021-12-19 05:29:10 +00:00
while True:
try:
async for msg in unifi.connect():
await process_message(msg)
except BaseException as e:
2026-06-13 11:30:45 -06:00
FAIL_COUNT += 1
logging.error('Problem connecting to Unifi Protect: %s - %s, fail count: %s', e.__class__.__name__, e, FAIL_COUNT)
2022-04-01 17:53:37 -06:00
await asyncio.sleep(5)
2021-10-15 14:31:17 -06:00
2021-10-15 15:18:27 -06:00
def disable_relays_on_exit(*args):
logging.info('Exiting, disabling relays...')
for _, doorbell in settings.DOORBELLS.items():
2021-10-16 01:33:57 +01:00
set_relay(doorbell['gpio'], RELAY_OFF)
2021-10-15 15:18:27 -06:00
logging.info('Goodbye.')
os._exit(0)
def init():
if IS_PI: GPIO.setmode(GPIO.BCM)
if IS_PI: GPIO.setwarnings(False)
2021-10-15 15:07:28 -06:00
for _, doorbell in settings.DOORBELLS.items():
if IS_PI: GPIO.setup(doorbell['gpio'], GPIO.OUT)
2021-11-29 06:53:32 +00:00
set_relay(doorbell['gpio'], RELAY_OFF)
2021-11-29 04:49:17 +00:00
#pulse_relay(doorbell['gpio'])
2021-10-16 01:33:57 +01:00
time.sleep(1)
2021-10-15 15:18:27 -06:00
logging.info('GPIO initialized')
for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
signal(sig, disable_relays_on_exit)
logging.info('Signals initialized')
2021-10-15 15:07:28 -06:00
2026-06-13 11:30:45 -06:00
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')
2021-10-15 14:31:17 -06:00
if __name__ == '__main__':
2021-10-16 01:41:22 +01:00
logging.info('')
logging.info('======================================')
logging.info('Boot up...')
2021-10-15 15:18:27 -06:00
init()
2021-10-15 14:31:17 -06:00
loop = asyncio.get_event_loop()
2026-06-13 11:30:45 -06:00
if not DEBUG:
a = loop.create_task(watchdog())
loop.run_until_complete(main())