refactor: use new port negotiation style

As changed in the API document, this now uses the new port negotiation style.

ref: N25B-168
This commit is contained in:
Twirre Meulenbelt
2025-10-16 17:22:04 +02:00
parent e12d88726d
commit 23c3379bfb
8 changed files with 43 additions and 69 deletions

View File

@@ -1,5 +1,4 @@
import logging import logging
import sys
import zmq import zmq
@@ -17,32 +16,10 @@ class ActuationReceiver(ReceiverBase):
:param port: The port to use. :param port: The port to use.
:type port: int :type port: int
""" """
super(ActuationReceiver, self).__init__("actuation", "json") super(ActuationReceiver, self).__init__("actuation")
self.create_socket(zmq_context, zmq.SUB, port) self.create_socket(zmq_context, zmq.SUB, port, options=[(zmq.SUBSCRIBE, u"")])
self.socket.setsockopt_string(zmq.SUBSCRIBE, u"") # Subscribe to all topics
self._qi_session = self._get_session()
self._tts_service = None self._tts_service = None
@staticmethod
def _get_session():
if "--qi-url" not in sys.argv:
logging.info("No Qi URL argument given. Running in stand-alone mode.")
return None
try:
import qi
except ImportError:
logging.info("Unable to import qi. Running in stand-alone mode.")
return None
try:
app = qi.Application()
app.start()
return app.session
except RuntimeError:
logging.info("Unable to connect to the robot. Running in stand-alone mode.")
return None
def _handle_speech(self, message): def _handle_speech(self, message):
text = message.get("data") text = message.get("data")
if not text: if not text:

View File

@@ -15,7 +15,7 @@ class MainReceiver(ReceiverBase):
:param port: The port to use. :param port: The port to use.
:type port: int :type port: int
""" """
super(MainReceiver, self).__init__("main", "json") super(MainReceiver, self).__init__("main")
self.create_socket(zmq_context, zmq.REP, port, bind=False) self.create_socket(zmq_context, zmq.REP, port, bind=False)
@staticmethod @staticmethod

View File

@@ -2,8 +2,6 @@ from abc import ABCMeta
import zmq import zmq
from robot_interface.utils import zmq_socket_type_int_to_str, zmq_socket_type_complement
class SocketBase(object): class SocketBase(object):
__metaclass__ = ABCMeta __metaclass__ = ABCMeta
@@ -11,16 +9,12 @@ class SocketBase(object):
name = None name = None
socket = None socket = None
def __init__(self, name, data_type): def __init__(self, identifier):
""" """
:param name: The name of the endpoint. :param identifier: The identifier of the endpoint.
:type name: str :type identifier: str
:param data_type: The data type of the endpoint, e.g. "json", "binary", "text", etc.
:type data_type: str
""" """
self.name = name self.identifier = identifier
self.data_type = data_type
self.port = None # Set later by `create_socket` self.port = None # Set later by `create_socket`
self.socket = None # Set later by `create_socket` self.socket = None # Set later by `create_socket`
self.bound = None # Set later by `create_socket` self.bound = None # Set later by `create_socket`
@@ -71,9 +65,7 @@ class SocketBase(object):
:rtype: dict :rtype: dict
""" """
return { return {
"name": self.name, "id": self.identifier,
"port": self.port, "port": self.port,
"pattern": zmq_socket_type_int_to_str[zmq_socket_type_complement[self.socket.getsockopt(zmq.TYPE)]],
"data_type": self.data_type,
"bind": not self.bound "bind": not self.bound
} }

View File

@@ -16,11 +16,11 @@ class VideoSender(SocketBase):
""" """
Prepares arguments for retrieving video images from Pepper and starts video loop on a separate thread. Prepares arguments for retrieving video images from Pepper and starts video loop on a separate thread.
""" """
app = qi.Application() if not state.qi_session:
app.start() logging.info("No QI session available, not starting video loop")
session = app.session return
video = session.service("ALVideoDevice") video = state.session.service("ALVideoDevice")
camera_index = 0 camera_index = 0
kQVGA = 2 kQVGA = 2

View File

@@ -2,6 +2,8 @@ import logging
import signal import signal
import threading import threading
from robot_interface.utils.qi_utils import get_qi_session
class State(object): class State(object):
""" """
@@ -15,6 +17,7 @@ class State(object):
self.is_initialized = False self.is_initialized = False
self.exit_event = None self.exit_event = None
self.sockets = [] # type: List[SocketBase] self.sockets = [] # type: List[SocketBase]
self.qi_session = None # type: None | ssl.SSLSession
def initialize(self): def initialize(self):
if self.is_initialized: if self.is_initialized:
@@ -28,6 +31,8 @@ class State(object):
signal.signal(signal.SIGINT, handle_exit) signal.signal(signal.SIGINT, handle_exit)
signal.signal(signal.SIGTERM, handle_exit) signal.signal(signal.SIGTERM, handle_exit)
self.qi_session = get_qi_session()
self.is_initialized = True self.is_initialized = True
def deinitialize(self): def deinitialize(self):

View File

@@ -1,25 +0,0 @@
zmq_socket_type_complement = {
0: 0, # PAIR - PAIR
1: 2, # PUB - SUB
2: 1, # SUB - PUB
3: 4, # REQ - REP
4: 3, # REP - REQ
5: 6, # DEALER - ROUTER
6: 5, # ROUTER - DEALER
7: 8, # PULL - PUSH
8: 7, # PUSH - PULL
}
zmq_socket_type_int_to_str = {
0: "PAIR",
1: "PUB",
2: "SUB",
3: "REQ",
4: "REP",
5: "DEALER",
6: "ROUTER",
7: "PULL",
8: "PUSH",
}
zmq_socket_type_str_to_int = {value: key for key, value in zmq_socket_type_int_to_str.items()}

View File

View File

@@ -0,0 +1,25 @@
import logging
import sys
try:
import qi
except ImportError:
qi = None
def get_qi_session():
if qi is None:
logging.info("Unable to import qi. Running in stand-alone mode.")
return None
if "--qi-url" not in sys.argv:
logging.info("No Qi URL argument given. Running in stand-alone mode.")
return None
try:
app = qi.Application()
app.start()
return app.session
except RuntimeError:
logging.info("Unable to connect to the robot. Running in stand-alone mode.")
return None