pykirdy: Update handling of json objects received

- Accept new line delimited json in read_response()
This commit is contained in:
linuswck 2024-03-25 12:40:46 +08:00
parent b1a1173075
commit a13c6fa86c
1 changed files with 15 additions and 9 deletions

View File

@ -622,20 +622,26 @@ class Kirdy:
self._reader = None self._reader = None
self._writer = None self._writer = None
async def _read_response(self): async def _read_response(self, buffer_size=16384):
"""
Decode newline delimited Json objects and return the latest json received inside the buffer.
- buffer_size: Integer
"""
try: try:
response = await asyncio.wait_for(self._reader.read(1024), self.timeout) raw_response = await asyncio.wait_for(self._reader.read(buffer_size), self.timeout)
except asyncio.exceptions.TimeoutError: except asyncio.exceptions.TimeoutError:
return { return {
"msg_type": "Internal Timeout" "msg_type": "Internal Timeout"
} }
response = response.decode('utf-8', errors='ignore')
try: response = raw_response.decode('utf-8', errors='ignore').split("\n")
return json.loads(response) for item in reversed(response):
except json.decoder.JSONDecodeError: try:
return { return json.loads(item)
"msg_type": "Internal Invalid" except json.decoder.JSONDecodeError:
} pass
return { "msg_type": "Internal No json object found in response" }
# If the cmd involves a cmd specific data type, # If the cmd involves a cmd specific data type,
# checking is done separately within the functions being called # checking is done separately within the functions being called