add thread for callback receiving
This commit is contained in:
parent
14e16cc6e8
commit
9374384e74
1 changed files with 43 additions and 11 deletions
54
utils.py
54
utils.py
|
@ -4,6 +4,9 @@ import socket
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import threading
|
||||||
|
import queue
|
||||||
|
|
||||||
DEBUG: bool = True
|
DEBUG: bool = True
|
||||||
RESET = "\033[0m"
|
RESET = "\033[0m"
|
||||||
CYAN = "\033[36m"
|
CYAN = "\033[36m"
|
||||||
|
@ -38,22 +41,51 @@ class RobotClient:
|
||||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.sock.connect((self.host, self.port))
|
self.sock.connect((self.host, self.port))
|
||||||
|
|
||||||
|
self._recv_thread = threading.Thread(target=self._recv_loop, daemon=True)
|
||||||
|
self._response_queue = queue.Queue()
|
||||||
|
self._callback_queue = queue.Queue()
|
||||||
|
self._recv_thread.start()
|
||||||
|
|
||||||
|
|
||||||
|
def _recv_loop(self):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
data = self.sock.recv(4096).decode("utf-8")
|
||||||
|
if not data:
|
||||||
|
if DEBUG:
|
||||||
|
print("DEBUG: socket closed by server")
|
||||||
|
break
|
||||||
|
ret = json.loads(data)
|
||||||
|
if DEBUG:
|
||||||
|
print(f"{YELLOW}DEBUG: received:{RESET} {ret}")
|
||||||
|
if ret["ret"] == RobotJasonServerError.RESUTL_CALL_BACK:
|
||||||
|
self._callback_queue.put(ret)
|
||||||
|
if DEBUG:
|
||||||
|
print(f"{YELLOW}DEBUG: received callback event: {ret['msg']}")
|
||||||
|
else:
|
||||||
|
self._response_queue.put(ret)
|
||||||
|
except Exception as e:
|
||||||
|
if DEBUG:
|
||||||
|
print(f"{RED}DEBUG: Exception in recv loop: {e}{RESET}")
|
||||||
|
break
|
||||||
|
|
||||||
def _send(self, cmd) -> dict:
|
def _send(self, cmd) -> dict:
|
||||||
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"))
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
data = self.sock.recv(4096).decode("utf-8")
|
try:
|
||||||
ret = json.loads(data)
|
ret = self._response_queue.get(timeout=5)
|
||||||
if DEBUG:
|
if ret:
|
||||||
print(f"{YELLOW}DEBUG: received:{RESET} {ret}")
|
return ret
|
||||||
if ret["ret"] == RobotJasonServerError.RESUTL_CALL_BACK:
|
except queue.Empty:
|
||||||
if DEBUG:
|
raise RuntimeError("Timeout waiting for response")
|
||||||
print(f"{YELLOW}DEBUG: received callback event (ignored as response): {ret['msg']}")
|
|
||||||
continue
|
def get_callback_event(self, timeout=0.1):
|
||||||
else:
|
try:
|
||||||
return ret
|
return self._callback_queue.get(timeout=timeout)
|
||||||
|
except queue.Empty:
|
||||||
|
return None
|
||||||
|
|
||||||
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