driver: Handle CancelledError for send_cmd handler

This commit is contained in:
linuswck 2024-04-19 15:39:29 +08:00
parent 2df22d5dcb
commit 5b35f32a42
1 changed files with 20 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import socket import socket
import asyncio import asyncio
import json import json
import logging
# Data Type Enums # Data Type Enums
IP_SETTINGS = "ip_settings" IP_SETTINGS = "ip_settings"
@ -18,6 +19,9 @@ class CmdDoesNotExist(Exception):
class InvalidDataType(Exception): class InvalidDataType(Exception):
pass pass
class InvalidCmd(Exception):
pass
class NoAckRecv(Exception): class NoAckRecv(Exception):
pass pass
@ -661,14 +665,19 @@ class Kirdy:
async def _send_raw_cmd(self, cmd, msg_type): async def _send_raw_cmd(self, cmd, msg_type):
retry = 0 retry = 0
while retry < 10: while retry < 10:
try:
self._writer.write(bytes(json.dumps(cmd), "UTF-8")) self._writer.write(bytes(json.dumps(cmd), "UTF-8"))
await self._writer.drain() await self._writer.drain()
response = await self._read_response() response = await self._read_response()
try:
if response["msg_type"] == msg_type: if response["msg_type"] == msg_type:
return response return response
except: if response["msg_type"] == "InvalidCmd":
return InvalidCmd
except asyncio.exceptions.CancelledError:
return None
except Exception as e:
retry += 1 retry += 1
logging.error(f"_send_raw_cmd Exception: {e}")
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
raise NoAckRecv raise NoAckRecv
@ -704,14 +713,17 @@ class Kirdy:
retry = 0 retry = 0
while retry < 10: while retry < 10:
try:
self._writer.write(bytes(json.dumps(cmd_dict), "UTF-8")) self._writer.write(bytes(json.dumps(cmd_dict), "UTF-8"))
await self._writer.drain() await self._writer.drain()
response = await self._read_response() response = await self._read_response()
try:
if response["msg_type"] == msg_type: if response["msg_type"] == msg_type:
return response return response
except: except asyncio.exceptions.CancelledError:
return None
except Exception as e:
retry += 1 retry += 1
logging.error(f"_send_cmd Exception: {e}")
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
raise NoAckRecv raise NoAckRecv