instantiate.py
Go to the documentation of this file.
00001 """
00002 Instantiate a new node and its child nodes from a node type.
00003 """
00004 
00005 
00006 from opcua import Node
00007 from opcua import ua
00008 from opcua.common import ua_utils
00009 from opcua.common.copy_node import _rdesc_from_node, _read_and_copy_attrs
00010 
00011 
00012 def instantiate(parent, node_type, nodeid=None, bname=None, idx=0):
00013     """
00014     instantiate a node type under a parent node.
00015     nodeid and browse name of new node can be specified, or just namespace index
00016     If they exists children of the node type, such as components, variables and
00017     properties are also instantiated
00018     """
00019     rdesc = _rdesc_from_node(parent, node_type)
00020     rdesc.TypeDefinition = node_type.nodeid
00021 
00022     if nodeid is None:
00023         nodeid = ua.NodeId(namespaceidx=idx)  # will trigger automatic node generation in namespace idx
00024     if bname is None:
00025         bname = rdesc.BrowseName
00026     elif isinstance(bname, str):
00027         bname = ua.QualifiedName.from_string(bname)
00028 
00029     nodeids = _instantiate_node(parent.server, parent.nodeid, rdesc, nodeid, bname)
00030     return [Node(parent.server, nid) for nid in nodeids]
00031 
00032 
00033 def _instantiate_node(server, parentid, rdesc, nodeid, bname, recursive=True):
00034     """
00035     instantiate a node type under parent
00036     """
00037     node_type = Node(server, rdesc.NodeId)
00038     refs = node_type.get_referenced_nodes(refs=ua.ObjectIds.HasModellingRule)
00039 
00040     # skip optional elements
00041     if len(refs) == 1 and refs[0].nodeid == ua.NodeId(ua.ObjectIds.ModellingRule_Optional):
00042         return []
00043 
00044     addnode = ua.AddNodesItem()
00045     addnode.RequestedNewNodeId = nodeid
00046     addnode.BrowseName = bname
00047     addnode.ParentNodeId = parentid
00048     addnode.ReferenceTypeId = rdesc.ReferenceTypeId
00049     addnode.TypeDefinition = rdesc.TypeDefinition
00050 
00051     if rdesc.NodeClass in (ua.NodeClass.Object, ua.NodeClass.ObjectType):
00052         addnode.NodeClass = ua.NodeClass.Object
00053         _read_and_copy_attrs(node_type, ua.ObjectAttributes(), addnode)
00054 
00055     elif rdesc.NodeClass in (ua.NodeClass.Variable, ua.NodeClass.VariableType):
00056         addnode.NodeClass = ua.NodeClass.Variable
00057         _read_and_copy_attrs(node_type, ua.VariableAttributes(), addnode)
00058     elif rdesc.NodeClass in (ua.NodeClass.Method,):
00059         addnode.NodeClass = ua.NodeClass.Method
00060         _read_and_copy_attrs(node_type, ua.MethodAttributes(), addnode)
00061     else:
00062         print("Instantiate: Node class not supported: ", rdesc.NodeClass)
00063         return
00064 
00065     res = server.add_nodes([addnode])[0]
00066     added_nodes = [res.AddedNodeId]
00067 
00068     if recursive:
00069         parents = ua_utils.get_node_supertypes(node_type, includeitself=True)
00070         node = Node(server, res.AddedNodeId)
00071         for parent in parents:
00072             descs = parent.get_children_descriptions(includesubtypes=False)
00073             for c_rdesc in descs:
00074                 # skip items that already exists, prefer the 'lowest' one in object hierarchy
00075                 if not ua_utils.is_child_present(node, c_rdesc.BrowseName):
00076                     # if root node being instantiated has a String NodeId, create the children with a String NodeId
00077                     if res.AddedNodeId.NodeIdType is ua.NodeIdType.String:
00078                         inst_nodeid = res.AddedNodeId.Identifier + "." + c_rdesc.BrowseName.Name
00079                         nodeids = _instantiate_node(server, res.AddedNodeId, c_rdesc, nodeid=ua.NodeId(identifier=inst_nodeid, namespaceidx=res.AddedNodeId.NamespaceIndex), bname=c_rdesc.BrowseName)
00080                     else:
00081                         nodeids = _instantiate_node(server, res.AddedNodeId, c_rdesc, nodeid=ua.NodeId(namespaceidx=res.AddedNodeId.NamespaceIndex), bname=c_rdesc.BrowseName)
00082                     added_nodes.extend(nodeids)
00083 
00084     return added_nodes
00085 
00086 


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Sat Jun 8 2019 18:26:23