-
Notifications
You must be signed in to change notification settings - Fork 74
feat: Add pinging to local client #627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |||||
| _LOGGER = logging.getLogger(__name__) | ||||||
| _PORT = 58867 | ||||||
| _TIMEOUT = 5.0 | ||||||
| _PING_INTERVAL = 10 | ||||||
|
|
||||||
|
|
||||||
| @dataclass | ||||||
|
|
@@ -58,6 +59,7 @@ def __init__(self, host: str, local_key: str): | |||||
| self._subscribers: CallbackList[RoborockMessage] = CallbackList(_LOGGER) | ||||||
| self._is_connected = False | ||||||
| self._local_protocol_version: LocalProtocolVersion | None = None | ||||||
| self._keep_alive_task: asyncio.Task[None] | None = None | ||||||
| self._update_encoder_decoder( | ||||||
| LocalChannelParams(local_key=local_key, connect_nonce=get_next_int(10000, 32767), ack_nonce=None) | ||||||
| ) | ||||||
|
|
@@ -132,6 +134,28 @@ async def _hello(self): | |||||
|
|
||||||
| raise RoborockException("Failed to connect to device with any known protocol") | ||||||
|
|
||||||
| async def _ping(self) -> None: | ||||||
| ping_message = RoborockMessage( | ||||||
| protocol=RoborockMessageProtocol.PING_REQUEST, version=self.protocol_version.encode() | ||||||
| ) | ||||||
| await self._send_message( | ||||||
| roborock_message=ping_message, | ||||||
| request_id=ping_message.seq, | ||||||
| response_protocol=RoborockMessageProtocol.PING_RESPONSE, | ||||||
| ) | ||||||
|
|
||||||
| async def _keep_alive_loop(self) -> None: | ||||||
| while self._is_connected: | ||||||
| try: | ||||||
| await asyncio.sleep(_PING_INTERVAL) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming you accept the above suggestion
Suggested change
|
||||||
| if self._is_connected: | ||||||
| await self._ping() | ||||||
| except asyncio.CancelledError: | ||||||
| break | ||||||
| except Exception: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's catch RoborockException (expected) separate from uncaught exceptions so we can log them separately e.g. "ping failed" vs "Uncaught exception" implying a bug/shouldn't happen case we need to fix. Similar to |
||||||
| _LOGGER.debug("Keep-alive ping failed", exc_info=True) | ||||||
| # Retry next interval | ||||||
|
Comment on lines
+147
to
+157
|
||||||
|
|
||||||
| @property | ||||||
| def protocol_version(self) -> LocalProtocolVersion: | ||||||
| """Return the negotiated local protocol version, or a sensible default.""" | ||||||
|
|
@@ -166,6 +190,7 @@ async def connect(self) -> None: | |||||
| # Perform protocol negotiation | ||||||
| try: | ||||||
| await self._hello() | ||||||
| self._keep_alive_task = asyncio.create_task(self._keep_alive_loop()) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this outside the scope of the try/catch. It won't throw RoborockException |
||||||
| except RoborockException: | ||||||
| # If protocol negotiation fails, clean up the connection state | ||||||
| self.close() | ||||||
|
|
@@ -177,6 +202,8 @@ def _data_received(self, data: bytes) -> None: | |||||
|
|
||||||
| def close(self) -> None: | ||||||
| """Disconnect from the device.""" | ||||||
| if self._keep_alive_task: | ||||||
| self._keep_alive_task.cancel() | ||||||
|
Lash-L marked this conversation as resolved.
|
||||||
| if self._transport: | ||||||
| self._transport.close() | ||||||
| else: | ||||||
|
|
@@ -187,6 +214,8 @@ def close(self) -> None: | |||||
| def _connection_lost(self, exc: Exception | None) -> None: | ||||||
| """Handle connection loss.""" | ||||||
| _LOGGER.warning("Connection lost to %s", self._host, exc_info=exc) | ||||||
| if self._keep_alive_task: | ||||||
| self._keep_alive_task.cancel() | ||||||
|
Lash-L marked this conversation as resolved.
|
||||||
| self._transport = None | ||||||
| self._is_connected = False | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use a timedelta here to be more explicit on the unit?