feat: single gestures are forwarded properly to ui

ref: N25B-399
This commit is contained in:
JobvAlewijk
2025-12-29 19:23:10 +01:00
parent 8cfd59c14b
commit 3571bd614f
5 changed files with 180 additions and 270 deletions

View File

@@ -27,16 +27,22 @@ class RobotGestureAgent(BaseAgent):
pubsocket: azmq.Socket
address = ""
bind = False
gesture_data = []
gesture_tags = []
gesture_basic = []
gesture_single = []
def __init__(
self,
name: str,
address=settings.zmq_settings.ri_command_address,
bind=False,
gesture_data=None,
gesture_tags=None,
gesture_basic=None,
gesture_single=None,
):
self.gesture_data = gesture_data or []
self.gesture_tags = gesture_tags or []
self.gesture_basic = gesture_basic or []
self.gesture_single = gesture_single or []
super().__init__(name)
self.address = address
self.bind = bind
@@ -92,13 +98,14 @@ class RobotGestureAgent(BaseAgent):
"""
try:
gesture_command = GestureCommand.model_validate_json(msg.body)
if gesture_command.endpoint == RIEndpoint.GESTURE_TAG:
if gesture_command.data not in self.gesture_data:
self.logger.warning(
"Received gesture tag '%s' which is not in available tags. Early returning",
gesture_command.data,
)
return
# if gesture_command.endpoint == RIEndpoint.GESTURE_TAG:
# if gesture_command.data not in self.gesture_data:
# self.logger.warning(
# "Received gesture tag '%s' which is not in available tags.
# Early returning",
# gesture_command.data,
# )
# return
await self.pubsocket.send_json(gesture_command.model_dump())
except Exception:
@@ -134,29 +141,39 @@ class RobotGestureAgent(BaseAgent):
async def _fetch_gestures_loop(self):
"""
Loop to handle fetching gestures received via ZMQ (e.g., from the UI).
Listens on the 'send_gestures' topic, and returns a list on the get_gestures topic.
REP socket handler for gesture queries.
Supports:
- tags
- basic_gestures
- single_gestures
"""
while self._running:
try:
# Get a request
body = await self.repsocket.recv()
req = await self.repsocket.recv_json()
# Figure out amount, if specified
try:
body = json.loads(body)
except json.JSONDecodeError:
body = None
req_type = req.get("type")
amount = req.get("count")
amount = None
if isinstance(body, int):
amount = body
if req_type == "tags":
data = self.gesture_tags
key = "tags"
# Fetch tags from gesture data and respond
tags = self.gesture_data[:amount] if amount else self.gesture_data
response = json.dumps({"tags": tags}).encode()
await self.repsocket.send(response)
elif req_type == "basic":
data = self.gesture_basic
key = "basic_gestures"
elif req_type == "single":
data = self.gesture_single
key = "single_gestures"
else:
await self.repsocket.send_json({})
continue
if amount:
data = data[:amount]
await self.repsocket.send_json({key: data})
except Exception:
self.logger.exception("Error fetching gesture tags.")
self.logger.exception("Error fetching gestures.")