Beispiel #1
0
    def test_connect_when_db_exists(self, mock_set_param, mock_get_param,
                                    mock_has_param):

        mock_has_param.side_effect = [True]
        mock_set_param.side_effect = [True]
        mock_get_param.side_effect = [0]

        ros_server = RosServerConnection()

        self.assertEqual(mock_has_param.call_count, 1)
        self.assertEqual(mock_set_param.call_count, 0)
        self.assertEqual(ros_server.num_elements(), 0)
Beispiel #2
0
    def test_add_new_element_in_non_empty_db(self, mock_set_param,
                                             mock_get_param, mock_has_param):

        mock_has_param.side_effect = [False]
        mock_set_param.side_effect = [True]
        mock_get_param.side_effect = [0]

        ros_server = RosServerConnection()

        self.assertEqual(mock_has_param.call_count, 1)
        self.assertEqual(mock_set_param.call_count, 1)
        self.assertEqual(ros_server.num_elements(), 0)

        mock_has_param.side_effect = [False]
        mock_set_param.side_effect = [True, True, True]
        mock_get_param.side_effect = [1, "other_key", 1, 1, 1]

        self.assertTrue(ros_server.add_element("key_1", Pose()))

        self.assertEqual(mock_has_param.call_count, 1)
        self.assertEqual(mock_set_param.call_count, 3)
        self.assertEqual(mock_get_param.call_count, 5)
Beispiel #3
0
    def test_add_new_element_in_empty_db(self, mock_set_param, mock_get_param,
                                         mock_has_param):

        mock_has_param.side_effect = [False]
        mock_set_param.side_effect = [True]
        mock_get_param.side_effect = [0]

        ros_server = RosServerConnection()

        self.assertEqual(mock_has_param.call_count, 1)
        self.assertEqual(mock_set_param.call_count, 1)
        self.assertEqual(ros_server.num_elements(), 0)

        mock_has_param.side_effect = [False]
        mock_set_param.side_effect = [True, True, True]
        mock_get_param.side_effect = [0, 0, 1, 1]

        pose = Pose()

        self.assertTrue(ros_server.add_element("key_1", pose))

        self.assertEqual(mock_has_param.call_count, 1)
        self.assertEqual(mock_set_param.call_count, 3)
        self.assertEqual(mock_get_param.call_count, 4)

        element = {
            'msg_type': pose._type,
            'msg_value':
            message_converter.convert_ros_message_to_dictionary(pose),
            'metadata': "some information",
            'uuid': 123456789
        }
        mock_get_param.side_effect = [1, "key_1", element]

        success, element = ros_server.get_element("key_1")

        self.assertTrue(success)
        self.assertEqual(Pose(), element[0])
        self.assertEqual("some information", element[1])
Beispiel #4
0
    def test_add_existing_element(self, mock_set_param, mock_get_param,
                                  mock_has_param):

        mock_has_param.side_effect = [False]
        mock_set_param.side_effect = [True]
        mock_get_param.side_effect = [0]

        ros_server = RosServerConnection()

        self.assertEqual(mock_has_param.call_count, 1)
        self.assertEqual(mock_set_param.call_count, 1)
        self.assertEqual(ros_server.num_elements(), 0)

        mock_has_param.side_effect = [False]
        mock_set_param.side_effect = [True, True, True]
        mock_get_param.side_effect = [1, "key_1"]

        self.assertFalse(ros_server.add_element("key_1", Pose()))

        self.assertEqual(mock_has_param.call_count, 1)
        self.assertEqual(mock_set_param.call_count, 1)
        self.assertEqual(mock_get_param.call_count, 3)
Beispiel #5
0
    def __init__(self, service_prefix, sdb_name='scene_database'):

        self._lock = Lock()
        self._ros_server = RosServerConnection(sdb_name)
        self._start_services(service_prefix)
