python-aidot 0.3.46__tar.gz → 0.3.48__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-aidot
3
- Version: 0.3.46
3
+ Version: 0.3.48
4
4
  Summary: aidot control wifi lights
5
5
  Home-page: https://github.com/Aidot-Development-Team/python-aidot
6
6
  Author: aidotdev2024
@@ -77,6 +77,7 @@ class AidotClient:
77
77
  password: str | None = None,
78
78
  token: dict | None = None,
79
79
  ) -> None:
80
+ _LOGGER.info(f"Aidot Client Version: v0.3.46")
80
81
  self.session = session
81
82
  self.username = username
82
83
  self.password = password
@@ -114,6 +114,7 @@ class DeviceClient(object):
114
114
  _ping_timer: Any = None
115
115
  writer: Any = None
116
116
  reader: Any = None
117
+ _TAG: str = "DeviceClient"
117
118
  @property
118
119
  def connect_and_login(self) -> bool:
119
120
  return self._connect_and_login
@@ -138,9 +139,11 @@ class DeviceClient(object):
138
139
  self.password = device.get(CONF_PASSWORD)
139
140
  self.device_id = device.get(CONF_ID)
140
141
  self._simpleVersion = device.get("simpleVersion")
142
+ self._TAG = f"{self.device_id}";
143
+ _LOGGER.warning(f"{self._TAG}:{device}")
141
144
 
142
145
  async def connect(self, ip_address) -> None:
143
- _LOGGER.info(f"connect device : {ip_address}")
146
+ _LOGGER.warning(f"{self._TAG}:connect device: {ip_address}")
144
147
  self.reader = self.writer = None
145
148
  self._connecting = True
146
149
  try:
@@ -208,34 +211,20 @@ class DeviceClient(object):
208
211
  try:
209
212
  self.writer.write(self.get_send_packet(json.dumps(message).encode(), 1))
210
213
  await self.writer.drain()
211
- data = await self.reader.read(1024)
212
- except (BrokenPipeError, ConnectionResetError) as e:
213
- _LOGGER.error(f"{self.device_id} login read status error {e}")
214
- except Exception as e:
215
- _LOGGER.error(f"recv data error {e}")
216
-
217
- data_len = len(data)
218
- if data_len <= 0:
219
- return
220
-
221
- try:
222
- magic, msgtype, bodysize = struct.unpack(">HHI", data[:8])
223
- encrypted_data = data[8:]
224
- if self.aes_key is not None:
225
- decrypted_data = aes_decrypt(encrypted_data, self.aes_key)
226
- else:
227
- decrypted_data = encrypted_data
228
-
214
+ header = await self.reader.readexactly(8)
215
+ magic, msgtype, bodysize = struct.unpack(">HHI", header)
216
+ body = await self.reader.readexactly(bodysize)
217
+ decrypted_data = aes_decrypt(body, self.aes_key) if self.aes_key else body
229
218
  json_data = json.loads(decrypted_data)
230
219
  code = json_data[CONF_ACK][CONF_CODE]
220
+
231
221
  if code != 200:
232
222
  # 登录失败
233
- _LOGGER.error(f"{self.device_id} login error, code: {code}")
223
+ _LOGGER.error(f"{self._TAG}:login error, code: {code}")
234
224
  await self.reset()
235
225
  return
236
226
 
237
- self.ascNumber = json_data[CONF_PAYLOAD][CONF_ASCNUMBER]
238
- self.ascNumber += 1
227
+ self.ascNumber = json_data[CONF_PAYLOAD][CONF_ASCNUMBER] + 1
239
228
  self.status.online = True
