Use ZMQ's global context instance and setup an XPUB/XSUB proxy intermediary to allow for easier multi-pubs. close: N25B-217
25 lines
724 B
Python
25 lines
724 B
Python
import logging
|
|
|
|
import zmq
|
|
from fastapi import APIRouter, Request
|
|
from zmq.asyncio import Context
|
|
|
|
from control_backend.core.config import settings
|
|
from control_backend.schemas.ri_message import SpeechCommand
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/command", status_code=202)
|
|
async def receive_command(command: SpeechCommand, request: Request):
|
|
# Validate and retrieve data.
|
|
SpeechCommand.model_validate(command)
|
|
topic = b"command"
|
|
pub_socket = Context.instance().socket(zmq.PUB)
|
|
pub_socket.connect(settings.zmq_settings.internal_pub_address)
|
|
await pub_socket.send_multipart([topic, command.model_dump_json().encode()])
|
|
|
|
return {"status": "Command received"}
|