def test_populate_request_args(self):
        # Test empty messages
        for srv_type in ["TestEmpty", "TestResponseOnly"]:
            cls = ros_loader.get_service_class("rosbridge_library/" + srv_type)
            for args in [[], {}, None]:
                # Should throw no exceptions
                services.args_to_service_request_instance(
                    "", cls._request_class(), args)

        # Test msgs with data message
        for srv_type in ["TestRequestOnly", "TestRequestAndResponse"]:
            cls = ros_loader.get_service_class("rosbridge_library/" + srv_type)
            for args in [[3], {"data": 3}]:
                # Should throw no exceptions
                services.args_to_service_request_instance(
                    "", cls._request_class(), args)
            self.assertRaises(FieldTypeMismatchException,
                              services.args_to_service_request_instance, "",
                              cls._request_class(), ["hello"])

        # Test message with multiple fields
        cls = ros_loader.get_service_class(
            "rosbridge_library/TestMultipleRequestFields")
        for args in [[3, 3.5, "hello", False], {
                "int": 3,
                "float": 3.5,
                "string": "hello",
                "bool": False
        }]:
            # Should throw no exceptions
            services.args_to_service_request_instance("", cls._request_class(),
                                                      args)
Пример #2
0
def call_service(node_handle, service, args=None):
    # Given the service name, fetch the type and class of the service,
    # and a request instance

    # This should be equivalent to rospy.resolve_name.
    service = expand_topic_name(service, node_handle.get_name(),
                                node_handle.get_namespace())

    service_names_and_types = dict(node_handle.get_service_names_and_types())
    service_type = service_names_and_types.get(service)
    if service_type is None:
        raise InvalidServiceException(service)
    # service_type is a tuple of types at this point; only one type is supported.
    if len(service_type) > 1:
        node_handle.get_logger().warning(
            'More than one service type detected: {}'.format(service_type))
    service_type = service_type[0]

    service_class = get_service_class(service_type)
    inst = get_service_request_instance(service_type)

    # Populate the instance with the provided args
    args_to_service_request_instance(service, inst, args)

    client = node_handle.create_client(service_class, service)

    future = client.call_async(inst)
    spin_until_future_complete(node_handle, future)
    if future.result() is not None:
        # Turn the response into JSON and pass to the callback
        json_response = extract_values(future.result())
    else:
        raise Exception(future.exception())

    return json_response
Пример #3
0
 def __init__(self, service_name, service_type, protocol):
     self.service_name = service_name
     self.service_type = service_type
     self.protocol = protocol
     # setup the service
     self.service_handle = rospy.Service(service_name,
                                         get_service_class(service_type),
                                         self.handle_request)
 def __init__(self, service_name, service_type, protocol):
     self.active_requests = 0
     self.shutdown_requested = False
     self.lock = Lock()
     self.service_name = service_name
     self.service_type = service_type
     self.protocol = protocol
     # setup the service
     self.service_handle = rospy.Service(service_name, get_service_class(service_type), self.handle_request)
Пример #5
0
 def __init__(self, service_name, service_type, protocol):
     self.active_requests = 0
     self.shutdown_requested = False
     self.lock = Lock()
     self.service_name = service_name
     self.service_type = service_type
     self.protocol = protocol
     # setup the service
     self.service_handle = rospy.Service(service_name, get_service_class(service_type), self.handle_request)
 def test_irregular_servicenames(self):
     irregular = ["roscpp//GetLoggers", "/roscpp/GetLoggers/",
     "/roscpp/GetLoggers", "//roscpp/GetLoggers", "/roscpp//GetLoggers",
     "roscpp/GetLoggers//", "/roscpp/GetLoggers//", "roscpp/GetLoggers/",
     "roscpp//GetLoggers//"]
     for x in irregular:
         self.assertNotEqual(ros_loader.get_service_class(x), None)
         self.assertNotEqual(ros_loader.get_service_instance(x), None)
         self.assertNotEqual(ros_loader.get_service_request_instance(x), None)
         self.assertNotEqual(ros_loader.get_service_response_instance(x), None)
    def test_populate_request_args(self):
        # Test empty messages
        for srv_type in ["TestEmpty", "TestResponseOnly"]:
            cls = ros_loader.get_service_class("rosbridge_library/" + srv_type)
            for args in [[], {}, None]:
                # Should throw no exceptions
                services.args_to_service_request_instance("", cls._request_class(), args)

        # Test msgs with data message
        for srv_type in ["TestRequestOnly", "TestRequestAndResponse"]:
            cls = ros_loader.get_service_class("rosbridge_library/" + srv_type)
            for args in [[3], {"data": 3}]:
                # Should throw no exceptions
                services.args_to_service_request_instance("", cls._request_class(), args)
            self.assertRaises(FieldTypeMismatchException, services.args_to_service_request_instance, "", cls._request_class(), ["hello"])

        # Test message with multiple fields
        cls = ros_loader.get_service_class("rosbridge_library/TestMultipleRequestFields")
        for args in [[3, 3.5, "hello", False], {"int": 3, "float": 3.5, "string": "hello", "bool": False}]:
            # Should throw no exceptions
            services.args_to_service_request_instance("", cls._request_class(), args)