240
229
  self._receive_task = asyncio.create_task(
241
230
  self.receive_data(),
@@ -247,38 +236,35 @@ class DeviceClient(object):
247
236
  self._reconnect_handle.cancel()
248
237
  self._reconnect_handle = None
249
238
  self._schedule_ping()
250
- _LOGGER.info(f"connect device success: {self._ip_address}")
239
+ _LOGGER.warning(f"{self._TAG}:connect success: {self._ip_address}")
251
240
  await self.send_action({}, "getDevAttrReq")
252
- except Exception as e:
253
- _LOGGER.error(f"connect device error : {e}")
254
- return
241
+ except (BrokenPipeError, ConnectionResetError, Exception) as e:
242
+ _LOGGER.error(f"{self.device_id} login read status error {e}")
255
243
 
256
244
  # TCP容易拼包,需要谨慎处理
257
245
  async def receive_data(self) -> None:
258
246
  while True:
259
247
  try:
260
- # 先读取 8 字节头
261
248
  header = await self.reader.readexactly(8)
262
249
  magic, msgtype, bodysize = struct.unpack(">HHI", header)
263
-
264
- # 再读取 body
250
+ self.ping_count = 0 #有读到数据就把ping清零
265
251
  body = await self.reader.readexactly(bodysize)
266
-
267
- # 解密
268
252
  decrypted_data = aes_decrypt(body, self.aes_key)
269
253
  json_data = json.loads(decrypted_data)
270
- # _LOGGER.info(f"reveive_data : {json_data}")
254
+ _LOGGER.warning(f"{self._TAG}:reveive_data : {json_data}")
271
255
  except asyncio.CancelledError:
272
- _LOGGER.debug("Receive task cancelled")
256
+ _LOGGER.debug(f"{self._TAG}:Receive task cancelled")
273
257
  raise
274
258
  except (BrokenPipeError, ConnectionResetError, asyncio.IncompleteReadError) as e:
275
- _LOGGER.error(f"{self.device_id} read status error {e}")
276
- await self.reset()
277
- self.status.online = False
278
- self._notify_status_update()
259
+ _LOGGER.error(f"{self._TAG}:read status error {e}")
260
+ # await self.reset()
261
+ syncio.get_running_loop().call_soon(
262
+ lambda: asyncio.create_task(self.reset())
263
+ )
279
264
  return
280
265
  except Exception as e:
281
- _LOGGER.error(f"recv error: {e}")
266
+ _LOGGER.error(f"{self._TAG}:recv error: {e}")
267
+ self.ping_count = 0
282
268
  continue
283
269
 
284
270
  if "service" in json_data:
@@ -295,7 +281,7 @@ class DeviceClient(object):
295
281
  def _schedule_ping(self):
296
282
  loop = asyncio.get_running_loop()
297
283
  loop.create_task(self.send_ping_action())
298
- self._ping_timer = loop.call_later(10, self._schedule_ping)
284
+ self._ping_timer = loop.call_later(30, self._schedule_ping)
299
285
 
300
286
  async def send_dev_attr(self, dev_attr) -> None:
301
287
  if not self._connect_and_login:
@@ -364,10 +350,10 @@ class DeviceClient(object):
364
350
  self.writer.write(self.get_send_packet(json.dumps(action).encode(), 1))
365
351
  await self.writer.drain()
366
352
  except (BrokenPipeError, ConnectionResetError) as e:
367
- _LOGGER.error(f"{self.device_id} send action error {e}")
353
+ _LOGGER.error(f"{self._TAG}:send action error {e}")
368
354
  await self.reset()
369
355
  except Exception as e:
370
- _LOGGER.error(f"{self.device_id} send action error {e}")
356
+ _LOGGER.error(f"{self._TAG}:send action error {e}")
371
357
 
372
358
  async def send_ping_action(self) -> int:
373
359
  if self._is_close:
@@ -379,11 +365,11 @@ class DeviceClient(object):
379
365
  "srcAddr": "x.xxxxxxx",
380
366
  CONF_PAYLOAD: {},
381
367
  }
382
- _LOGGER.info(f"{self.device_id} send_ping_action {ping}")
368
+ # _LOGGER.info(f"{self.device_id} send_ping_action {ping}")
383
369
  try:
384
- if self.ping_count >= 2:
370
+ if self.ping_count >= 3:
385
371
  _LOGGER.error(
386
- f"Last ping did not return within 20 seconds. device id:{self.device_id}"
372
+ f"{self._TAG}:Device unresponsive within 90 seconds, disconnecting."
387
373
  )
388
374
  await self.reset()
389
375
  return -1
@@ -12,7 +12,7 @@ from .exceptions import AidotOSError
12
12
  _LOGGER = logging.getLogger(__name__)
13
13
  # _DISCOVER_TIME = 15
14
14
 
15
- _DISCOVER_FAST = 5 # 启动时快速发现
15
+ _DISCOVER_FAST = 10 # 启动时快速发现
16
16
  _DISCOVER_SLOW = 120 # 稳定后慢速维持
17
17
 
18
18
  class BroadcastProtocol:
@@ -111,7 +111,7 @@ class Discover:
111
111
 
112
112
  def start_repeat_broadcast(self) -> None:
113
113
  self._is_close = False
114
- self._fast_discover_count = 6 # 前6次快速(30秒内)
114
+ self._fast_discover_count = 3 # 前三次快速
115
115
  self._schedule_broadcast()
116
116
 
117
117
  def _schedule_broadcast(self) -> None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-aidot
3
- Version: 0.3.46
3
+ Version: 0.3.48
4
4
  Summary: aidot control wifi lights
5
5
  Home-page: https://github.com/Aidot-Development-Team/python-aidot
6
6
  Author: aidotdev2024
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setuptools.setup(
7
7
  name="python-aidot",
8
- version="0.3.46",
8
+ version="0.3.48",
9
9
  author="aidotdev2024",
10
10
  url='https://github.com/Aidot-Development-Team/python-aidot',
11
11
  description="aidot control wifi lights",
File without changes
File without changes
File without changes