api: implement automatic mqtt reconnect in ccujack cron
All checks were successful
Build Container / Build Container (push) Successful in 1m27s

This commit is contained in:
lilly 2026-05-28 16:23:03 +02:00
commit 6bbfc5dc68
Signed by: lilly
SSH key fingerprint: SHA256:y9T5GFw2A20WVklhetIxG1+kcg/Ce0shnQmbu1LQ37g
2 changed files with 12 additions and 1 deletions

View file

@ -162,9 +162,12 @@ class CCUJackClient:
while True: while True:
try: try:
await asyncio.sleep(60 * 60) # 1 hour await asyncio.sleep(15 * 60) # 15 minutes
logger.info("Running CCUJack cron") logger.info("Running CCUJack cron")
await self.find_locks() await self.find_locks()
if not self.mqtt.is_connected():
logger.warning("MQTT client was discovered to be disconnected; reconnecting now")
await self.mqtt.connect()
except Exception as e: except Exception as e:
logger.exception(f"Error in CCUJack cron task: {e}") logger.exception(f"Error in CCUJack cron task: {e}")

View file

@ -154,8 +154,16 @@ class AsyncMqttClient:
await self.fut_connected await self.fut_connected
# re-establish all supposed mqtt subscriptions
if len(self.active_subscriptions > 0):
qos = 1
await self.client.subscribe((i, qos) for i in self.active_subscriptions)
async def disconnect(self): async def disconnect(self):
logger.info("Disconnecting mqtt client from broker") logger.info("Disconnecting mqtt client from broker")
self.fut_disconnect = asyncio.get_running_loop().create_future() self.fut_disconnect = asyncio.get_running_loop().create_future()
self.client.disconnect() self.client.disconnect()
await self.fut_disconnect await self.fut_disconnect
def is_connected(self) -> bool:
return self.client.is_connected()