|
|
|
@ -19,7 +19,7 @@ import json |
|
|
|
|
import time |
|
|
|
|
import requests |
|
|
|
|
from aiohttp import web, ClientSession, ClientError |
|
|
|
|
from asyncio_mqtt import Client |
|
|
|
|
import aiomqtt |
|
|
|
|
from datetime import datetime, timedelta |
|
|
|
|
import pytz |
|
|
|
|
TIMEZONE = pytz.timezone('America/Edmonton') |
|
|
|
@ -295,7 +295,7 @@ async def process_mqtt(message): |
|
|
|
|
except UnicodeDecodeError: |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
topic = message.topic |
|
|
|
|
topic = message.topic.value |
|
|
|
|
logging.debug('MQTT topic: %s, message: %s', topic, text) |
|
|
|
|
|
|
|
|
|
if topic.startswith('test'): |
|
|
|
@ -313,11 +313,21 @@ async def process_mqtt(message): |
|
|
|
|
await process_data(data) |
|
|
|
|
|
|
|
|
|
async def fetch_mqtt(): |
|
|
|
|
async with Client('localhost') as client: |
|
|
|
|
async with client.filtered_messages('#') as messages: |
|
|
|
|
await asyncio.sleep(3) |
|
|
|
|
|
|
|
|
|
# from https://sbtinstruments.github.io/aiomqtt/reconnection.html |
|
|
|
|
# modified to make new client since their code didn't work |
|
|
|
|
# https://github.com/sbtinstruments/aiomqtt/issues/269 |
|
|
|
|
while True: |
|
|
|
|
try: |
|
|
|
|
async with aiomqtt.Client('localhost') as client: |
|
|
|
|
await client.subscribe('#') |
|
|
|
|
async for message in messages: |
|
|
|
|
async for message in client.messages: |
|
|
|
|
await process_mqtt(message) |
|
|
|
|
except aiomqtt.MqttError: |
|
|
|
|
logging.info('MQTT connection lost, reconnecting in 5 seconds...') |
|
|
|
|
await asyncio.sleep(5) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def owntracks(request): |
|
|
|
|
data = await request.json() |
|
|
|
|