2 High level node object, to access node attribute 3 and browse address space 13 High level node object, to access node attribute, 14 browse and populate address space. 15 Node objects are usefull as-is but they do not expose the entire 16 OPC-UA protocol. Feel free to look at the code of this class and call 17 directly UA services methods to optimize your code 23 if isinstance(nodeid, Node):
24 self.
nodeid = nodeid.nodeid
27 elif type(nodeid)
in (str, bytes):
28 self.
nodeid = ua.NodeId.from_string(nodeid)
29 elif isinstance(nodeid, int):
32 raise ua.UaError(
"argument to node must be a NodeId object or a string defining a nodeid found {0} of type {1}".format(nodeid, type(nodeid)))
35 if isinstance(other, Node)
and self.
nodeid == other.nodeid:
40 return not self.
__eq__(other)
43 return "Node({0})".format(self.
nodeid)
47 return self.nodeid.__hash__()
51 Get browse name of a node. A browse name is a QualifiedName object 52 composed of a string(name) and a namespace index. 55 return result.Value.Value
59 get description attribute of node 62 return result.Value.Value
66 get data type of node as NodeId 69 return result.Value.Value
73 get data type of node as VariantType 74 This only works if node is a variable, otherwise type 75 may not be convertible to VariantType 78 return ua.datatype_to_varianttype(result.Value.Value)
82 Get the access level attribute of the node as a set of AccessLevel enum values. 85 return ua.AccessLevel.parse_bitfield(result.Value.Value)
89 Get the user access level attribute of the node as a set of AccessLevel enum values. 92 return ua.AccessLevel.parse_bitfield(result.Value.Value)
96 Get the event notifier attribute of the node as a set of EventNotifier enum values. 99 return ua.EventNotifier.parse_bitfield(result.Value.Value)
103 Set the event notifier attribute. 105 :param values: an iterable of EventNotifier enum values. 107 event_notifier_bitfield = ua.EventNotifier.to_bitfield(values)
112 get node class attribute of node 115 return result.Value.Value
119 get description attribute class of node 122 return result.Value.Value
126 Get value of a node as a python type. Only variables ( and properties) have values. 127 An exception will be generated for other node types. 130 return result.Value.Value
134 Get value of a node as a DataValue object. Only variables (and properties) have values. 135 An exception will be generated for other node types. 136 DataValue contain a variable value as a variant as well as server and source timestamps 142 Set attribute ArrayDimensions of node 143 make sure it has the correct data type 150 Read and return ArrayDimensions attribute of node 153 return res.Value.Value
157 Set attribute ArrayDimensions of node 164 Read and return ArrayDimensions attribute of node 167 return res.Value.Value
171 Set value of a node. Only variables(properties) have values. 172 An exception will be generated for other node types. 173 value argument is either: 174 * a python built-in type, converted to opc-ua 175 optionnaly using the variantype argument. 176 * a ua.Variant, varianttype is then ignored 177 * a ua.DataValue, you then have full control over data send to server 188 set_data_value = set_value
192 Set node as writable by clients. 193 A node is always writable on server side. 196 self.
set_attr_bit(ua.AttributeIds.AccessLevel, ua.AccessLevel.CurrentWrite)
197 self.
set_attr_bit(ua.AttributeIds.UserAccessLevel, ua.AccessLevel.CurrentWrite)
199 self.
unset_attr_bit(ua.AttributeIds.AccessLevel, ua.AccessLevel.CurrentWrite)
200 self.
unset_attr_bit(ua.AttributeIds.UserAccessLevel, ua.AccessLevel.CurrentWrite)
204 val.Value.Value = ua.ua_binary.set_bit(val.Value.Value, bit)
209 val.Value.Value = ua.ua_binary.unset_bit(val.Value.Value, bit)
214 Set a node as read-only for clients. 215 A node is always writable on server side. 221 Set an attribute of a node 222 attributeid is a member of ua.AttributeIds 223 datavalue is a ua.DataValue object 227 attr.AttributeId = attributeid
228 attr.Value = datavalue
230 params.NodesToWrite = [attr]
231 result = self.server.write(params)
236 Read one attribute of a node 237 result code from server is checked and an exception is raised in case of error 241 rv.AttributeId = attr
243 params.NodesToRead.append(rv)
244 result = self.server.read(params)
245 result[0].StatusCode.check()
250 Read several attributes of a node 251 list of DataValue is returned 257 rv.AttributeId = attr
258 params.NodesToRead.append(rv)
260 results = self.server.read(params)
263 def get_children(self, refs=ua.ObjectIds.HierarchicalReferences, nodeclassmask=ua.NodeClass.Unspecified):
265 Get all children of a node. By default hierarchical references and all node classes are returned. 266 Other reference types may be given: 268 NonHierarchicalReferences = 32 269 HierarchicalReferences = 33 273 HasModellingRule = 37 276 HasTypeDefinition = 40 283 HasOrderedComponent = 49 289 return properties of node. 290 properties are child nodes with a reference of type HasProperty and a NodeClass of Variable 292 return self.
get_children(refs=ua.ObjectIds.HasProperty, nodeclassmask=ua.NodeClass.Variable)
296 return variables of node. 297 properties are child nodes with a reference of type HasComponent and a NodeClass of Variable 299 return self.
get_children(refs=ua.ObjectIds.HasComponent, nodeclassmask=ua.NodeClass.Variable)
303 return methods of node. 304 properties are child nodes with a reference of type HasComponent and a NodeClass of Method 306 return self.
get_children(refs=ua.ObjectIds.HasComponent, nodeclassmask=ua.NodeClass.Method)
308 def get_children_descriptions(self, refs=ua.ObjectIds.HierarchicalReferences, nodeclassmask=ua.NodeClass.Unspecified, includesubtypes=True):
309 return self.
get_references(refs, ua.BrowseDirection.Forward, nodeclassmask, includesubtypes)
311 def get_references(self, refs=ua.ObjectIds.References, direction=ua.BrowseDirection.Both, nodeclassmask=ua.NodeClass.Unspecified, includesubtypes=True):
313 returns references of the node based on specific filter defined with: 315 refs = ObjectId of the Reference 316 direction = Browse direction for references 317 nodeclassmask = filter nodes based on specific class 318 includesubtypes = If true subtypes of the reference (ref) are also included 321 desc.BrowseDirection = direction
323 desc.IncludeSubtypes = includesubtypes
324 desc.NodeClassMask = nodeclassmask
325 desc.ResultMask = ua.BrowseResultMask.All
329 params.View.Timestamp = ua.get_win_epoch()
330 params.NodesToBrowse.append(desc)
331 results = self.server.browse(params)
332 return results[0].References
334 def get_referenced_nodes(self, refs=ua.ObjectIds.References, direction=ua.BrowseDirection.Both, nodeclassmask=ua.NodeClass.Unspecified, includesubtypes=True):
336 returns referenced nodes based on specific filter 337 Paramters are the same as for get_references 340 references = self.
get_references(refs, direction, nodeclassmask, includesubtypes)
342 for desc
in references:
349 returns type definition of the node. 351 references = self.
get_references(refs=ua.ObjectIds.HasTypeDefinition, direction=ua.BrowseDirection.Forward)
352 if len(references) == 0:
354 return references[0].NodeId
358 Attempt to find path of node from root node and return it as a list of strings. 359 There might several possible paths to a node, this function will return one 360 Some nodes may be missing references, so this method may 362 Since address space may have circular references, a max length is specified 366 path = [ref.BrowseName.to_string()
for ref
in path]
372 Attempt to find path of node from root node and return it as a list of Nodes. 373 There might several possible paths to a node, this function will return one 374 Some nodes may be missing references, so this method may 376 Since address space may have circular references, a max length is specified 380 path = [
Node(self.
server, ref.NodeId)
for ref
in path]
386 Attempt to find path of node from root node and return it as a list of Nodes. 387 There might several possible paths to a node, this function will return one 388 Some nodes may be missing references, so this method may 390 Since address space may have circular references, a max length is specified 396 refs = node.get_references(refs=ua.ObjectIds.HierarchicalReferences, direction=ua.BrowseDirection.Inverse)
398 path.insert(0, refs[0])
400 if len(path) >= (max_length -1):
407 returns parent of the node. 408 A Node may have several parents, the first found is returned. 409 This method uses reverse references, a node might be missing such a link, 410 thus we will not find its parent. 412 refs = self.
get_references(refs=ua.ObjectIds.HierarchicalReferences, direction=ua.BrowseDirection.Inverse)
420 get a child specified by its path from this node. 422 * a string representing a qualified name. 425 * a list of qualified names 427 if type(path)
not in (list, tuple):
431 bpath.StartingNode = self.
nodeid 432 bpath.RelativePath = rpath
433 result = self.server.translate_browsepaths_to_nodeids([bpath])
435 result.StatusCode.check()
437 return Node(self.
server, result.Targets[0].TargetId)
445 el.IncludeSubtypes =
True 449 el.TargetName = ua.QualifiedName.from_string(item)
450 rpath.Elements.append(el)
455 Read raw history of a node 456 result code from server is checked and an exception is raised in case of error 457 If numvalues is > 0 and number of events in period is > numvalues 458 then result will be truncated 461 details.IsReadModified =
False 463 details.StartTime = starttime
465 details.StartTime = ua.get_win_epoch()
467 details.EndTime = endtime
469 details.EndTime = ua.get_win_epoch()
470 details.NumValuesPerNode = numvalues
471 details.ReturnBounds =
True 473 return result.HistoryData.DataValues
477 Read raw history of a node, low-level function 478 result code from server is checked and an exception is raised in case of error 481 valueid.NodeId = self.
nodeid 482 valueid.IndexRange =
'' 485 params.HistoryReadDetails = details
486 params.TimestampsToReturn = ua.TimestampsToReturn.Both
487 params.ReleaseContinuationPoints =
False 488 params.NodesToRead.append(valueid)
489 result = self.server.history_read(params)[0]
492 def read_event_history(self, starttime=None, endtime=None, numvalues=0, evtypes=ua.ObjectIds.BaseEventType):
494 Read event history of a source node 495 result code from server is checked and an exception is raised in case of error 496 If numvalues is > 0 and number of events in period is > numvalues 497 then result will be truncated 502 details.StartTime = starttime
504 details.StartTime = ua.get_win_epoch()
506 details.EndTime = endtime
508 details.EndTime = ua.get_win_epoch()
509 details.NumValuesPerNode = numvalues
511 if not isinstance(evtypes, (list, tuple)):
514 evtypes = [
Node(self.
server, evtype)
for evtype
in evtypes]
516 evfilter = events.get_filter_from_event_type(evtypes)
517 details.Filter = evfilter
521 for res
in result.HistoryData.Events:
522 event_res.append(events.Event.from_event_fields(evfilter.SelectClauses, res.EventFields))
527 Read event history of a node, low-level function 528 result code from server is checked and an exception is raised in case of error 531 valueid.NodeId = self.
nodeid 532 valueid.IndexRange =
'' 535 params.HistoryReadDetails = details
536 params.TimestampsToReturn = ua.TimestampsToReturn.Both
537 params.ReleaseContinuationPoints =
False 538 params.NodesToRead.append(valueid)
539 result = self.server.history_read(params)[0]
542 def delete(self, delete_references=True):
544 Delete node from address space 547 ditem.NodeId = self.
nodeid 548 ditem.DeleteTargetReferences = delete_references
550 params.NodesToDelete = [ditem]
551 result = self.server.delete_nodes(params)
560 def add_variable(self, nodeid, bname, val, varianttype=None, datatype=None):
572 def add_property(self, nodeid, bname, val, varianttype=None, datatype=None):
def read_event_history(self, starttime=None, endtime=None, numvalues=0, evtypes=ua.ObjectIds.BaseEventType)
def add_variable(self, nodeid, bname, val, varianttype=None, datatype=None)
def set_array_dimensions(self, value)
def get_child(self, path)
def _get_path(self, max_length=20)
def set_value_rank(self, value)
def add_variable_type(self, nodeid, bname, datatype)
def get_event_notifier(self)
def get_path(self, max_length=20)
def get_children(self, refs=ua.ObjectIds.HierarchicalReferences, nodeclassmask=ua.NodeClass.Unspecified)
def get_description(self)
def delete(self, delete_references=True)
def __init__(self, server, nodeid)
def get_data_type_as_variant_type(self)
def set_attribute(self, attributeid, datavalue)
def get_referenced_nodes(self, refs=ua.ObjectIds.References, direction=ua.BrowseDirection.Both, nodeclassmask=ua.NodeClass.Unspecified, includesubtypes=True)
def set_attr_bit(self, attr, bit)
def get_path_as_string(self, max_length=20)
def create_object(parent, nodeid, bname, objecttype=None)
def history_read(self, details)
def get_access_level(self)
def call_method(parent, methodid, args)
def get_display_name(self)
def add_data_type(self, nodeid, bname, description=None)
def add_folder(self, nodeid, bname)
def set_writable(self, writable=True)
def create_folder(parent, nodeid, bname)
def get_attribute(self, attr)
def create_data_type(parent, nodeid, bname, description=None)
def add_property(self, nodeid, bname, val, varianttype=None, datatype=None)
def add_method(self, args)
def get_browse_name(self)
def add_object_type(self, nodeid, bname)
def create_property(parent, nodeid, bname, val, varianttype=None, datatype=None)
def set_event_notifier(self, values)
def get_array_dimensions(self)
def history_read_events(self, details)
def unset_attr_bit(self, attr, bit)
def _make_relative_path(self, path)
def add_reference_type(self, parent, nodeid, bname)
def get_user_access_level(self)
def call_method(self, methodid, args)
def create_object_type(parent, nodeid, bname)
def create_reference_type(parent, nodeid, bname)
def create_variable_type(parent, nodeid, bname, datatype)
def get_attributes(self, attrs)
def add_object(self, nodeid, bname, objecttype=None)
def create_variable(parent, nodeid, bname, val, varianttype=None, datatype=None)
def create_method(parent, args)
def get_type_definition(self)
def read_raw_history(self, starttime=None, endtime=None, numvalues=0)
def get_references(self, refs=ua.ObjectIds.References, direction=ua.BrowseDirection.Both, nodeclassmask=ua.NodeClass.Unspecified, includesubtypes=True)
def get_children_descriptions(self, refs=ua.ObjectIds.HierarchicalReferences, nodeclassmask=ua.NodeClass.Unspecified, includesubtypes=True)
def set_value(self, value, varianttype=None)