Compare commits
2 Commits
8fa91a9c55
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 8a8997750e | |||
| a0e0651703 |
@@ -14,6 +14,7 @@ try:
|
|||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
IS_PI = True
|
IS_PI = True
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
|
logging.info('RPi.GPIO not found, running without GPIO.')
|
||||||
IS_PI = False
|
IS_PI = False
|
||||||
|
|
||||||
import time
|
import time
|
||||||
@@ -27,13 +28,15 @@ RELAY_OFF = True
|
|||||||
|
|
||||||
cooldown_time = time.time()
|
cooldown_time = time.time()
|
||||||
|
|
||||||
|
FAIL_COUNT = 0
|
||||||
|
|
||||||
def set_relay(pin, state):
|
def set_relay(pin, state):
|
||||||
if IS_PI: GPIO.output(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')
|
logging.info('Set relay on pin %s to %s', pin, 'ON' if state == RELAY_ON else 'OFF')
|
||||||
|
|
||||||
def pulse_relay(pin):
|
def pulse_relay(pin):
|
||||||
set_relay(pin, RELAY_ON)
|
set_relay(pin, RELAY_ON)
|
||||||
time.sleep(0.5) # atomic
|
time.sleep(0.25) # atomic
|
||||||
set_relay(pin, RELAY_OFF)
|
set_relay(pin, RELAY_OFF)
|
||||||
|
|
||||||
def ring_bell(camera):
|
def ring_bell(camera):
|
||||||
@@ -47,12 +50,20 @@ def ring_bell(camera):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
doorbell = settings.DOORBELLS[camera]
|
doorbell = settings.DOORBELLS[camera]
|
||||||
|
logging.info('Ringing doorbell: %s', doorbell['name'])
|
||||||
pulse_relay(doorbell['gpio'])
|
pulse_relay(doorbell['gpio'])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
logging.error('Doorbell %s not found!', camera)
|
logging.error('Doorbell %s not found!', camera)
|
||||||
|
|
||||||
|
|
||||||
async def process_message(msg):
|
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':
|
if msg.get('type', '') != 'ring':
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -61,13 +72,16 @@ async def process_message(msg):
|
|||||||
ring_bell(msg['camera'])
|
ring_bell(msg['camera'])
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
global FAIL_COUNT
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
async for msg in unifi.connect():
|
async for msg in unifi.connect():
|
||||||
await process_message(msg)
|
await process_message(msg)
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
logging.error('Error connecting to Unifi Protect: %s. Trying again...', str(e))
|
FAIL_COUNT += 1
|
||||||
await asyncio.sleep(3)
|
logging.error('Problem connecting to Unifi Protect: %s - %s, fail count: %s', e.__class__.__name__, e, FAIL_COUNT)
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
def disable_relays_on_exit(*args):
|
def disable_relays_on_exit(*args):
|
||||||
@@ -92,6 +106,23 @@ def init():
|
|||||||
signal(sig, disable_relays_on_exit)
|
signal(sig, disable_relays_on_exit)
|
||||||
logging.info('Signals initialized')
|
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__':
|
if __name__ == '__main__':
|
||||||
logging.info('')
|
logging.info('')
|
||||||
logging.info('======================================')
|
logging.info('======================================')
|
||||||
@@ -99,5 +130,6 @@ if __name__ == '__main__':
|
|||||||
init()
|
init()
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
if not DEBUG:
|
||||||
|
a = loop.create_task(watchdog())
|
||||||
loop.run_until_complete(main())
|
loop.run_until_complete(main())
|
||||||
loop.close()
|
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
|
import os, logging
|
||||||
|
DEBUG = os.environ.get('DEBUG')
|
||||||
|
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 asyncio
|
import asyncio
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import zlib
|
import zlib
|
||||||
@@ -15,12 +22,24 @@ async def connect():
|
|||||||
rememberMe=True,
|
rememberMe=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logging.info('Connecting to Unifi Protect...')
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(settings.UFP_ADDRESS + '/api/auth/login', json=data, ssl=False) as resp:
|
async with session.post(settings.UFP_ADDRESS + '/api/auth/login', json=data, ssl=False, timeout=5) as resp:
|
||||||
cookie = resp.cookies['TOKEN']
|
cookie = resp.cookies['TOKEN']
|
||||||
|
|
||||||
|
logging.info('Got cookie.')
|
||||||
|
|
||||||
headers = {'cookie': cookie.key + '=' + cookie.value}
|
headers = {'cookie': cookie.key + '=' + cookie.value}
|
||||||
async with session.ws_connect(settings.UFP_ADDRESS + '/proxy/protect/ws/updates', headers=headers, ssl=False) as ws:
|
async with session.ws_connect(
|
||||||
|
settings.UFP_ADDRESS + '/proxy/protect/ws/updates',
|
||||||
|
headers=headers,
|
||||||
|
ssl=False,
|
||||||
|
receive_timeout=10.0,
|
||||||
|
heartbeat=10.0,
|
||||||
|
) as ws:
|
||||||
|
yield 'CONNECTED'
|
||||||
|
|
||||||
async for msg in ws:
|
async for msg in ws:
|
||||||
packet_type, payload_format, deflated, unknown, payload_size = struct.unpack('!bbbbi', msg.data[0:HEADER_LENGTH])
|
packet_type, payload_format, deflated, unknown, payload_size = struct.unpack('!bbbbi', msg.data[0:HEADER_LENGTH])
|
||||||
action_start = HEADER_LENGTH
|
action_start = HEADER_LENGTH
|
||||||
@@ -30,6 +49,8 @@ async def connect():
|
|||||||
|
|
||||||
yield json.loads(data_packet.decode())
|
yield json.loads(data_packet.decode())
|
||||||
|
|
||||||
|
logging.info('Lost connection to web socket.')
|
||||||
|
|
||||||
|
|
||||||
async def test():
|
async def test():
|
||||||
async for msg in connect():
|
async for msg in connect():
|
||||||
|
|||||||
Reference in New Issue
Block a user