Skip to content

Commit

Permalink
Adjust received data function. #206
Browse files Browse the repository at this point in the history
  • Loading branch information
xZetsubou committed Apr 25, 2024
1 parent 93cbb8c commit 1566b32
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions custom_components/localtuya/core/pytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def parse_header(data):
# sanity check. currently the max payload length is somewhere around 300 bytes
if payload_len > 2000:
_LOGGER.debug(
f"Header claims the packet size is over 2000 bytes! It is most likely corrupt. Claimed size: {payload_len} bytes. fmt: {fmt} unpacked: {unpacked}"
f"Header claims the packet size is over 2000 bytes! It is most likely corrupt. Claimed size: {payload_len} bytes. fmt: {fmt} unpacked: {unpacked}"
)
# raise DecodeError(
# "Header claims the packet size is over 2000 bytes! It is most likely corrupt. Claimed size: %d bytes. fmt:%s unpacked:%r"
Expand Down Expand Up @@ -625,19 +625,15 @@ def add_data(self, data):
while self.buffer:
prefix_offset_55AA = self.buffer.find(PREFIX_55AA_BIN)
prefix_offset_6699 = self.buffer.find(PREFIX_6699_BIN)

if prefix_offset_55AA < 0 and prefix_offset_6699 < 0:
self.buffer = self.buffer[1 - prefix_len :]
else:
prefix_offset = (
prefix_offset_6699 if prefix_offset_55AA < 0 else prefix_offset_55AA
)
self.buffer = self.buffer[prefix_offset:]
prefix_offset = (
prefix_offset_6699 if prefix_offset_55AA < 0 else prefix_offset_55AA
)

# Check if enough data for measage header
if len(self.buffer) < header_len:
break

self.buffer = self.buffer[prefix_offset:]
header = parse_header(self.buffer)
hmac_key = self.local_key if self.version >= 3.4 else None
no_retcode = False
Expand All @@ -648,7 +644,8 @@ def add_data(self, data):
no_retcode=no_retcode,
logger=self,
)
self.buffer = self.buffer[header_len - 4 + header.length :]
buffer_offset = 6 if prefix_offset_6699 != -1 else 0

This comment has been minimized.

Copy link
@Lurker00

Lurker00 Apr 25, 2024

6 is wrong value! It must be 8.

This comment has been minimized.

Copy link
@xZetsubou

xZetsubou Apr 25, 2024

Author Owner

I guess I forgot to change it back before push it.

self.buffer = self.buffer[header_len - 4 + header.length + buffer_offset :]

This comment has been minimized.

Copy link
@Lurker00

Lurker00 Apr 25, 2024

From my code investigation, better is:

            self.buffer = self.buffer[header.total_length:]

without additional efforts. It runs so far in my HA. The total_length is calculated from MESSAGE_HEADER_FMT_55AA, and does not require -4 compensation for ret_code. It counts 6699 suffix as well.

This comment has been minimized.

Copy link
@xZetsubou

xZetsubou Apr 25, 2024

Author Owner

I'll give it a try.

This comment has been minimized.

Copy link
@xZetsubou

xZetsubou Apr 25, 2024

Author Owner

Yeah, Total length is enough dose the job correctly and it probably the ideal way to handle buffer offset.

self._dispatch(msg)

def _dispatch(self, msg):
Expand Down

0 comments on commit 1566b32

Please sign in to comment.