Skip to content

Fix read_status failure for multiple statuses after actions#2

Open
huangruoqi wants to merge 2 commits into
AiDot-Development-Team:mainfrom
huangruoqi:main
Open

Fix read_status failure for multiple statuses after actions#2
huangruoqi wants to merge 2 commits into
AiDot-Development-Team:mainfrom
huangruoqi:main

Conversation

@huangruoqi

Copy link
Copy Markdown

Summary

This PR fixes an issue where read_status() could fail due to multiple status data when using reader.read(). The updated implementation uses reader.readexactly() to ensure the AES-encrypted payload is fully and correctly received before decryption.

Problem

Previously, read_status() used:

data = await self.reader.read(1024)

Using this test script

cl = DeviceClient(device, user_info)

await cl.connect("10.0.0.250")
for i in range(6):
    await asyncio.sleep(0.5)
    await cl.async_set_brightness(i * 50)

for i in range(1 + 6*2):
    print((await cl.read_status()).__dict__)

causes:

recv json error : The length of the provided data is not a multiple of the block length.
recv json error : The length of the provided data is not a multiple of the block length.
recv json error : The length of the provided data is not a multiple of the block length.
recv json error : 'utf-8' codec can't decode byte 0xd0 in position 1: invalid continuation byte
{'online': True, 'on': 1, 'dimming': 147, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 249, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
^C
KeyboardInterrupt
>>>
KeyboardInterrupt

This was unsafe because AES requires a block-aligned input; partial data causes decryption errors. This led to errors like:

  • The length of the provided data is not a multiple of the block length
  • utf-8 decode error: invalid continuation byte

Changes

Replaced read() with readexactly(8) for the header and readexactly(bodysize) for the encrypted payload in read_status().

Testing

Reran the test script:

{'online': True, 'on': 1, 'dimming': 249, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 249, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 0, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 0, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 48, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 48, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 99, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 99, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 147, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 147, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 198, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 198, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}
{'online': True, 'on': 1, 'dimming': 249, 'rgdb': -4422400, 'rgbw': (255, 188, 133, 0)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant