add callback ignoring

This commit is contained in:
Ilya Uraev 2025-05-22 17:18:19 +03:00
parent ebf5783613
commit 9eaf89faec

View file

@ -18,6 +18,19 @@ class RobotStatus:
Resumed = 3
class RobotJasonServerError:
RESUTL_SUCC = 0
RESUTL_BASE = 3000
RESUTL_ERROR_BAD_COMMAND_STRING = RESUTL_BASE + 1
RESUTL_ERROR_UNKNOWN_COMMAND = RESUTL_BASE + 2
RESUTL_ERROR_ARGS = RESUTL_BASE + 2
RESUTL_ERROR_MOVE = RESUTL_BASE + 3
RESUTL_CALL_BACK = RESUTL_BASE + 4
RESUTL_ERROR_CLIENTS_NUMBER = RESUTL_BASE + 5
RESUTL_ERROR_DISCONNECT_TO_MODBUS_SERVER = RESUTL_BASE + 6
RESUTL_ERROR_CONNECT_TO_MODBUS_SERVER = RESUTL_BASE + 7
class RobotClient:
def __init__(self, host, port):
self.host = host
@ -29,26 +42,18 @@ class RobotClient:
if DEBUG:
print(f"{CYAN}DEBUG: sended:{RESET} {cmd}")
self.sock.sendall(json.dumps(cmd).encode("utf-8"))
try:
ret = self._recv_full_json()
while True:
data = self.sock.recv(4096).decode("utf-8")
ret = json.loads(data)
if DEBUG:
print(f"{YELLOW}DEBUG: received:{RESET} {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(4096).decode("utf-8")
if not chunk:
raise ConnectionError("Connection closed")
buffer += chunk
try:
return json.loads(buffer)
except json.JSONDecodeError:
continue # not полный JSON, продолжаем читать
if ret["ret"] == RobotJasonServerError.RESUTL_CALL_BACK:
if DEBUG:
print(f"{YELLOW}DEBUG: received callback event (ignored as response): {ret['msg']}")
continue
else:
return ret
def rpy_to_quat(self, arr):
cmd = {"command": "rpy_to_quaternion", "rpy": arr}