diff --git a/src/robot_interface/endpoints/actuation_receiver.py b/src/robot_interface/endpoints/actuation_receiver.py index 79a38ef..ca004f6 100644 --- a/src/robot_interface/endpoints/actuation_receiver.py +++ b/src/robot_interface/endpoints/actuation_receiver.py @@ -1,5 +1,4 @@ import logging -import sys import zmq @@ -17,32 +16,10 @@ class ActuationReceiver(ReceiverBase): :param port: The port to use. :type port: int """ - super(ActuationReceiver, self).__init__("actuation", "json") - self.create_socket(zmq_context, zmq.SUB, port) - self.socket.setsockopt_string(zmq.SUBSCRIBE, u"") # Subscribe to all topics - self._qi_session = self._get_session() + super(ActuationReceiver, self).__init__("actuation") + self.create_socket(zmq_context, zmq.SUB, port, options=[(zmq.SUBSCRIBE, u"")]) 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): text = message.get("data") if not text: diff --git a/src/robot_interface/endpoints/main_receiver.py b/src/robot_interface/endpoints/main_receiver.py index 0ebd01a..cce4f96 100644 --- a/src/robot_interface/endpoints/main_receiver.py +++ b/src/robot_interface/endpoints/main_receiver.py @@ -15,7 +15,7 @@ class MainReceiver(ReceiverBase): :param port: The port to use. :type port: int """ - super(MainReceiver, self).__init__("main", "json") + super(MainReceiver, self).__init__("main") self.create_socket(zmq_context, zmq.REP, port, bind=False) @staticmethod diff --git a/src/robot_interface/endpoints/socket_base.py b/src/robot_interface/endpoints/socket_base.py index 3419349..fc351ad 100644 --- a/src/robot_interface/endpoints/socket_base.py +++ b/src/robot_interface/endpoints/socket_base.py @@ -2,8 +2,6 @@ from abc import ABCMeta import zmq -from robot_interface.utils import zmq_socket_type_int_to_str, zmq_socket_type_complement - class SocketBase(object): __metaclass__ = ABCMeta @@ -11,16 +9,12 @@ class SocketBase(object): name = None socket = None - def __init__(self, name, data_type): + def __init__(self, identifier): """ - :param name: The name of the endpoint. - :type name: str - - :param data_type: The data type of the endpoint, e.g. "json", "binary", "text", etc. - :type data_type: str + :param identifier: The identifier of the endpoint. + :type identifier: str """ - self.name = name - self.data_type = data_type + self.identifier = identifier self.port = None # Set later by `create_socket` self.socket = None # Set later by `create_socket` self.bound = None # Set later by `create_socket` @@ -71,9 +65,7 @@ class SocketBase(object): :rtype: dict """ return { - "name": self.name, + "id": self.identifier, "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 } diff --git a/src/robot_interface/endpoints/video_sender.py b/src/robot_interface/endpoints/video_sender.py index 793385b..41a9e6e 100644 --- a/src/robot_interface/endpoints/video_sender.py +++ b/src/robot_interface/endpoints/video_sender.py @@ -15,12 +15,12 @@ class VideoSender(SocketBase): def start_video_rcv(self): """ Prepares arguments for retrieving video images from Pepper and starts video loop on a separate thread. - """ - app = qi.Application() - app.start() - session = app.session + """ + if not state.qi_session: + logging.info("No QI session available, not starting video loop") + return - video = session.service("ALVideoDevice") + video = state.session.service("ALVideoDevice") camera_index = 0 kQVGA = 2 diff --git a/src/robot_interface/state.py b/src/robot_interface/state.py index d10cf77..b6f8ce1 100644 --- a/src/robot_interface/state.py +++ b/src/robot_interface/state.py @@ -2,6 +2,8 @@ import logging import signal import threading +from robot_interface.utils.qi_utils import get_qi_session + class State(object): """ @@ -15,6 +17,7 @@ class State(object): self.is_initialized = False self.exit_event = None self.sockets = [] # type: List[SocketBase] + self.qi_session = None # type: None | ssl.SSLSession def initialize(self): if self.is_initialized: @@ -28,6 +31,8 @@ class State(object): signal.signal(signal.SIGINT, handle_exit) signal.signal(signal.SIGTERM, handle_exit) + self.qi_session = get_qi_session() + self.is_initialized = True def deinitialize(self): diff --git a/src/robot_interface/utils.py b/src/robot_interface/utils.py deleted file mode 100644 index f2a74bd..0000000 --- a/src/robot_interface/utils.py +++ /dev/null @@ -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()} diff --git a/src/robot_interface/utils/__init__.py b/src/robot_interface/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/robot_interface/utils/qi_utils.py b/src/robot_interface/utils/qi_utils.py new file mode 100644 index 0000000..fc7640b --- /dev/null +++ b/src/robot_interface/utils/qi_utils.py @@ -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