Пример #8
0
 def test_irregular_servicenames(self):
     irregular = ["roscpp//GetLoggers", "/roscpp/GetLoggers/",
                  "/roscpp/GetLoggers", "//roscpp/GetLoggers", "/roscpp//GetLoggers",
                  "roscpp/GetLoggers//", "/roscpp/GetLoggers//", "roscpp/GetLoggers/",
                  "roscpp//GetLoggers//"]
     for x in irregular:
         self.assertNotEqual(ros_loader.get_service_class(x), None)
         self.assertNotEqual(ros_loader.get_service_instance(x), None)
         self.assertNotEqual(
             ros_loader.get_service_request_instance(x), None)
         self.assertNotEqual(
             ros_loader.get_service_response_instance(x), None)
 def test_srv_cache(self):
     common = ["roscpp/GetLoggers", "roscpp/SetLoggerLevel",
     "std_srvs/Empty", "nav_msgs/GetMap", "nav_msgs/GetPlan",
     "sensor_msgs/SetCameraInfo", "topic_tools/MuxAdd",
     "topic_tools/MuxSelect", "tf2_msgs/FrameGraph",
     "rospy_tutorials/BadTwoInts", "rospy_tutorials/AddTwoInts"]
     for x in common:
         self.assertNotEqual(ros_loader.get_service_class(x), None)
         self.assertNotEqual(ros_loader.get_service_instance(x), None)
         self.assertNotEqual(ros_loader.get_service_request_instance(x), None)
         self.assertNotEqual(ros_loader.get_service_response_instance(x), None)
         self.assertTrue(x in ros_loader._loaded_srvs)
Пример #10
0
 def test_srv_cache(self):
     common = ["roscpp/GetLoggers", "roscpp/SetLoggerLevel",
               "std_srvs/Empty", "nav_msgs/GetMap", "nav_msgs/GetPlan",
               "sensor_msgs/SetCameraInfo", "topic_tools/MuxAdd",
               "topic_tools/MuxSelect", "tf2_msgs/FrameGraph",
               "rospy_tutorials/BadTwoInts", "rospy_tutorials/AddTwoInts"]
     for x in common:
         self.assertNotEqual(ros_loader.get_service_class(x), None)
         self.assertNotEqual(ros_loader.get_service_instance(x), None)
         self.assertNotEqual(
             ros_loader.get_service_request_instance(x), None)
         self.assertNotEqual(
             ros_loader.get_service_response_instance(x), None)
         self.assertTrue(x in ros_loader._loaded_srvs)
Пример #11
0
    def spawn_ROS_service(self, service_module, service_type, service_name, client_id):
        # import service type for ros-service that we want to register in ros
        service_class = get_service_class(service_module+'/'+service_type)
        # register service in ros
        self.ros_serviceproxy = rospy.Service( service_name, service_class, self.handle_service_request)

        # TODO: check if service successfully registered in ros. current state is that rosbridge "thinks" service is registered, but calls will fail
#        myServiceManager = rospy.service.ServiceManager()
#        myServiceManager.register(service_name, self.ros_serviceproxy)
#        print myServiceManager.get_services()

        # log service registration
        log_msg = "registered service: " + self.service_name
        self.protocol.log("info", log_msg)
        self.spawned = True
Пример #12
0
    def spawn_ROS_service(self, service_module, service_type, service_name,
                          client_id):
        # import service type for ros-service that we want to register in ros
        service_class = get_service_class(service_module + '/' + service_type)
        # register service in ros
        self.ros_serviceproxy = rospy.Service(service_name, service_class,
                                              self.handle_service_request)

        # TODO: check if service successfully registered in ros. current state is that rosbridge "thinks" service is registered, but calls will fail
        #        myServiceManager = rospy.service.ServiceManager()
        #        myServiceManager.register(service_name, self.ros_serviceproxy)
        #        print myServiceManager.get_services()

        # log service registration
        log_msg = "registered service: " + self.service_name
        self.protocol.log("info", log_msg)
        self.spawned = True
Пример #13
0
def call_service(service, args=None):
    # Given the service name, fetch the type and class of the service,
    # and a request instance
    service_type = get_service_type(str(service))
    if service_type is None:
        raise InvalidServiceException(service)
    service_class = get_service_class(service_type)
    inst = get_service_request_instance(service_type)

    # Populate the instance with the provided args
    args_to_service_request_instance(service, inst, args)

    # Call the service
    proxy = ServiceProxy(service, service_class)
    response = proxy.call(inst)

    # Turn the response into JSON and pass to the callback
    json_response = extract_values(response)

    return json_response
Пример #14
0
def call_service(service, args=None):
    # Given the service name, fetch the type and class of the service,
    # and a request instance
    service_type = get_service_type(str(service))
    if service_type is None:
        raise InvalidServiceException(service)
    service_class = get_service_class(service_type)
    inst = get_service_request_instance(service_type)

    # Populate the instance with the provided args
    args_to_service_request_instance(service, inst, args)

    # Call the service
    proxy = ServiceProxy(service, service_class)
    response = proxy.call(inst)

    # Turn the response into JSON and pass to the callback
    json_response = extract_values(response)

    return json_response
 def __init__(self, name, srv_type):
     self.name = name
     self.srvClass = ros_loader.get_service_class(srv_type)
     self.service = rospy.Service(name, self.srvClass, self.callback)
 def __init__(self, service_name, service_type, protocol):
     self.service_name = service_name
     self.service_type = service_type
     self.protocol = protocol
     # setup the service
     self.service_handle = rospy.Service(service_name, get_service_class(service_type), self.handle_request)
 def __init__(self, name, srv_type):
     self.name = name
     self.srvClass = ros_loader.get_service_class(srv_type)
     self.service = rospy.Service(name, self.srvClass, self.callback)