feat: fixed socket typing for communication agent and ping router- automatically try to reconnect with robot.

ref: N25B-151
This commit is contained in:
Björn Otgaar
2025-10-29 21:55:23 +01:00
parent 669d0190d6
commit 4f2d45fb44
3 changed files with 47 additions and 28 deletions

View File

@@ -6,7 +6,7 @@ import zmq.asyncio
import json
import datetime
from zmq import Socket
from zmq.asyncio import Socket
from control_backend.core.zmq_context import context
from control_backend.core.config import settings
from control_backend.schemas.ri_message import SpeechCommand, RIEndpoint
@@ -24,7 +24,7 @@ async def receive_command(command: SpeechCommand, request: Request):
# Validate and retrieve data.
SpeechCommand.model_validate(command)
topic = b"command"
pub_socket: Socket = request.app.state.internal_comm_socket
pub_socket : Socket = request.app.state.internal_comm_socket
pub_socket.send_multipart([topic, command.model_dump_json().encode()])
return {"status": "Command received"}
@@ -41,7 +41,8 @@ async def ping_stream(request: Request):
async def event_stream():
# Set up internal socket to receive ping updates
logger.debug("Ping stream router event stream entered.")
sub_socket = zmq.asyncio.Context().socket(zmq.SUB)
sub_socket = context.socket(zmq.SUB)
sub_socket.connect(settings.zmq_settings.internal_comm_address)
sub_socket.setsockopt(zmq.SUBSCRIBE, b"ping")
connected = True
@@ -69,6 +70,5 @@ async def ping_stream(request: Request):
logger.debug(f"Yielded new connection event in robot ping router: {str(connected)}")
yield f"data: {str(connected)}, time:{str(datetime.datetime.now().strftime("%H:%M:%S"))}\n\n"
return StreamingResponse(event_stream(), media_type="text/event-stream")