fix: Reconnect MQTT client on disconnection errors
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
6
main.py
6
main.py
@@ -66,14 +66,20 @@ async def process_mqtt(message, p):
|
|||||||
async def fetch_mqtt(p):
|
async def fetch_mqtt(p):
|
||||||
await asyncio.sleep(3)
|
await asyncio.sleep(3)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
async with Client(
|
async with Client(
|
||||||
hostname='172.17.17.181',
|
hostname='172.17.17.181',
|
||||||
port=1883,
|
port=1883,
|
||||||
) as client:
|
) as client:
|
||||||
|
logging.info('MQTT client connected')
|
||||||
await client.subscribe('#')
|
await client.subscribe('#')
|
||||||
async for message in client.messages:
|
async for message in client.messages:
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.create_task(process_mqtt(message, p))
|
loop.create_task(process_mqtt(message, p))
|
||||||
|
except aiomqtt.MqttError as e:
|
||||||
|
logging.warning('MQTT error: %s. Reconnecting in 5 seconds...', e)
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
async def manage_display(disp):
|
async def manage_display(disp):
|
||||||
|
|||||||
Reference in New Issue
Block a user