Skip to content

Commit

Permalink
Reset partial_data on every _send_request
Browse files Browse the repository at this point in the history
  • Loading branch information
mletenay committed May 20, 2024
1 parent bbbbcbb commit ef9034b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions goodwe/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def datagram_received(self, data: bytes, addr: Tuple[str, int]) -> None:
self._timer = None
try:
if self._partial_data and self._partial_missing == len(data):
logger.debug("Composed fragmented response: %s", data.hex())
logger.debug("Composed fragmented response: %s + %s", self._partial_data.hex(), data.hex())
data = self._partial_data + data
self._partial_data = None
self._partial_missing = 0
Expand All @@ -141,7 +141,6 @@ def datagram_received(self, data: bytes, addr: Tuple[str, int]) -> None:
logger.debug("Received response fragment (%d of %d): %s", ex.length, ex.expected, data.hex())
self._partial_data = data
self._partial_missing = ex.expected - ex.length
return
except asyncio.InvalidStateError:
logger.debug("Response already handled: %s", data.hex())
except RequestRejectedException as ex:
Expand All @@ -161,7 +160,6 @@ async def send_request(self, command: ProtocolCommand) -> Future:
await self._connect()
response_future = asyncio.get_running_loop().create_future()
self._retry = 0
self._partial_data = None
self._send_request(command, response_future)
await response_future
return response_future
Expand All @@ -170,6 +168,8 @@ def _send_request(self, command: ProtocolCommand, response_future: Future) -> No
"""Send message via transport"""
self.command = command
self.response_future = response_future
self._partial_data = None
self._partial_missing = 0
payload = command.request_bytes()
if self._retry > 0:
logger.debug("Sending: %s - retry #%s/%s", self.command, self._retry, self.retries)
Expand Down Expand Up @@ -268,7 +268,7 @@ def data_received(self, data: bytes) -> None:
self._timer.cancel()
try:
if self._partial_data and self._partial_missing == len(data):
logger.debug("Composed fragmented response: %s", data.hex())
logger.debug("Composed fragmented response: %s + %s", self._partial_data.hex(), data.hex())
data = self._partial_data + data
self._partial_data = None
self._partial_missing = 0
Expand All @@ -284,7 +284,6 @@ def data_received(self, data: bytes) -> None:
logger.debug("Received response fragment (%d of %d): %s", ex.length, ex.expected, data.hex())
self._partial_data = data
self._partial_missing = ex.expected - ex.length
return
except asyncio.InvalidStateError:
logger.debug("Response already handled: %s", data.hex())
except RequestRejectedException as ex:
Expand All @@ -304,7 +303,6 @@ async def send_request(self, command: ProtocolCommand) -> Future:
try:
await asyncio.wait_for(self._connect(), timeout=5)
response_future = asyncio.get_running_loop().create_future()
self._partial_data = None
self._send_request(command, response_future)
await response_future
return response_future
Expand Down Expand Up @@ -336,6 +334,8 @@ def _send_request(self, command: ProtocolCommand, response_future: Future) -> No
"""Send message via transport"""
self.command = command
self.response_future = response_future
self._partial_data = None
self._partial_missing = 0
payload = command.request_bytes()
if self._retry > 0:
logger.debug("Sending: %s - retry #%s/%s", self.command, self._retry, self.retries)
Expand Down

0 comments on commit ef9034b

Please sign in to comment.