fix: change the address to the config, update some logic, seperate the api endpoint, renaming things. yes, the tests don't work right now- this shouldn't be merged yet.

ref: N25B-334
This commit is contained in:
Björn Otgaar
2025-12-15 11:35:56 +01:00
parent 2e472ea292
commit daf31ac6a6
4 changed files with 45 additions and 25 deletions

View File

@@ -16,14 +16,40 @@ logger = logging.getLogger(__name__)
router = APIRouter()
@router.post("/command", status_code=202)
async def receive_command(command: SpeechCommand, request: Request):
@router.post("/command/speech", status_code=202)
async def receive_command_speech(command: SpeechCommand, request: Request):
"""
Send a direct speech command to the robot.
Publishes the command to the internal 'command' topic. The
:class:`~control_backend.agents.actuation.robot_speech_agent.RobotSpeechAgent`
or
will forward this to the robot.
:param command: The speech command payload.
:param request: The FastAPI request object.
"""
# Validate and retrieve data.
try:
validated = SpeechCommand.model_validate(command)
except ValidationError as e:
raise HTTPException(
status_code=422, detail=f"Payload is not valid SpeechCommand: {e}"
) from e
topic = b"command"
pub_socket: Socket = request.app.state.endpoints_pub_socket
await pub_socket.send_multipart([topic, validated.model_dump_json().encode()])
return {"status": "Speech command received"}
@router.post("/command/gesture", status_code=202)
async def receive_command_gesture(command: GestureCommand, request: Request):
"""
Send a direct gesture command to the robot.
Publishes the command to the internal 'command' topic. The
:class:`~control_backend.agents.actuation.robot_speech_agent.RobotGestureAgent`
will forward this to the robot.
@@ -31,23 +57,19 @@ async def receive_command(command: SpeechCommand, request: Request):
:param request: The FastAPI request object.
"""
# Validate and retrieve data.
validated = None
valid_commands = (GestureCommand, SpeechCommand)
for command_model in valid_commands:
try:
validated = command_model.model_validate(command)
except ValidationError:
continue
if validated is None:
raise HTTPException(status_code=422, detail="Payload is not valid for command models")
try:
validated = GestureCommand.model_validate(command)
except ValidationError as e:
raise HTTPException(
status_code=422, detail=f"Payload is not valid GestureCommand: {e}"
) from e
topic = b"command"
pub_socket: Socket = request.app.state.endpoints_pub_socket
await pub_socket.send_multipart([topic, validated.model_dump_json().encode()])
return {"status": "Command received"}
return {"status": "Gesture command received"}
@router.get("/ping_check")
@@ -58,8 +80,8 @@ async def ping(request: Request):
pass
@router.get("/get_available_gesture_tags")
async def get_available_gesture_tags(request: Request):
@router.get("/commands/gesture/tags")
async def get_available_gesture_tags(request: Request, count=0):
"""
Endpoint to retrieve the available gesture tags for the robot.
@@ -67,10 +89,10 @@ async def get_available_gesture_tags(request: Request):
:return: A list of available gesture tags.
"""
req_socket = Context.instance().socket(zmq.REQ)
req_socket.connect("tcp://localhost:7788")
req_socket.connect(request.app.state.gesture_rep_address)
# TODO: Implement a way to get a certain ammount from the UI, rather than everything.
amount = None
# Check to see if we've got any count given in the query parameter
amount = count or None
timeout = 5 # seconds
await req_socket.send(f"{amount}".encode() if amount else b"None")