Beispiel #6
0
class SceneDatabase(object):

    def __init__(self, service_prefix, sdb_name='scene_database'):

        self._lock = Lock()
        self._ros_server = RosServerConnection(sdb_name)
        self._start_services(service_prefix)

    def _start_services(self, prefix):

        service_prefix = prefix + '/'

        service_name = service_prefix + ServiceNames.DIAGNOSTICS_DB
        rospy.Service(service_name, DiagnosticsDB, self._diagnostics_db)

        service_name = service_prefix + ServiceNames.RESET_DB
        rospy.Service(service_name, ResetDB, self._reset_db)

        service_name = service_prefix + ServiceNames.ADD_ELEMENT
        rospy.Service(service_name, AddElement, self._add_element)

        service_name = service_prefix + ServiceNames.FIND_ELEMENT
        rospy.Service(service_name, FindElement, self._find_element)

        service_name = service_prefix + ServiceNames.UPDATE_ELEMENT
        rospy.Service(service_name, UpdateElement, self._update_element)

        service_name = service_prefix + ServiceNames.REMOVE_ELEMENT
        rospy.Service(service_name, RemoveElement, self._remove_element)

        service_name = service_prefix + ServiceNames.RETRIEVE_ELEMENTS
        rospy.Service(service_name, RetrieveElements, self._retrieve_elements)

    def _diagnostics_db(self, request):

        rospy.loginfo("[RPpt][SDB] _diagnostics_db")

        with self._lock:
            num_elements = self._ros_server.num_elements()

        return True, num_elements

    def _reset_db(self, request):

        rospy.loginfo("[RPpt][SDB] _reset_db")

        with self._lock:
            self._ros_server.reset()
        return True

    def _add_element(self, request):

        rospy.loginfo("[RPpt][SDB] _add_element (%s) = %s", request.key, request.value)

        success = False
        element = string_to_sdb_element(request.metadata, request.value)

        with self._lock:
            if not self._ros_server.element_exists(request.key) and request.value != '':
                success = self._ros_server.add_element(request.key, element.value(), element.metadata())

        return success

    def _find_element(self, request):

        rospy.loginfo("[RPpt][SDB] _find_element (%s)", request.key)

        success = False
        metadata = ""
        value = ""

        with self._lock:
            if self._ros_server.element_exists(request.key):
                success, result = self._ros_server.get_element(request.key)
                if success:
                    metadata, value = sdb_element_to_string(Element(result[0], result[1]))

        return success, metadata, value

    def _update_element(self, request):

        rospy.loginfo("[RPpt][SDB] _update_element (%s) = %s", request.key, request.value)

        success = False
        element = string_to_sdb_element(request.metadata, request.value)

        with self._lock:
            if self._ros_server.element_exists(request.key) and request.value != "":
                success = self._ros_server.update_element(request.key, element.value(), element.metadata())

        return success

    def _remove_element(self, request):

        rospy.loginfo("[RPpt][SDB] _remove_element (%s)", request.key)

        success = False
        key = request.key

        with self._lock:
            if self._ros_server.element_exists(key):
                success = self._ros_server.remove_element(key)

        return success

    def _retrieve_elements(self, request):

        rospy.loginfo("[RPpt][SDB] _retrieve_elements")

        keys = []

        with self._lock:
            elements = self._ros_server.get_all_elements()
            for e in elements:
                keys.append(e[0])

        return True, keys

    def _remove_all_elements(self):

        rospy.loginfo("[RPpt][SDB] _remove_all_elements")

        with self._lock:
            elements = self._ros_server.get_all_elements()
            for key, value, metadata, id_ in elements:
                self._ros_server.remove_element(key)

        return True
Beispiel #7
0
    def test(self):

        ros_server = RosServerConnection()
        ros_server.reset()

        self.assertEqual(ros_server.num_elements(), 0)

        self.assertTrue(ros_server.add_element("key_1", Pose()))
        self.assertEqual(ros_server.num_elements(), 1)

        self.assertTrue(ros_server.add_element("key_2", Pose()))
        self.assertEqual(ros_server.num_elements(), 2)

        self.assertTrue(ros_server.add_element("key_3", Empty()))
        self.assertEqual(ros_server.num_elements(), 3)

        success, element = ros_server.get_element("key_1")
        self.assertTrue(success)
        success, element = ros_server.get_element("key_2")
        self.assertTrue(success)
        success, element = ros_server.get_element("key_3")
        self.assertTrue(success)

        self.assertTrue(ros_server.remove_element("key_1"))
        self.assertEqual(ros_server.num_elements(), 2)

        self.assertTrue(ros_server.remove_element("key_2"))
        self.assertEqual(ros_server.num_elements(), 1)

        self.assertTrue(ros_server.remove_element("key_3"))
        self.assertEqual(ros_server.num_elements(), 0)