Ejemplo n.º 1
0
    def update_value(self, topic_name, message):
        if hasattr(message, '__slots__') and hasattr(message, '_slot_types'):
            for slot_name in message.__slots__:
                self.update_value(topic_name + '/' + slot_name,
                                  getattr(message, slot_name))

        elif type(message) in (list, tuple):
            if (len(message) > 0) and hasattr(message[0], '__slots__'):
                for index, slot in enumerate(message):
                    if topic_name + '[%d]' % index in self._feedback_nodes:
                        self.update_value(topic_name + '[%d]' % index, slot)
                    else:
                        if topic_name in self._feedback_nodes:
                            base_type_str, _ = ros_topics._extract_array_info(
                                self._feedback_nodes[topic_name].text(
                                    self.feedback_type))
                            self._recursive_create_items(
                                self._feedback_nodes[topic_name],
                                topic_name + '[%d]' % index, base_type_str,
                                slot, None)
            # remove obsolete children
            if topic_name in self._feedback_nodes:
                if len(message) < len(
                        self._feedback_nodes[topic_name].get_children()):
                    for i in range(
                            len(message),
                            self._feedback_nodes[topic_name].childCount()):
                        item_topic_name = topic_name + '[%d]' % i
                        self.recursive_delete_items(
                            self._feedback_nodes[item_topic_name])
                        del self._feedback_nodes[item_topic_name]
        else:
            if topic_name in self._feedback_nodes and self._feedback_nodes[
                    topic_name] is not None:
                self._feedback_nodes[topic_name].set_value(repr(message))
Ejemplo n.º 2
0
    def _recursive_create_feedback_items(self, parent, feedback_topic_name,
                                         type_name, feedback_message):
        idx = self.idx
        topic_text = feedback_topic_name.split('/')[-1]
        if '[' in topic_text:
            topic_text = topic_text[topic_text.index('['):]

        # This here are 'complex data'
        if hasattr(feedback_message, '__slots__') and hasattr(
                feedback_message, '_slot_types'):
            complex_type = True
            new_node = parent.add_object(
                ua.NodeId(feedback_topic_name, parent.nodeid.NamespaceIndex,
                          ua.NodeIdType.String),
                ua.QualifiedName(feedback_topic_name,
                                 parent.nodeid.NamespaceIndex))

            for slot_name, type_name_child in zip(
                    feedback_message.__slots__, feedback_message._slot_types):
                self._recursive_create_feedback_items(
                    new_node, feedback_topic_name + '/' + slot_name,
                    type_name_child, getattr(feedback_message, slot_name))
            self._feedback_nodes[feedback_topic_name] = new_node

        else:
            # This are arrays
            base_type_str, array_size = ros_topics._extract_array_info(
                type_name)
            try:
                base_instance = roslib.message.get_message_class(
                    base_type_str)()
            except (ValueError, TypeError):
                base_instance = None

            if array_size is not None and hasattr(base_instance, '__slots__'):
                for index in range(array_size):
                    self._recursive_create_feedback_items(
                        parent, feedback_topic_name + '[%d]' % index,
                        base_type_str, base_instance)
            else:
                new_node = ros_topics._create_node_with_type(
                    parent, idx, feedback_topic_name, topic_text, type_name,
                    array_size)
                new_node.set_writable(True)
                self._feedback_nodes[feedback_topic_name] = new_node
        return