From 462109c330d30ca591948a4de78ff08d27ef2d68 Mon Sep 17 00:00:00 2001 From: starkillerOG Date: Sat, 30 Nov 2024 13:16:21 +0100 Subject: [PATCH] Add check_subscribe_events as a watchdog --- reolink_aio/baichuan/baichuan.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/reolink_aio/baichuan/baichuan.py b/reolink_aio/baichuan/baichuan.py index fc6a438..422c8ed 100644 --- a/reolink_aio/baichuan/baichuan.py +++ b/reolink_aio/baichuan/baichuan.py @@ -70,6 +70,7 @@ def __init__( self._events_active: bool = False self._keepalive_task: asyncio.Task | None = None self._keepalive_interval: float = KEEP_ALLIVE_INTERVAL + self._time_keepalive_loop: float = 0 self._time_reestablish: float = 0 self._time_keepalive_increase: float = 0 self._time_connection_lost: float = 0 @@ -490,9 +491,9 @@ async def _keepalive_loop(self) -> None: now: float = 0 while True: try: - while self._protocol is not None: now = time_now() + self._time_keepalive_loop = now sleep_t = min(self._keepalive_interval - (now - self._protocol.time_recv), self._keepalive_interval) if sleep_t < 0.5: break @@ -543,6 +544,20 @@ async def unsubscribe_events(self) -> None: self._keepalive_task = None await self.logout() + async def check_subscribe_events(self) -> None: + """Subscribe to baichuan push events, keeping the connection open""" + if not self._subscribed: + await self.subscribe_events() + return + + if time_now() - self._time_keepalive_loop > 5 * KEEP_ALLIVE_INTERVAL: + # keepalive loop seems to have stopped running, restart + _LOGGER.error("Baichuan host %s: keepalive loop seems to have stopped running, restarting", self._host) + self._events_active = False + if self._keepalive_task is not None: + self._keepalive_task.cancel() + self._keepalive_task = self._loop.create_task(self._keepalive_loop()) + async def login(self) -> None: """Login using the Baichuan protocol""" nonce = await self._get_nonce()