12 from opcua
import ua, uamethod, common
18 def __init__(self, server, parent, idx, service_name, service_class):
29 for module
in (math, random, time):
30 self._eval_locals.update(module.__dict__)
40 rospy.loginfo(
"Created ROS Service with name: %s", self.
name)
45 rospy.loginfo(
"Calling Service with name: " + self.
name)
47 rospy.logdebug(
"Created Input Request for Service " + self.
name +
" : " + str(input_msg))
48 response = self.
proxy(input_msg)
49 rospy.logdebug(
"got response: " + str(response))
50 rospy.logdebug(
"Creating response message object")
52 for slot
in response.__slots__:
53 rospy.logdebug(
"Converting slot: " + str(getattr(response, slot)))
54 return_values.append(getattr(response, slot))
55 rospy.logdebug(
"Current Response list: " + str(return_values))
57 except (TypeError, rospy.ROSException, rospy.ROSInternalException, rospy.ROSSerializationException,
58 common.uaerrors.UaError, rosservice.ROSServiceException)
as e:
59 rospy.logerr(
"Error when calling service " + self.
name, e)
62 rospy.logdebug(
"Creating message for goal call")
64 if isinstance(inputs, tuple):
67 while (arg_counter < len(inputs)
and object_counter < len(sample.__slots__)):
68 cur_arg = inputs[arg_counter]
69 cur_slot = sample.__slots__[object_counter]
70 real_slot = getattr(sample, cur_slot)
72 "cur_arg: " + str(cur_arg) +
" cur_slot_name: " + str(cur_slot) +
" real slot content: " + str(
74 if hasattr(real_slot,
'_type'):
75 rospy.logdebug(
"We found an object with name " + str(cur_slot) +
", creating it recursively")
77 arg_counter, inputs, sample)
80 already_set.append(cur_slot)
82 setattr(sample, cur_slot, cur_arg)
89 rospy.loginfo(
"Create Object Instance Notify")
91 while (object_counter < len(object.__slots__)
and counter < len(inputs)):
92 cur_arg = inputs[counter]
93 cur_slot = object.__slots__[object_counter]
94 real_slot = getattr(object, cur_slot)
96 "cur_arg: " + str(cur_arg) +
" cur_slot_name: " + str(cur_slot) +
" real slot content: " + str(
98 if hasattr(real_slot,
'_type'):
99 rospy.logdebug(
"Recursive Object found in request/response of service call")
104 already_set.append(cur_slot)
105 setattr(object, cur_slot, cur_arg)
109 setattr(sample, name, object)
110 return already_set, counter
114 for child
in item.get_children():
118 self.server.server.delete_nodes([child])
119 self.server.server.delete_nodes([self.
method])
123 hierachy = name.split(
'/')
124 if len(hierachy) == 0
or len(hierachy) == 1:
126 for name
in hierachy:
129 nodewithsamename = self.server.find_service_node_with_same_name(name, idx)
130 rospy.logdebug(
"nodewithsamename for name: " + str(name) +
" is : " + str(nodewithsamename))
131 if nodewithsamename
is not None:
132 rospy.logdebug(
"recursive call for same name for: " + name)
136 newparent = parent.add_object(
137 ua.NodeId(name, parent.nodeid.NamespaceIndex, ua.NodeIdType.String),
138 ua.QualifiedName(name, parent.nodeid.NamespaceIndex))
141 except IndexError, common.uaerrors.UaError:
142 newparent = parent.add_object(
143 ua.NodeId(name + str(random.randint(0, 10000)), parent.nodeid.NamespaceIndex,
144 ua.NodeIdType.String),
145 ua.QualifiedName(name, parent.nodeid.NamespaceIndex))
153 for slot_name
in sample_req.__slots__:
154 slot = getattr(sample_req, slot_name)
155 if hasattr(slot,
'_type'):
157 array.extend(array_to_merge)
159 if isinstance(slot, list):
164 arg.ArrayDimensions = [1]
165 arg.Description = ua.LocalizedText(
"Array")
171 arg.ArrayDimensions = []
172 arg.Description = ua.LocalizedText(slot_name)
179 rosservices = rosservice.get_service_list(namespace=namespace_ros)
181 for service_name_ros
in rosservices:
183 if service_name_ros
not in servicesdict
or servicesdict[service_name_ros]
is None:
184 service =
OpcUaROSService(server, services_object_opc, idx, service_name_ros,
185 rosservice.get_service_class_by_name(service_name_ros))
186 servicesdict[service_name_ros] = service
187 except (rosservice.ROSServiceException, rosservice.ROSServiceIOException)
as e:
189 rospy.logerr(
"Error when trying to refresh services", e)
190 except TypeError
as e2:
191 rospy.logerr(
"Error when logging an Exception, can't convert everything to string")
194 rosservices = rosservice.get_service_list()
195 for service_nameOPC
in servicesdict:
197 for rosservice_name
in rosservices:
198 if service_nameOPC == rosservice_name:
200 if not found
and servicesdict[service_nameOPC]
is not None:
201 servicesdict[service_nameOPC].recursive_delete_items(
202 server.server.get_node(ua.NodeId(service_nameOPC, idx)))
203 tobedeleted.append(service_nameOPC)
204 if len(servicesdict[service_nameOPC].parent.get_children()) == 0:
205 server.server.delete_nodes([servicesdict[service_nameOPC].parent])
206 for name
in tobedeleted:
207 del servicesdict[name]
211 if type_name ==
'bool':
212 dv = ua.ObjectIds.Boolean
213 elif type_name ==
'byte':
214 dv = ua.ObjectIds.Byte
215 elif type_name ==
'int':
216 dv = ua.ObjectIds.Int16
217 elif type_name ==
'int8':
218 dv = ua.ObjectIds.SByte
219 elif type_name ==
'uint8':
220 dv = ua.ObjectIds.Byte
221 elif type_name ==
'int16':
222 dv = ua.ObjectIds.Int16
223 rospy.roswarn(
"Int16??")
224 elif type_name ==
'uint16':
225 dv = ua.ObjectIds.UInt16
226 elif type_name ==
'int32':
227 dv = ua.ObjectIds.Int32
228 elif type_name ==
'uint32':
229 dv = ua.ObjectIds.UInt32
230 elif type_name ==
'int64':
231 dv = ua.ObjectIds.Int64
232 elif type_name ==
'uint64':
233 dv = ua.ObjectIds.UInt64
234 elif type_name ==
'float' or type_name ==
'float32' or type_name ==
'float64':
235 dv = ua.ObjectIds.Float
236 elif type_name ==
'double':
237 dv = ua.ObjectIds.Double
238 elif type_name ==
'string' or type_name ==
'str':
239 dv = ua.ObjectIds.String
240 elif type_name ==
'array':
241 dv = ua.ObjectIds.Enumeration
242 elif type_name ==
'Time' or type_name ==
'time':
243 dv = ua.ObjectIds.Time
245 rospy.logerr(
"Can't create type with name " + type_name)
def refresh_services(namespace_ros, server, servicesdict, idx, services_object_opc)
def recursive_create_objects(self, name, idx, parent)
def recursive_delete_items(self, item)
def nextname(hierachy, index_of_last_processed)
def create_message_instance(self, inputs, sample)
def getobjectidfromtype(type_name)
def __init__(self, server, parent, idx, service_name, service_class)
def getargarray(sample_req)
def own_rosnode_cleanup()
def call_service(self, parent, inputs)
def create_object_instance(self, already_set, object, name, counter, inputs, sample)