driver: use readuntil to get response msg

- The driver may recv partial ctrl msg, which causes a json decode error
This commit is contained in:
linuswck 2024-09-03 16:49:46 +08:00
parent 27bf573010
commit e632cbbfdd
1 changed files with 16 additions and 21 deletions

View File

@ -806,10 +806,10 @@ class Kirdy:
try: try:
while True: while True:
if self._report_mode_on: if self._report_mode_on:
responses = await asyncio.wait_for(self._read_response(), self._timeout) response = await asyncio.wait_for(self._read_response(), self._timeout)
else: else:
responses = await self._read_response() response = await self._read_response()
for response in responses:
if response["msg_type"] == 'HardReset': if response["msg_type"] == 'HardReset':
raise asyncio.exceptions.CancelledError raise asyncio.exceptions.CancelledError
if response["msg_type"] == 'Report' and not self._msg_queue_get_report: if response["msg_type"] == 'Report' and not self._msg_queue_get_report:
@ -860,18 +860,13 @@ class Kirdy:
writer_sock = self._writer.get_extra_info("socket") writer_sock = self._writer.get_extra_info("socket")
writer_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) writer_sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
async def _read_response(self, buffer_size=16384): async def _read_response(self):
raw_response = b'' raw_response = b''
while len(raw_response) == 0: while len(raw_response) == 0:
# Ignore 0 size packet # Ignore 0 size packet
raw_response = await self._reader.read(buffer_size) raw_response = await self._reader.readuntil()
response = raw_response.decode('utf-8', errors='ignore').split("\n") response = raw_response.decode('utf-8', errors='ignore').split("\n")
response = response[:-1] return json.loads(response[0])
items = []
for item in response:
items.append(json.loads(item))
return items
async def _send_raw_cmd(self, cmd, msg_type="Acknowledge", sig=None): async def _send_raw_cmd(self, cmd, msg_type="Acknowledge", sig=None):
if self.connected(): if self.connected():