forked from M-Labs/kirdy
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:
parent
27bf573010
commit
e632cbbfdd
|
@ -806,21 +806,21 @@ 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:
|
||||||
if self._report_sig is None:
|
if self._report_sig is None:
|
||||||
self._report_queue.put_nowait_overwrite(response)
|
self._report_queue.put_nowait_overwrite(response)
|
||||||
else:
|
|
||||||
self._report_sig.emit(response)
|
|
||||||
else:
|
else:
|
||||||
if self._msg_queue_get_report:
|
self._report_sig.emit(response)
|
||||||
self._msg_queue_get_report = False
|
else:
|
||||||
self._int_msg_queue.put_nowait_overwrite(response)
|
if self._msg_queue_get_report:
|
||||||
|
self._msg_queue_get_report = False
|
||||||
|
self._int_msg_queue.put_nowait_overwrite(response)
|
||||||
except asyncio.exceptions.CancelledError:
|
except asyncio.exceptions.CancelledError:
|
||||||
pass
|
pass
|
||||||
except (TimeoutError, ConnectionResetError, ConnectionError) as exec:
|
except (TimeoutError, ConnectionResetError, ConnectionError) as exec:
|
||||||
|
@ -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():
|
||||||
|
|
Loading…
Reference in New Issue