fix: Reconnect MQTT client automatically on MqttError
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
7
main.py
7
main.py
@@ -10,6 +10,7 @@ import json
|
||||
|
||||
import asyncio
|
||||
from aiomqtt import Client
|
||||
from aiomqtt.exceptions import MqttError
|
||||
from dbus_next.aio import MessageBus
|
||||
from dbus_next.service import ServiceInterface, method
|
||||
from dbus_next.constants import BusType
|
||||
@@ -311,14 +312,20 @@ async def process_mqtt(message):
|
||||
async def fetch_mqtt():
|
||||
await asyncio.sleep(3)
|
||||
|
||||
while True:
|
||||
try:
|
||||
async with Client(
|
||||
hostname='10.55.0.106',
|
||||
port=1883,
|
||||
) as client:
|
||||
logging.info("MQTT client connected")
|
||||
await client.subscribe('iot/12ser/#')
|
||||
async for message in client.messages:
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(process_mqtt(message))
|
||||
except MqttError as e:
|
||||
logging.warning(f"MQTT connection error: {e}. Reconnecting in 5 seconds...")
|
||||
await asyncio.sleep(5)
|
||||
|
||||
|
||||
def suppress_hfp_rejection_error(loop, context):
|
||||
|
||||
Reference in New Issue
Block a user