Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions aidot/device_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,41 +214,48 @@ async def login(self) -> None:
await self.send_action({}, "getDevAttrReq")

async def read_status(self) -> DeviceStatusData:
if self._connect_and_login is False:
if not self._connect_and_login:
await asyncio.sleep(2)
raise AidotNotLogin

try:
data = await self.reader.read(1024)
except (BrokenPipeError, ConnectionResetError) as e:
_LOGGER.error(f"{self.device_id} read status error {e}")
# Read exactly 8 bytes for the header
header = await self.reader.readexactly(8)
magic, msgtype, bodysize = struct.unpack(">HHI", header)

# Read exactly the declared body length
encrypted_data = await self.reader.readexactly(bodysize)

except asyncio.IncompleteReadError as e:
_LOGGER.error(f"{self.device_id} incomplete read: {e}")
await self.reset()
self.status.online = False
return self.status
except Exception as e:
_LOGGER.error(f"recv data error {e}")
return self.status
data_len = len(data)
if data_len <= 0:
_LOGGER.error("recv data error len")
except (BrokenPipeError, ConnectionResetError) as e:
_LOGGER.error(f"{self.device_id} read status error: {e}")
await self.reset()
self.status.online = False
return self.status
except Exception as e:
_LOGGER.error(f"{self.device_id} read error: {e}")
return self.status

try:
magic, msgtype, bodysize = struct.unpack(">HHI", data[:8])
decrypted_data = aes_decrypt(data[8:], self.aes_key)
decrypted_data = aes_decrypt(encrypted_data, self.aes_key)
json_data = json.loads(decrypted_data)
except Exception as e:
_LOGGER.error(f"recv json error : {e}")
_LOGGER.error(f"{self.device_id} recv json error: {e}")
return await self.read_status() # retry once

if json_data.get("service") == "test":
self.ping_count = 0
return await self.read_status()

if "service" in json_data:
if "test" == json_data["service"]:
self.ping_count = 0
return await self.read_status()
payload = json_data.get(CONF_PAYLOAD)
if payload is not None:
self.ascNumber = payload.get(CONF_ASCNUMBER)
self.status.update(payload.get(CONF_ATTR))

return self.status

async def ping_task(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="python-aidot",
version="0.3.41",
version="0.3.42",
author="aidotdev2024",
url='https://github.com/Aidot-Development-Team/python-aidot',
description="aidot control wifi lights",
Expand Down