Initial skeleton of MQTT listener
This commit is contained in:
54
main.py
Normal file
54
main.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import os, sys
|
||||||
|
import logging
|
||||||
|
DEBUG = os.environ.get('DEBUG')
|
||||||
|
logging.basicConfig(stream=sys.stdout,
|
||||||
|
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
|
||||||
|
level=logging.DEBUG if DEBUG else logging.INFO)
|
||||||
|
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from aiomqtt import Client
|
||||||
|
|
||||||
|
async def manage_bluetooth():
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
async def process_bluetooth_command(topic, text):
|
||||||
|
logging.info('Bluetooth command: %s', text)
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def process_mqtt(message):
|
||||||
|
text = message.payload.decode()
|
||||||
|
topic = message.topic.value
|
||||||
|
logging.debug('MQTT topic: %s, message: %s', topic, text)
|
||||||
|
|
||||||
|
if topic.startswith('iot/12ser/bluetooth'):
|
||||||
|
await process_bluetooth_command(topic, text)
|
||||||
|
else:
|
||||||
|
logging.debug('Invalid topic, returning')
|
||||||
|
return
|
||||||
|
|
||||||
|
async def fetch_mqtt():
|
||||||
|
await asyncio.sleep(3)
|
||||||
|
|
||||||
|
async with Client(
|
||||||
|
hostname='10.55.0.106',
|
||||||
|
port=1883,
|
||||||
|
) as client:
|
||||||
|
await client.subscribe('#')
|
||||||
|
async for message in client.messages:
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
loop.create_task(process_mqtt(message))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
logging.info('')
|
||||||
|
logging.info('==========================')
|
||||||
|
logging.info('Booting up...')
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
a = loop.create_task(manage_bluetooth())
|
||||||
|
loop.run_until_complete(fetch_mqtt())
|
||||||
Reference in New Issue
Block a user