add full received json
This commit is contained in:
parent
166baa08c9
commit
a564b91a1e
1 changed files with 21 additions and 6 deletions
21
utils.py
21
utils.py
|
@ -7,7 +7,7 @@ DEBUG: bool = True
|
||||||
RESET = "\033[0m"
|
RESET = "\033[0m"
|
||||||
CYAN = "\033[36m"
|
CYAN = "\033[36m"
|
||||||
YELLOW = "\033[33m"
|
YELLOW = "\033[33m"
|
||||||
|
RED = "\033[31m"
|
||||||
|
|
||||||
class RobotClient:
|
class RobotClient:
|
||||||
def __init__(self, host, port):
|
def __init__(self, host, port):
|
||||||
|
@ -20,11 +20,26 @@ class RobotClient:
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
print(f"{CYAN}DEBUG: sended:{RESET} {cmd}")
|
print(f"{CYAN}DEBUG: sended:{RESET} {cmd}")
|
||||||
self.sock.sendall(json.dumps(cmd).encode("utf-8"))
|
self.sock.sendall(json.dumps(cmd).encode("utf-8"))
|
||||||
data = self.sock.recv(1024).decode("utf-8")
|
try:
|
||||||
ret = json.loads(data)
|
ret = self._recv_full_json()
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
print(f"{YELLOW}DEBUG: received:{RESET} {ret}")
|
print(f"{YELLOW}DEBUG: received:{RESET} {ret}")
|
||||||
return ret
|
return ret
|
||||||
|
except Exception as e:
|
||||||
|
print(f"{RED}ERROR: {e}{RESET}")
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def _recv_full_json(self) -> dict:
|
||||||
|
buffer = ""
|
||||||
|
while True:
|
||||||
|
chunk = self.sock.recv(1024).decode("utf-8")
|
||||||
|
if not chunk:
|
||||||
|
raise ConnectionError("Connection closed")
|
||||||
|
buffer += chunk
|
||||||
|
try:
|
||||||
|
return json.loads(buffer)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
continue # not полный JSON, продолжаем читать
|
||||||
|
|
||||||
def rpy_to_quat(self, arr):
|
def rpy_to_quat(self, arr):
|
||||||
cmd = {"command": "rpy_to_quaternion", "rpy": arr}
|
cmd = {"command": "rpy_to_quaternion", "rpy": arr}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue