woof/main.py

102 lines
2.3 KiB
Python
Raw Normal View History

2022-01-30 23:52:17 +00:00
import os, sys
2021-10-07 04:02:16 +00:00
import logging
2022-01-30 23:52:17 +00:00
logging.basicConfig(stream=sys.stdout,
2021-10-07 04:02:16 +00:00
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
level=logging.DEBUG if os.environ.get('DEBUG') else logging.INFO)
2021-10-06 09:04:07 +00:00
import time
2021-10-07 04:02:16 +00:00
import json
import asyncio
from asyncio_mqtt import Client
2021-10-06 09:04:07 +00:00
import pygame
2021-10-07 04:02:16 +00:00
import secrets
2021-10-06 09:04:07 +00:00
COOLDOWN = time.time()
2022-06-27 23:13:24 +00:00
CAMERAS = {
'NE-W-ZoneB': {
'name': 'Front Door',
2022-06-27 23:13:24 +00:00
'sound': 'barkLR.ogg',
},
2022-06-27 23:13:24 +00:00
'SE-N-ZoneB': {
'name': 'Side Door',
'sound': 'barkRL.ogg',
}
}
async def play_sound(filename):
2021-10-06 09:04:07 +00:00
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
2021-10-07 04:02:16 +00:00
logging.info('Playing sound %s', filename)
2021-10-06 09:04:07 +00:00
while pygame.mixer.music.get_busy():
#pygame.time.Clock().tick(10)
await asyncio.sleep(0.1)
2022-06-27 23:13:24 +00:00
async def bark(sound):
global COOLDOWN
if time.time() - COOLDOWN < 5.0:
logging.info('Cooldown skipping.')
return
COOLDOWN = time.time()
await asyncio.sleep(0.1)
await play_sound(sound)
2022-06-27 23:13:24 +00:00
logging.info('Done barking.')
async def process_mqtt(message):
text = message.payload.decode()
topic = message.topic
logging.info('MQTT topic: %s, message: %s', topic, text)
2021-10-06 09:04:07 +00:00
2022-06-27 23:13:24 +00:00
if not topic.startswith('iot/cameras'):
logging.info('Invalid topic, returning')
return
2021-10-06 09:04:07 +00:00
try:
data = json.loads(text)
except json.JSONDecodeError:
logging.info('Invalid json, returning')
return
id_ = str(data.get('id', ''))
2022-06-27 23:13:24 +00:00
if id_ not in CAMERAS:
logging.info('Invalid id, returning')
return
2022-06-27 23:13:24 +00:00
camera = CAMERAS[id_]
2022-06-27 23:13:24 +00:00
logging.info('Ringing %s...', camera['name'])
await ring_bell(doorbell['sound'])
async def fetch_mqtt():
await asyncio.sleep(3)
2022-06-27 23:13:24 +00:00
async with Client('192.168.0.100') as client:
async with client.filtered_messages('iot/cameras') as messages:
await client.subscribe('#')
async for message in messages:
loop = asyncio.get_event_loop()
loop.create_task(process_mqtt(message))
2021-10-06 09:04:07 +00:00
2021-10-07 04:02:16 +00:00
if __name__ == '__main__':
logging.info('')
logging.info('==========================')
logging.info('Booting up...')
2021-10-07 04:02:16 +00:00
pygame.init()
2022-01-30 23:52:17 +00:00
pygame.mixer.pre_init(buffer=4096)
pygame.mixer.init(buffer=4096)
2021-10-06 09:04:07 +00:00
loop = asyncio.get_event_loop()
loop.run_until_complete(fetch_mqtt())