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')
response = raw_response.decode('utf-8', errors='ignore').split("\n")
for item in reversed(response):
try: try:
return json.loads(response) return json.loads(item)
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
return { pass
"msg_type": "Internal Invalid" 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