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