Skip to content

Commit

Permalink
chore: fix check_protocol got timeout and not close socket
Browse files Browse the repository at this point in the history
  • Loading branch information
wuwentao committed Sep 19, 2024
1 parent f9f7475 commit 5ff3686
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions midealocal/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ def get_capabilities(self) -> None:
for cmd in cmds:
self.build_send(cmd)

def _recv_message(self) -> dict[str, MessageResult | bytes]:
def _recv_message(
self,
check_protocol: bool = False,
) -> dict[str, MessageResult | bytes]:
"""Recv message."""
if not self._socket:
_LOGGER.warning("[%s] _recv_message socket error", self._device_id)
Expand All @@ -325,9 +328,14 @@ def _recv_message(self) -> dict[str, MessageResult | bytes]:
if msg:
return {"result": MessageResult.SUCCESS, "msg": msg}
except TimeoutError:
_LOGGER.debug("[%s] _recv_message Socket timed out", self._device_id)
# close socket when exception matched
self.close_socket()
_LOGGER.debug(
"[%s] _recv_message Socket timed out with check_protocol %s",
self._device_id,
check_protocol,
)
# close socket when timeout and not check_protocol
if not check_protocol:
self.close_socket()
return {"result": MessageResult.TIMEOUT}
except Exception as e:
_LOGGER.exception(
Expand All @@ -350,7 +358,7 @@ def refresh_status(self, check_protocol: bool = False) -> None:
if cmd.__class__.__name__ not in self._unsupported_protocol:
# set query flag for query timeout
self.build_send(cmd, query=True)
response = self._recv_message()
response = self._recv_message(check_protocol=check_protocol)
# recovery timeout after _recv_message
self._recovery_timeout()
# normal msg
Expand All @@ -360,10 +368,13 @@ def refresh_status(self, check_protocol: bool = False) -> None:
msg = response.get("msg")
if isinstance(msg, bytes):
result = self.parse_message(msg=msg)
if result == MessageResult.SUCCESS:
break
# msg padding
continue
if result != MessageResult.SUCCESS:
_LOGGER.error(
"[%s] parse_message %s result is %s",
self._device_id,
msg,
result,
)
# empty msg
elif response.get("result") == MessageResult.PADDING:
continue
Expand Down

0 comments on commit 5ff3686

Please sign in to comment.