feat: added endpoint userinterrupt to userinterrupt
ref: N25B-400
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import zmq
|
||||
@@ -30,6 +31,26 @@ class UserInterruptAgent(BaseAgent):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.sub_socket = None
|
||||
self.pub_socket = None
|
||||
|
||||
async def setup(self):
|
||||
"""
|
||||
Initialize the agent.
|
||||
|
||||
Connects the internal ZMQ SUB socket and subscribes to the 'button_pressed' topic.
|
||||
Starts the background behavior to receive the user interrupts.
|
||||
"""
|
||||
context = Context.instance()
|
||||
|
||||
self.sub_socket = context.socket(zmq.SUB)
|
||||
self.sub_socket.connect(settings.zmq_settings.internal_sub_address)
|
||||
self.sub_socket.subscribe("button_pressed")
|
||||
|
||||
self.pub_socket = context.socket(zmq.PUB)
|
||||
self.pub_socket.connect(settings.zmq_settings.internal_pub_address)
|
||||
|
||||
self.add_behavior(self._receive_button_event())
|
||||
self.add_behavior(self.test_sending_behaviour())
|
||||
|
||||
async def _receive_button_event(self):
|
||||
"""
|
||||
@@ -78,6 +99,33 @@ class UserInterruptAgent(BaseAgent):
|
||||
event_context,
|
||||
)
|
||||
|
||||
async def test_sending_behaviour(self):
|
||||
self.logger.info("Starting simple test sending behaviour...")
|
||||
|
||||
while True:
|
||||
try:
|
||||
test_data = {"type": "heartbeat", "status": "ok"}
|
||||
|
||||
await self._send_experiment_update(test_data)
|
||||
|
||||
except zmq.ZMQError as ze:
|
||||
self.logger.error(f"ZMQ error: {ze}")
|
||||
except Exception as e:
|
||||
self.logger.error(f"Error: {e}")
|
||||
|
||||
await asyncio.sleep(2)
|
||||
|
||||
async def _send_experiment_update(self, data):
|
||||
"""
|
||||
Sends an update to the 'experiment' topic.
|
||||
The SSE endpoint will pick this up and push it to the UI.
|
||||
"""
|
||||
if self.pub_socket:
|
||||
topic = b"experiment"
|
||||
body = json.dumps(data).encode("utf-8")
|
||||
await self.pub_socket.send_multipart([topic, body])
|
||||
self.logger.debug(f"Sent experiment update: {data}")
|
||||
|
||||
async def _send_to_speech_agent(self, text_to_say: str):
|
||||
"""
|
||||
method to send prioritized speech command to RobotSpeechAgent.
|
||||
@@ -129,18 +177,3 @@ class UserInterruptAgent(BaseAgent):
|
||||
"Sent button_override belief with id '%s' to Program manager.",
|
||||
belief_id,
|
||||
)
|
||||
|
||||
async def setup(self):
|
||||
"""
|
||||
Initialize the agent.
|
||||
|
||||
Connects the internal ZMQ SUB socket and subscribes to the 'button_pressed' topic.
|
||||
Starts the background behavior to receive the user interrupts.
|
||||
"""
|
||||
context = Context.instance()
|
||||
|
||||
self.sub_socket = context.socket(zmq.SUB)
|
||||
self.sub_socket.connect(settings.zmq_settings.internal_sub_address)
|
||||
self.sub_socket.subscribe("button_pressed")
|
||||
|
||||
self.add_behavior(self._receive_button_event())
|
||||
|
||||
Reference in New Issue
Block a user