Skip to content

Commit

Permalink
Add check_subscribe_events as a watchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
starkillerOG committed Nov 30, 2024
1 parent 9077aa8 commit 3ad435d
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion reolink_aio/baichuan/baichuan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -543,6 +544,19 @@ 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
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()
Expand Down

0 comments on commit 3ad435d

Please sign in to comment.