11 from opcua
import ua, uamethod
19 def __init__(self, server, parent, idx, topic_name, topic_type):
29 self.
message_class = roslib.message.get_message_class(topic_type)
32 except rospy.ROSException:
34 rospy.logfatal(
"Couldn't find message class for type " + topic_type)
40 rospy.loginfo(
"Created ROS Topic with name: " + str(self.
name))
43 topic_text = topic_name.split(
'/')[-1]
45 topic_text = topic_text[topic_text.index(
'['):]
48 if hasattr(message,
'__slots__')
and hasattr(message,
'_slot_types'):
50 new_node = parent.add_object(ua.NodeId(topic_name, parent.nodeid.NamespaceIndex, ua.NodeIdType.String),
51 ua.QualifiedName(topic_name, parent.nodeid.NamespaceIndex))
52 new_node.add_property(ua.NodeId(topic_name +
".Type", idx),
53 ua.QualifiedName(
"Type", parent.nodeid.NamespaceIndex), type_name)
55 new_node.add_method(ua.NodeId(topic_name +
".Update", parent.nodeid.NamespaceIndex),
56 ua.QualifiedName(
"Update", parent.nodeid.NamespaceIndex),
58 for slot_name, type_name_child
in zip(message.__slots__, message._slot_types):
60 getattr(message, slot_name))
61 self.
_nodes[topic_name] = new_node
67 base_instance = roslib.message.get_message_class(base_type_str)()
68 except (ValueError, TypeError):
71 if array_size
is not None and hasattr(base_instance,
'__slots__'):
72 for index
in range(array_size):
76 self.
_nodes[topic_name] = new_node
78 if topic_name
in self.
_nodes and self.
_nodes[topic_name].get_node_class() == ua.NodeClass.Variable:
79 self.
_nodes[topic_name].set_writable(
True)
88 for nodeName
in self.
_nodes:
89 child = self.
_nodes[nodeName]
90 name = child.get_display_name().Text
92 if child.get_node_class() == ua.NodeClass.Variable:
95 elif child.get_node_class == ua.NodeClass.Object:
98 except rospy.ROSException
as e:
99 rospy.logerr(
"Error when updating node " + self.
name, e)
100 self.server.server.delete_nodes([self.
parent])
103 if hasattr(message,
'__slots__')
and hasattr(message,
'_slot_types'):
104 for slot_name
in message.__slots__:
105 self.
update_value(topic_name +
'/' + slot_name, getattr(message, slot_name))
107 elif type(message)
in (list, tuple):
108 if (len(message) > 0)
and hasattr(message[0],
'__slots__'):
109 for index, slot
in enumerate(message):
110 if topic_name +
'[%d]' % index
in self.
_nodes:
113 if topic_name
in self.
_nodes:
120 if topic_name
in self.
_nodes:
121 if len(message) < len(self.
_nodes[topic_name].get_children()):
122 for i
in range(len(message), self.
_nodes[topic_name].childCount()):
123 item_topic_name = topic_name +
'[%d]' % i
125 del self.
_nodes[item_topic_name]
127 if topic_name
in self.
_nodes and self.
_nodes[topic_name]
is not None:
128 self.
_nodes[topic_name].set_value(repr(message))
131 self._publisher.unregister()
132 self._subscriber.unregister()
133 for child
in item.get_children():
137 self.server.server.delete_nodes([child])
138 self.server.server.delete_nodes([item])
139 if len(self.parent.get_children()) == 0:
140 self.server.server.delete_nodes([self.
parent])
143 for child
in node.get_children():
144 name = child.get_display_name().Text
146 if child.get_node_class() == ua.NodeClass.Variable:
149 elif child.get_node_class == ua.NodeClass.Object:
154 hierachy = topic_name.split(
'/')
155 if len(hierachy) == 0
or len(hierachy) == 1:
157 for name
in hierachy:
160 nodewithsamename = self.server.find_topics_node_with_same_name(name, idx)
161 if nodewithsamename
is not None:
167 newparent = parent.add_object(
168 ua.NodeId(name, parent.nodeid.NamespaceIndex, ua.NodeIdType.String),
169 ua.QualifiedName(name, parent.nodeid.NamespaceIndex))
173 except IndexError, common.UaError:
174 newparent = parent.add_object(
175 ua.NodeId(name + str(random.randint(0, 10000)), parent.nodeid.NamespaceIndex,
176 ua.NodeIdType.String),
177 ua.QualifiedName(name, parent.nodeid.NamespaceIndex))
187 data_value = node.get_data_value()
188 result = node.get_value()
189 if isinstance(data_value, ua.DataValue):
190 if typemessage.__name__ ==
"float":
191 result = numpy.float(result)
192 if typemessage.__name__ ==
"double":
193 result = numpy.double(result)
194 if typemessage.__name__ ==
"int":
195 result = int(result) & 0xff
197 rospy.logerr(
"can't convert: " + str(node.get_data_value.Value))
204 if '[' in type_str
and type_str[-1] ==
']':
205 type_str, array_size_str = type_str.split(
'[', 1)
206 array_size_str = array_size_str[:-1]
207 if len(array_size_str) > 0:
208 array_size = int(array_size_str)
212 return type_str, array_size
217 type_name = type_name[:type_name.index(
'[')]
219 if type_name ==
'bool':
220 dv = ua.Variant(
False, ua.VariantType.Boolean)
221 elif type_name ==
'byte':
222 dv = ua.Variant(0, ua.VariantType.Byte)
223 elif type_name ==
'int':
224 dv = ua.Variant(0, ua.VariantType.Int32)
225 elif type_name ==
'int8':
226 dv = ua.Variant(0, ua.VariantType.SByte)
227 elif type_name ==
'uint8':
228 dv = ua.Variant(0, ua.VariantType.Byte)
229 elif type_name ==
'int16':
230 dv = ua.Variant(0, ua.VariantType.Int16)
231 elif type_name ==
'uint16':
232 dv = ua.Variant(0, ua.VariantType.UInt16)
233 elif type_name ==
'int32':
234 dv = ua.Variant(0, ua.VariantType.Int32)
235 elif type_name ==
'uint32':
236 dv = ua.Variant(0, ua.VariantType.UInt32)
237 elif type_name ==
'int64':
238 dv = ua.Variant(0, ua.VariantType.Int64)
239 elif type_name ==
'uint64':
240 dv = ua.Variant(0, ua.VariantType.UInt64)
241 elif type_name ==
'float' or type_name ==
'float32' or type_name ==
'float64':
242 dv = ua.Variant(0.0, ua.VariantType.Float)
243 elif type_name ==
'double':
244 dv = ua.Variant(0.0, ua.VariantType.Double)
245 elif type_name ==
'string':
246 dv = ua.Variant(
'', ua.VariantType.String)
248 rospy.logerr(
"can't create node with type" + str(type_name))
251 if array_size
is not None:
253 for i
in range(array_size):
255 return parent.add_variable(ua.NodeId(topic_name, parent.nodeid.NamespaceIndex),
256 ua.QualifiedName(topic_text, parent.nodeid.NamespaceIndex), dv.Value)
262 if nametolookfor !=
"/rosout":
263 ret = topicsDict[nametolookfor]._subscriber.get_num_connections()
270 ros_topics = rospy.get_published_topics(namespace_ros)
271 rospy.logdebug(str(ros_topics))
272 rospy.logdebug(str(rospy.get_published_topics(
'/move_base_simple')))
273 for topic_name, topic_type
in ros_topics:
274 if topic_name
not in topicsdict
or topicsdict[topic_name]
is None:
275 splits = topic_name.split(
'/')
276 if "cancel" in splits[-1]
or "result" in splits[-1]
or "feedback" in splits[-1]
or "goal" in splits[-1]
or "status" in splits[-1]:
277 rospy.logdebug(
"Found an action: " + str(topic_name))
279 if correct_name
not in actionsdict:
281 rospy.loginfo(
"Creating Action with name: " + correct_name)
288 except (ValueError, TypeError, AttributeError)
as e:
290 rospy.logerr(
"Error while creating Action Objects for Action " + topic_name)
294 topic =
OpcUaROSTopic(server, topics, idx_topics, topic_name, topic_type)
295 topicsdict[topic_name] = topic
297 topicsdict[topic_name].recursive_delete_items(server.server.get_node(ua.NodeId(topic_name, idx_topics)))
298 del topicsdict[topic_name]
301 ros_topics = rospy.get_published_topics(namespace_ros)
304 for topic_nameOPC
in topicsdict:
306 for topicROS, topic_type
in ros_topics:
307 if topic_nameOPC == topicROS:
310 topicsdict[topic_nameOPC].recursive_delete_items(server.get_node(ua.NodeId(topic_nameOPC, idx_topics)))
311 tobedeleted.append(topic_nameOPC)
312 for name
in tobedeleted:
319 type, name, fn = rostopic.get_topic_type(action_name +
"/feedback")
321 except rospy.ROSException
as e:
323 type, name, fn = rostopic.get_topic_type(action_name +
"/Feedback", e)
325 except rospy.ROSException
as e2:
326 rospy.logerr(
"Couldn't find feedback type for action " + action_name, e2)
332 type, name, fn = rostopic.get_topic_type(action_name +
"/goal")
334 except rospy.ROSException
as e:
336 type, name, fn = rostopic.get_topic_type(action_name +
"/Goal", e)
338 except rospy.ROSException
as e2:
339 rospy.logerr(
"Couldn't find goal type for action " + action_name, e2)
def message_callback(self, message)
def _recursive_create_items(self, parent, idx, topic_name, type_name, message, top_level=False)
def update_value(self, topic_name, message)
def get_correct_name(topic_name)
def correct_type(node, typemessage)
def _extract_array_info(type_str)
def __init__(self, server, parent, idx, topic_name, topic_type)
def numberofsubscribers(nametolookfor, topicsDict)
def nextname(hierachy, index_of_last_processed)
def opcua_update_callback(self, parent)
def get_goal_type(action_name)
def refresh_dict(namespace_ros, actionsdict, topicsdict, server, idx_actions)
def recursive_create_objects(self, topic_name, idx, parent)
def create_message_instance(self, node)
def get_feedback_type(action_name)
def recursive_delete_items(self, item)
def refresh_topics_and_actions(namespace_ros, server, topicsdict, actionsdict, idx_topics, idx_actions, topics, actions)
def own_rosnode_cleanup()
def _create_node_with_type(parent, idx, topic_name, topic_text, type_name, array_size)