fix: ruff checks is now in order:)
ref: N25B-205
This commit is contained in:
@@ -1,23 +1,21 @@
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
from spade.agent import Agent
|
||||
from spade.behaviour import CyclicBehaviour
|
||||
|
||||
import zmq
|
||||
import zmq.asyncio
|
||||
from spade.agent import Agent
|
||||
from spade.behaviour import CyclicBehaviour
|
||||
|
||||
|
||||
from control_backend.agents.ri_command_agent import RICommandAgent
|
||||
from control_backend.core.config import settings
|
||||
from control_backend.core.zmq_context import context
|
||||
from control_backend.schemas.ri_message import RIMessage
|
||||
from control_backend.agents.ri_command_agent import RICommandAgent
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RICommunicationAgent(Agent):
|
||||
_pub_socket: zmq.asyncio.Socket
|
||||
_pub_socket: zmq.asyncio.Socket
|
||||
req_socket: zmq.asyncio.Socket | None
|
||||
_address = ""
|
||||
_bind = True
|
||||
@@ -32,7 +30,6 @@ class RICommunicationAgent(Agent):
|
||||
verify_security: bool = False,
|
||||
address="tcp://localhost:0000",
|
||||
bind=False,
|
||||
|
||||
):
|
||||
super().__init__(jid, password, port, verify_security)
|
||||
self._address = address
|
||||
@@ -54,9 +51,10 @@ class RICommunicationAgent(Agent):
|
||||
await asyncio.wait_for(
|
||||
self.agent.req_socket.send_json(message), timeout=seconds_to_wait_total / 2
|
||||
)
|
||||
except TimeoutError as e:
|
||||
except TimeoutError:
|
||||
logger.debug(
|
||||
f"Waited too long to send message - we probably dont have any receivers... but let's check!"
|
||||
"Waited too long to send message - "
|
||||
"we probably dont have any receivers... but let's check!"
|
||||
)
|
||||
|
||||
# Wait up to three seconds for a reply:)
|
||||
@@ -67,8 +65,11 @@ class RICommunicationAgent(Agent):
|
||||
)
|
||||
|
||||
# We didnt get a reply :(
|
||||
except TimeoutError as e:
|
||||
logger.info(f"No ping back retrieved in {seconds_to_wait_total/2} seconds totalling {seconds_to_wait_total} of time, killing myself (or maybe just laying low).")
|
||||
except TimeoutError:
|
||||
logger.info(
|
||||
f"No ping back retrieved in {seconds_to_wait_total / 2} seconds totalling"
|
||||
f"{seconds_to_wait_total} of time, killing myself (or maybe just laying low)."
|
||||
)
|
||||
# TODO: Send event to UI letting know that we've lost connection
|
||||
topic = b"ping"
|
||||
data = json.dumps(False).encode()
|
||||
@@ -95,8 +96,7 @@ class RICommunicationAgent(Agent):
|
||||
"Received message with topic different than ping, while ping expected."
|
||||
)
|
||||
|
||||
|
||||
async def setup_req_socket(self, force = False):
|
||||
async def setup_req_socket(self, force=False):
|
||||
"""
|
||||
Sets up request socket for communication agent.
|
||||
"""
|
||||
@@ -107,7 +107,6 @@ class RICommunicationAgent(Agent):
|
||||
else:
|
||||
self.req_socket.connect(self._address)
|
||||
|
||||
|
||||
async def setup(self, max_retries: int = 5):
|
||||
"""
|
||||
Try to setup the communication agent, we have 5 retries in case we dont have a response yet.
|
||||
@@ -116,15 +115,14 @@ class RICommunicationAgent(Agent):
|
||||
|
||||
# Bind request socket
|
||||
await self.setup_req_socket()
|
||||
|
||||
|
||||
retries = 0
|
||||
# Let's try a certain amount of times before failing connection
|
||||
while retries < max_retries:
|
||||
|
||||
# Make sure the socket is properly setup.
|
||||
if self.req_socket is None:
|
||||
continue
|
||||
|
||||
|
||||
# Send our message and receive one back:)
|
||||
message = {"endpoint": "negotiate/ports", "data": {}}
|
||||
await self.req_socket.send_json(message)
|
||||
@@ -132,7 +130,7 @@ class RICommunicationAgent(Agent):
|
||||
try:
|
||||
received_message = await asyncio.wait_for(self.req_socket.recv_json(), timeout=20.0)
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
except TimeoutError:
|
||||
logger.warning(
|
||||
"No connection established in 20 seconds (attempt %d/%d)",
|
||||
retries + 1,
|
||||
|
||||
Reference in New Issue
Block a user