Gestures in the CB. #36

Merged
9828273 merged 16 commits from feat/cb2ri-gestures into dev 2025-12-16 09:24:26 +00:00
2 changed files with 20 additions and 154 deletions
Showing only changes of commit 6034263259 - Show all commits

View File

@@ -23,6 +23,7 @@ class RobotGestureAgent(BaseAgent):
""" """
subsocket: azmq.Socket subsocket: azmq.Socket
repsocket: azmq.Socket
pubsocket: azmq.Socket pubsocket: azmq.Socket
address = "" address = ""
bind = False bind = False
@@ -56,9 +57,8 @@ class RobotGestureAgent(BaseAgent):
context = azmq.Context.instance() context = azmq.Context.instance()
# To the robot # To the robot
self.pubsocket = context.socket(zmq.PUB) self.pubsocket = context.socket(zmq.PUB)
if self.bind: # TODO: Should this ever be the case? if self.bind:
self.pubsocket.bind(self.address) self.pubsocket.bind(self.address)
else: else:
self.pubsocket.connect(self.address) 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"command")
self.subsocket.setsockopt(zmq.SUBSCRIBE, b"send_gestures") 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._zmq_command_loop())
self.add_behavior(self._fetch_gestures_loop()) self.add_behavior(self._fetch_gestures_loop())
@@ -92,7 +96,7 @@ class RobotGestureAgent(BaseAgent):
try: try:
gesture_command = GestureCommand.model_validate_json(msg.body) gesture_command = GestureCommand.model_validate_json(msg.body)
if gesture_command.endpoint == RIEndpoint.GESTURE_TAG: 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( self.logger.warning(
"Received gesture tag '%s' which is not in available tags. Early returning", "Received gesture tag '%s' which is not in available tags. Early returning",
gesture_command.data, gesture_command.data,
@@ -120,7 +124,7 @@ class RobotGestureAgent(BaseAgent):
body = json.loads(body) body = json.loads(body)
gesture_command = GestureCommand.model_validate(body) gesture_command = GestureCommand.model_validate(body)
if gesture_command.endpoint == RIEndpoint.GESTURE_TAG: 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( self.logger.warning(
"Received gesture tag '%s' which is not in available tags.\ "Received gesture tag '%s' which is not in available tags.\
Early returning", Early returning",
@@ -139,157 +143,23 @@ class RobotGestureAgent(BaseAgent):
""" """
while self._running: while self._running:
try: try:
topic, body = await self.subsocket.recv_multipart() # Get a request
body = await self.repsocket.recv()
# Don't process commands here
if topic != b"send_gestures":
continue
# Figure out amount, if specified
try: try:
body = json.loads(body) body = json.loads(body)
except json.JSONDecodeError: except json.JSONDecodeError:
body = None body = None
# We could have the body be the nummer of gestures you want to fetch or something.
amount = None amount = None
if isinstance(body, int): if isinstance(body, int):
amount = body 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() response = json.dumps({"tags": tags}).encode()
await self.repsocket.send(response)
await self.pubsocket.send_multipart(
[
b"get_gestures",
response,
]
)
except Exception: except Exception:
self.logger.exception("Error fetching gesture tags.") 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",
]

View File

@@ -66,23 +66,19 @@ async def get_available_gesture_tags(request: Request):
:param request: The FastAPI request object. :param request: The FastAPI request object.
:return: A list of available gesture tags. :return: A list of available gesture tags.
""" """
sub_socket = Context.instance().socket(zmq.SUB) req_socket = Context.instance().socket(zmq.REQ)
sub_socket.connect(settings.zmq_settings.internal_sub_address) req_socket.connect("tcp://localhost:7788")
sub_socket.setsockopt(zmq.SUBSCRIBE, b"get_gestures")
pub_socket: Socket = request.app.state.endpoints_pub_socket
topic = b"send_gestures"
# TODO: Implement a way to get a certain ammount from the UI, rather than everything. # TODO: Implement a way to get a certain ammount from the UI, rather than everything.
amount = None amount = None
timeout = 5 # seconds 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: try:
_, body = await asyncio.wait_for(sub_socket.recv_multipart(), timeout=timeout) body = await asyncio.wait_for(req_socket.recv(), timeout=timeout)
except TimeoutError: except TimeoutError:
body = b"tags: []" body = '{"tags": []}'
logger.debug("got timeout error fetching gestures") logger.debug("Got timeout error fetching gestures.")
# Handle empty response and JSON decode errors # Handle empty response and JSON decode errors
available_tags = [] available_tags = []