You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.4 KiB

import os, sys
import logging
logging.basicConfig(stream=sys.stdout,
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
level=logging.DEBUG if os.environ.get('DEBUG') else logging.INFO)
import time
import json
import asyncio
from asyncio_mqtt import Client
import pygame
import secrets
COOLDOWN = time.time()
CAMERAS = {
'NE-W-ZoneB': {
'name': 'Front Door',
'sound': 'barkLR.ogg',
},
'SE-N-ZoneB': {
'name': 'Side Door',
'sound': 'barkRL.ogg',
},
'B-E-ZoneB': {
'name': 'Back Door',
'sound': 'barkLR.ogg',
},
}
async def play_sound(filename):
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
logging.info('Playing sound %s', filename)
while pygame.mixer.music.get_busy():
#pygame.time.Clock().tick(10)
await asyncio.sleep(0.1)
async def barkbark(sound):
global COOLDOWN
if time.time() - COOLDOWN < 20.0:
logging.info('Cooldown skipping.')
return
COOLDOWN = time.time()
await asyncio.sleep(0.1)
await play_sound(sound)
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)
if not topic.startswith('iot/cameras'):
logging.info('Invalid topic, returning')
return
try:
data = json.loads(text)
except json.JSONDecodeError:
logging.info('Invalid json, returning')
return
serial = str(data.get('serial', ''))
if serial not in CAMERAS:
logging.info('Invalid serial, returning')
return
camera = CAMERAS[serial]
logging.info('Barking %s...', camera['name'])
await barkbark(camera['sound'])
async def fetch_mqtt():
await asyncio.sleep(3)
async with Client('192.168.0.106') 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))
if __name__ == '__main__':
logging.info('')
logging.info('==========================')
logging.info('Booting up...')
pygame.init()
pygame.mixer.pre_init(buffer=4096)
pygame.mixer.init(buffer=4096)
loop = asyncio.get_event_loop()
loop.run_until_complete(fetch_mqtt())