Misc
This commit is contained in:
parent
8a05f1aacb
commit
14051edfc5
91
main.py
91
main.py
|
@ -1,10 +1,12 @@
|
|||
import os, logging
|
||||
DEBUG = os.environ.get('DEBUG')
|
||||
NO_WATCHDOG = os.environ.get('NO_WATCHDOG')
|
||||
logging.basicConfig(
|
||||
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
|
||||
level=logging.DEBUG if DEBUG else logging.INFO)
|
||||
logging.getLogger('aiohttp').setLevel(logging.DEBUG if DEBUG else logging.WARNING)
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import asyncio
|
||||
|
@ -24,21 +26,30 @@ RELAY_OFF = True
|
|||
|
||||
allow_watchdog = False
|
||||
|
||||
cooldown_time = time.time()
|
||||
|
||||
def set_relay(pin, state):
|
||||
GPIO.output(pin, state)
|
||||
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)
|
||||
time.sleep(0.5)
|
||||
time.sleep(0.5) # atomic
|
||||
set_relay(pin, RELAY_OFF)
|
||||
|
||||
def ring_bell(mac):
|
||||
global allow_watchdog
|
||||
if not allow_watchdog and not DEBUG:
|
||||
global allow_watchdog, cooldown_time
|
||||
|
||||
if not allow_watchdog and not DEBUG and not NO_WATCHDOG:
|
||||
logging.info('Enabling watchdog...')
|
||||
allow_watchdog = True
|
||||
|
||||
if time.time() - cooldown_time < 2:
|
||||
logging.info('Cooldown skipping.')
|
||||
return
|
||||
|
||||
cooldown_time = time.time()
|
||||
|
||||
try:
|
||||
doorbell = settings.DOORBELLS[mac]
|
||||
pulse_relay(doorbell['gpio'])
|
||||
|
@ -58,44 +69,52 @@ def feed_watchdog():
|
|||
wdt.write('1')
|
||||
|
||||
async def ws_listener():
|
||||
session = ClientSession(cookie_jar=CookieJar(unsafe=True))
|
||||
|
||||
unifiprotect = UpvServer(
|
||||
session,
|
||||
settings.UFP_ADDRESS,
|
||||
settings.UFP_PORT,
|
||||
settings.UFP_USERNAME,
|
||||
settings.UFP_PASSWORD,
|
||||
)
|
||||
|
||||
await unifiprotect.update()
|
||||
|
||||
unsub = unifiprotect.subscribe_websocket(subscriber)
|
||||
|
||||
logging.info('Connecting to websocket.')
|
||||
await asyncio.sleep(2)
|
||||
|
||||
while True:
|
||||
session = ClientSession(cookie_jar=CookieJar(unsafe=True))
|
||||
try:
|
||||
updates = await unifiprotect.update()
|
||||
logging.debug('')
|
||||
logging.debug('Updates: %s', json.dumps(updates, indent=4))
|
||||
except NvrError:
|
||||
logging.error('Error updating connection. Reconnecting...')
|
||||
break
|
||||
|
||||
unifiprotect = UpvServer(
|
||||
session,
|
||||
settings.UFP_ADDRESS,
|
||||
settings.UFP_PORT,
|
||||
settings.UFP_USERNAME,
|
||||
settings.UFP_PASSWORD,
|
||||
)
|
||||
active_ws = await unifiprotect.check_ws()
|
||||
if not active_ws:
|
||||
logging.error('Websocket unactive. Reconnecting...')
|
||||
break
|
||||
|
||||
await unifiprotect.update()
|
||||
if allow_watchdog:
|
||||
feed_watchdog()
|
||||
|
||||
unsub = unifiprotect.subscribe_websocket(subscriber)
|
||||
await asyncio.sleep(1)
|
||||
|
||||
logging.info('Connecting to websocket.')
|
||||
await asyncio.sleep(2)
|
||||
await session.close()
|
||||
unsub()
|
||||
|
||||
while True:
|
||||
try:
|
||||
updates = await unifiprotect.update()
|
||||
logging.debug('Updates: %s', str(updates))
|
||||
except NvrError:
|
||||
logging.error('Problem connecting to Unifi Protect. Reconnecting...')
|
||||
break
|
||||
|
||||
active_ws = await unifiprotect.check_ws()
|
||||
if not active_ws:
|
||||
logging.error('Websocket unactive. Reconnecting...')
|
||||
break
|
||||
|
||||
if allow_watchdog and not DEBUG:
|
||||
feed_watchdog()
|
||||
|
||||
await asyncio.sleep(1)
|
||||
|
||||
await session.close()
|
||||
unsub()
|
||||
async def connect():
|
||||
while True:
|
||||
try:
|
||||
await ws_listener()
|
||||
except NvrError as e:
|
||||
logging.error('Error connecting to Unifi Protect: %s. Trying again...', str(e))
|
||||
await asyncio.sleep(3)
|
||||
|
||||
def disable_relays_on_exit(*args):
|
||||
logging.info('Exiting, disabling relays...')
|
||||
|
@ -126,5 +145,5 @@ if __name__ == '__main__':
|
|||
init()
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(ws_listener())
|
||||
loop.run_until_complete(connect())
|
||||
loop.close()
|
||||
|
|
Loading…
Reference in New Issue
Block a user