fix: unit tests fixes and ruff formating
N25B-205
This commit is contained in:
@@ -10,13 +10,22 @@ from control_backend.schemas.ri_message import SpeechCommand
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RICommandAgent(Agent):
|
||||
subsocket: zmq.Socket
|
||||
pubsocket: zmq.Socket
|
||||
address = ""
|
||||
bind = False
|
||||
|
||||
def __init__(self, jid: str, password: str, port: int = 5222, verify_security: bool = False, address = "tcp://localhost:0000", bind = False):
|
||||
def __init__(
|
||||
self,
|
||||
jid: str,
|
||||
password: str,
|
||||
port: int = 5222,
|
||||
verify_security: bool = False,
|
||||
address="tcp://localhost:0000",
|
||||
bind=False,
|
||||
):
|
||||
super().__init__(jid, password, port, verify_security)
|
||||
self.address = address
|
||||
self.bind = bind
|
||||
@@ -29,12 +38,12 @@ class RICommandAgent(Agent):
|
||||
assert self.agent is not None
|
||||
# Get a message internally (with topic command)
|
||||
topic, body = await self.agent.subsocket.recv_multipart()
|
||||
|
||||
|
||||
# Try to get body
|
||||
try:
|
||||
body = json.loads(body)
|
||||
message = SpeechCommand.model_validate(body)
|
||||
|
||||
|
||||
# Send to the robot.
|
||||
await self.agent.pubsocket.send_json(message.model_dump())
|
||||
except Exception as e:
|
||||
@@ -48,11 +57,11 @@ class RICommandAgent(Agent):
|
||||
|
||||
# To the robot
|
||||
self.pubsocket = context.socket(zmq.PUB)
|
||||
if self.bind:
|
||||
if self.bind:
|
||||
self.pubsocket.bind(self.address)
|
||||
else :
|
||||
else:
|
||||
self.pubsocket.connect(self.address)
|
||||
|
||||
|
||||
# Receive internal topics regarding commands
|
||||
self.subsocket = context.socket(zmq.SUB)
|
||||
self.subsocket.connect(settings.zmq_settings.internal_comm_address)
|
||||
|
||||
@@ -12,14 +12,21 @@ from control_backend.agents.ri_command_agent import RICommandAgent
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RICommunicationAgent(Agent):
|
||||
req_socket: zmq.Socket
|
||||
_address = ""
|
||||
_bind = True
|
||||
|
||||
def __init__(self, jid: str, password: str, port: int = 5222,
|
||||
verify_security: bool = False, address = "tcp://localhost:0000",
|
||||
bind = False):
|
||||
def __init__(
|
||||
self,
|
||||
jid: str,
|
||||
password: str,
|
||||
port: int = 5222,
|
||||
verify_security: bool = False,
|
||||
address="tcp://localhost:0000",
|
||||
bind=False,
|
||||
):
|
||||
super().__init__(jid, password, port, verify_security)
|
||||
self._address = address
|
||||
self._bind = bind
|
||||
@@ -37,28 +44,26 @@ class RICommunicationAgent(Agent):
|
||||
|
||||
# Wait up to three seconds for a reply:)
|
||||
try:
|
||||
message = await asyncio.wait_for(
|
||||
self.agent.req_socket.recv_json(),
|
||||
timeout=3.0)
|
||||
message = await asyncio.wait_for(self.agent.req_socket.recv_json(), timeout=3.0)
|
||||
|
||||
# We didnt get a reply :(
|
||||
except asyncio.TimeoutError as e:
|
||||
except asyncio.TimeoutError as e:
|
||||
logger.info("No ping retrieved in 3 seconds, killing myself.")
|
||||
self.kill()
|
||||
|
||||
logger.debug("Received message \"%s\"", message)
|
||||
logger.debug('Received message "%s"', message)
|
||||
if "endpoint" not in message:
|
||||
logger.error("No received endpoint in message, excepted ping endpoint.")
|
||||
return
|
||||
|
||||
|
||||
# See what endpoint we received
|
||||
match message["endpoint"]:
|
||||
case "ping":
|
||||
case "ping":
|
||||
await asyncio.sleep(1)
|
||||
case _:
|
||||
logger.info("Received message with topic different than ping," \
|
||||
" while ping expected.")
|
||||
|
||||
logger.info(
|
||||
"Received message with topic different than ping, while ping expected."
|
||||
)
|
||||
|
||||
async def setup(self, max_retries: int = 5):
|
||||
"""
|
||||
@@ -67,14 +72,13 @@ class RICommunicationAgent(Agent):
|
||||
logger.info("Setting up %s", self.jid)
|
||||
retries = 0
|
||||
|
||||
|
||||
# Let's try a certain amount of times before failing connection
|
||||
while retries < max_retries:
|
||||
# Bind request socket
|
||||
self.req_socket = context.socket(zmq.REQ)
|
||||
if self._bind:
|
||||
if self._bind:
|
||||
self.req_socket.bind(self._address)
|
||||
else:
|
||||
else:
|
||||
self.req_socket.connect(self._address)
|
||||
|
||||
# Send our message and receive one back:)
|
||||
@@ -85,10 +89,13 @@ class RICommunicationAgent(Agent):
|
||||
received_message = await asyncio.wait_for(self.req_socket.recv_json(), timeout=20.0)
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning("No connection established in 20 seconds (attempt %d/%d)",
|
||||
retries + 1, max_retries)
|
||||
logger.warning(
|
||||
"No connection established in 20 seconds (attempt %d/%d)",
|
||||
retries + 1,
|
||||
max_retries,
|
||||
)
|
||||
retries += 1
|
||||
continue
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Unexpected error during negotiation: %s", e)
|
||||
@@ -99,10 +106,14 @@ class RICommunicationAgent(Agent):
|
||||
endpoint = received_message.get("endpoint")
|
||||
if endpoint != "negotiate/ports":
|
||||
# TODO: Should this send a message back?
|
||||
logger.error("Invalid endpoint '%s' received (attempt %d/%d)",
|
||||
endpoint, retries + 1, max_retries)
|
||||
logger.error(
|
||||
"Invalid endpoint '%s' received (attempt %d/%d)",
|
||||
endpoint,
|
||||
retries + 1,
|
||||
max_retries,
|
||||
)
|
||||
retries += 1
|
||||
continue
|
||||
continue
|
||||
|
||||
# At this point, we have a valid response
|
||||
try:
|
||||
@@ -113,7 +124,7 @@ class RICommunicationAgent(Agent):
|
||||
|
||||
if not bind:
|
||||
addr = f"tcp://localhost:{port}"
|
||||
else:
|
||||
else:
|
||||
addr = f"tcp://*:{port}"
|
||||
|
||||
match id:
|
||||
@@ -125,11 +136,13 @@ class RICommunicationAgent(Agent):
|
||||
self.req_socket.bind(addr)
|
||||
case "actuation":
|
||||
ri_commands_agent = RICommandAgent(
|
||||
settings.agent_settings.ri_command_agent_name +
|
||||
'@' + settings.agent_settings.host,
|
||||
settings.agent_settings.ri_command_agent_name,
|
||||
address=addr,
|
||||
bind=bind )
|
||||
settings.agent_settings.ri_command_agent_name
|
||||
+ "@"
|
||||
+ settings.agent_settings.host,
|
||||
settings.agent_settings.ri_command_agent_name,
|
||||
address=addr,
|
||||
bind=bind,
|
||||
)
|
||||
await ri_commands_agent.start()
|
||||
case _:
|
||||
logger.warning("Unhandled negotiation id: %s", id)
|
||||
@@ -150,5 +163,3 @@ class RICommunicationAgent(Agent):
|
||||
listen_behaviour = self.ListenBehaviour()
|
||||
self.add_behaviour(listen_behaviour)
|
||||
logger.info("Finished setting up %s", self.jid)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user