diff --git a/src/control_backend/agents/actuation/robot_gesture_agent.py b/src/control_backend/agents/actuation/robot_gesture_agent.py index 1741899..6830874 100644 --- a/src/control_backend/agents/actuation/robot_gesture_agent.py +++ b/src/control_backend/agents/actuation/robot_gesture_agent.py @@ -23,6 +23,7 @@ class RobotGestureAgent(BaseAgent): """ subsocket: azmq.Socket + repsocket: azmq.Socket pubsocket: azmq.Socket address = "" bind = False @@ -56,9 +57,8 @@ class RobotGestureAgent(BaseAgent): context = azmq.Context.instance() # To the robot - self.pubsocket = context.socket(zmq.PUB) - if self.bind: # TODO: Should this ever be the case? + if self.bind: self.pubsocket.bind(self.address) else: self.pubsocket.connect(self.address) @@ -69,6 +69,10 @@ class RobotGestureAgent(BaseAgent): self.subsocket.setsockopt(zmq.SUBSCRIBE, b"command") self.subsocket.setsockopt(zmq.SUBSCRIBE, b"send_gestures") + # REP socket for replying to gesture requests + self.repsocket = context.socket(zmq.REP) + self.repsocket.bind("tcp://localhost:7788") + self.add_behavior(self._zmq_command_loop()) self.add_behavior(self._fetch_gestures_loop()) @@ -92,7 +96,7 @@ 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.availableTags(): + 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, @@ -120,7 +124,7 @@ class RobotGestureAgent(BaseAgent): body = json.loads(body) gesture_command = GestureCommand.model_validate(body) if gesture_command.endpoint == RIEndpoint.GESTURE_TAG: - if gesture_command.data not in self.availableTags(): + if gesture_command.data not in self.gesture_data: self.logger.warning( "Received gesture tag '%s' which is not in available tags.\ Early returning", @@ -139,157 +143,23 @@ class RobotGestureAgent(BaseAgent): """ while self._running: try: - topic, body = await self.subsocket.recv_multipart() - - # Don't process commands here - if topic != b"send_gestures": - continue + # Get a request + body = await self.repsocket.recv() + # Figure out amount, if specified try: body = json.loads(body) except json.JSONDecodeError: body = None - # We could have the body be the nummer of gestures you want to fetch or something. amount = None if isinstance(body, int): amount = body - tags = self.availableTags()[:amount] if amount else self.availableTags() + # 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.pubsocket.send_multipart( - [ - b"get_gestures", - response, - ] - ) + await self.repsocket.send(response) except Exception: self.logger.exception("Error fetching gesture tags.") - - def availableTags(self): - """ - Returns the available gesture tags. - - :return: List of available gesture tags. - """ - return [ - "above", - "affirmative", - "afford", - "agitated", - "all", - "allright", - "alright", - "any", - "assuage", - "assuage", - "attemper", - "back", - "bashful", - "beg", - "beseech", - "blank", - "body language", - "bored", - "bow", - "but", - "call", - "calm", - "choose", - "choice", - "cloud", - "cogitate", - "cool", - "crazy", - "disappointed", - "down", - "earth", - "empty", - "embarrassed", - "enthusiastic", - "entire", - "estimate", - "except", - "exalted", - "excited", - "explain", - "far", - "field", - "floor", - "forlorn", - "friendly", - "front", - "frustrated", - "gentle", - "gift", - "give", - "ground", - "happy", - "hello", - "her", - "here", - "hey", - "hi", - "him", - "hopeless", - "hysterical", - "I", - "implore", - "indicate", - "joyful", - "me", - "meditate", - "modest", - "negative", - "nervous", - "no", - "not know", - "nothing", - "offer", - "ok", - "once upon a time", - "oppose", - "or", - "pacify", - "pick", - "placate", - "please", - "present", - "proffer", - "quiet", - "reason", - "refute", - "reject", - "rousing", - "sad", - "select", - "shamefaced", - "show", - "show sky", - "sky", - "soothe", - "sun", - "supplicate", - "tablet", - "tall", - "them", - "there", - "think", - "timid", - "top", - "unless", - "up", - "upstairs", - "void", - "warm", - "winner", - "yeah", - "yes", - "yoo-hoo", - "you", - "your", - "zero", - "zestful", - ] diff --git a/src/control_backend/api/v1/endpoints/robot.py b/src/control_backend/api/v1/endpoints/robot.py index c9d93a1..b34e171 100644 --- a/src/control_backend/api/v1/endpoints/robot.py +++ b/src/control_backend/api/v1/endpoints/robot.py @@ -66,23 +66,19 @@ async def get_available_gesture_tags(request: Request): :param request: The FastAPI request object. :return: A list of available gesture tags. """ - sub_socket = Context.instance().socket(zmq.SUB) - sub_socket.connect(settings.zmq_settings.internal_sub_address) - sub_socket.setsockopt(zmq.SUBSCRIBE, b"get_gestures") - - pub_socket: Socket = request.app.state.endpoints_pub_socket - topic = b"send_gestures" + req_socket = Context.instance().socket(zmq.REQ) + req_socket.connect("tcp://localhost:7788") # TODO: Implement a way to get a certain ammount from the UI, rather than everything. amount = None timeout = 5 # seconds - await pub_socket.send_multipart([topic, amount.to_bytes(4, "big") if amount else b""]) + await req_socket.send(f"{amount}".encode() if amount else b"None") try: - _, body = await asyncio.wait_for(sub_socket.recv_multipart(), timeout=timeout) + body = await asyncio.wait_for(req_socket.recv(), timeout=timeout) except TimeoutError: - body = b"tags: []" - logger.debug("got timeout error fetching gestures") + body = '{"tags": []}' + logger.debug("Got timeout error fetching gestures.") # Handle empty response and JSON decode errors available_tags = []