Compare commits
No commits in common. "59fcd694b6d94818ffbe5aa318c3112cc46e3c65" and "5a29373838739605a855f654c90b37df6064d178" have entirely different histories.
59fcd694b6
...
5a29373838
41
main.py
41
main.py
|
@ -11,7 +11,6 @@ import asyncio
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
import time
|
import time
|
||||||
from signal import *
|
|
||||||
from aiohttp import ClientSession, CookieJar
|
from aiohttp import ClientSession, CookieJar
|
||||||
|
|
||||||
import settings
|
import settings
|
||||||
|
@ -21,24 +20,12 @@ from pyunifiprotect.unifi_protect_server import UpvServer
|
||||||
RELAY_ON = False
|
RELAY_ON = False
|
||||||
RELAY_OFF = True
|
RELAY_OFF = True
|
||||||
|
|
||||||
allow_watchdog = False
|
|
||||||
|
|
||||||
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.25)
|
|
||||||
set_relay(pin, RELAY_OFF)
|
|
||||||
|
|
||||||
def ring_bell(mac):
|
def ring_bell(mac):
|
||||||
global allow_watchdog
|
|
||||||
allow_watchdog = True
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
doorbell = settings.DOORBELLS[mac]
|
doorbell = settings.DOORBELLS[mac]
|
||||||
pulse_relay(doorbell['gpio'])
|
GPIO.output(doorbell['gpio'], RELAY_ON)
|
||||||
|
time.sleep(0.5)
|
||||||
|
GPIO.output(doorbell['gpio'], RELAY_OFF)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
logging.error('Doorbell %s not found!', mac)
|
logging.error('Doorbell %s not found!', mac)
|
||||||
|
|
||||||
|
@ -50,10 +37,6 @@ def subscriber(updated):
|
||||||
logging.info('%s: %s is ringing!', data['mac'], data['name'])
|
logging.info('%s: %s is ringing!', data['mac'], data['name'])
|
||||||
ring_bell(data['mac'])
|
ring_bell(data['mac'])
|
||||||
|
|
||||||
def feed_watchdog():
|
|
||||||
with open('/dev/watchdog', 'w') as wdt:
|
|
||||||
wdt.write('1')
|
|
||||||
|
|
||||||
async def ws_listener():
|
async def ws_listener():
|
||||||
session = ClientSession(cookie_jar=CookieJar(unsafe=True))
|
session = ClientSession(cookie_jar=CookieJar(unsafe=True))
|
||||||
|
|
||||||
|
@ -70,10 +53,7 @@ async def ws_listener():
|
||||||
|
|
||||||
unsub = unifiprotect.subscribe_websocket(subscriber)
|
unsub = unifiprotect.subscribe_websocket(subscriber)
|
||||||
|
|
||||||
while True:
|
for i in range(15000):
|
||||||
if not DEBUG and allow_watchdog:
|
|
||||||
logging.debug('Feeding watchdog...')
|
|
||||||
feed_watchdog()
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
await session.close()
|
await session.close()
|
||||||
|
@ -82,7 +62,7 @@ async def ws_listener():
|
||||||
def disable_relays_on_exit(*args):
|
def disable_relays_on_exit(*args):
|
||||||
logging.info('Exiting, disabling relays...')
|
logging.info('Exiting, disabling relays...')
|
||||||
for _, doorbell in settings.DOORBELLS.items():
|
for _, doorbell in settings.DOORBELLS.items():
|
||||||
set_relay(doorbell['gpio'], RELAY_OFF)
|
GPIO.output(doorbell['gpio'], RELAY_OFF)
|
||||||
logging.info('Goodbye.')
|
logging.info('Goodbye.')
|
||||||
os._exit(0)
|
os._exit(0)
|
||||||
|
|
||||||
|
@ -92,17 +72,24 @@ def init():
|
||||||
|
|
||||||
for _, doorbell in settings.DOORBELLS.items():
|
for _, doorbell in settings.DOORBELLS.items():
|
||||||
GPIO.setup(doorbell['gpio'], GPIO.OUT)
|
GPIO.setup(doorbell['gpio'], GPIO.OUT)
|
||||||
pulse_relay(doorbell['gpio'])
|
GPIO.output(doorbell['gpio'], RELAY_OFF)
|
||||||
time.sleep(1)
|
|
||||||
logging.info('GPIO initialized')
|
logging.info('GPIO initialized')
|
||||||
|
|
||||||
for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
|
for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
|
||||||
signal(sig, disable_relays_on_exit)
|
signal(sig, disable_relays_on_exit)
|
||||||
logging.info('Signals initialized')
|
logging.info('Signals initialized')
|
||||||
|
|
||||||
|
async def feed_watchdog():
|
||||||
|
while True:
|
||||||
|
with open('/dev/watchdog', 'w') as wdt:
|
||||||
|
wdt.write('1')
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
init()
|
init()
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
if not DEBUG:
|
||||||
|
loop.create_task(feed_watchdog())
|
||||||
loop.run_until_complete(ws_listener())
|
loop.run_until_complete(ws_listener())
|
||||||
loop.close()
|
loop.close()
|
||||||
|
|
|
@ -5,14 +5,11 @@ attrs==21.2.0
|
||||||
chardet==4.0.0
|
chardet==4.0.0
|
||||||
click==8.0.3
|
click==8.0.3
|
||||||
idna==3.3
|
idna==3.3
|
||||||
importlib-metadata==4.8.1
|
|
||||||
multidict==5.2.0
|
multidict==5.2.0
|
||||||
Pillow==8.4.0
|
Pillow==8.4.0
|
||||||
PyJWT==2.2.0
|
PyJWT==2.2.0
|
||||||
python-dotenv==0.19.1
|
python-dotenv==0.19.1
|
||||||
pyunifiprotect==0.33.0
|
pyunifiprotect==0.33.0
|
||||||
RPi.GPIO==0.7.0
|
|
||||||
typer==0.4.0
|
typer==0.4.0
|
||||||
typing-extensions==3.10.0.2
|
typing-extensions==3.10.0.2
|
||||||
yarl==1.7.0
|
yarl==1.7.0
|
||||||
zipp==3.6.0
|
|
||||||
|
|
|
@ -5,11 +5,11 @@ UFP_PORT = 443
|
||||||
|
|
||||||
DOORBELLS = {
|
DOORBELLS = {
|
||||||
'123456780ABC': {
|
'123456780ABC': {
|
||||||
'name': 'Front Door',
|
name: 'Front Door',
|
||||||
'gpio': 26,
|
gpio: 26,
|
||||||
},
|
},
|
||||||
'123456780ABC': {
|
'123456780ABC': {
|
||||||
'name': 'Side Door',
|
name: 'Side Door',
|
||||||
'gpio': 19,
|
gpio: 19,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user