MCPcopy Create free account
hub / github.com/FreeOpcUa/python-opcua / _instantiate_node

Function _instantiate_node

opcua/common/instantiate.py:44–129  ·  view source on GitHub ↗

instantiate a node type under parent

(server,
                      node_type,
                      parentid,
                      rdesc,
                      nodeid,
                      bname,
                      dname=None,
                      recursive=True,
                      instantiate_optional=True)

Source from the content-addressed store, hash-verified

42
43
44def _instantiate_node(server,
45 node_type,
46 parentid,
47 rdesc,
48 nodeid,
49 bname,
50 dname=None,
51 recursive=True,
52 instantiate_optional=True):
53 """
54 instantiate a node type under parent
55 """
56 addnode = ua.AddNodesItem()
57 addnode.RequestedNewNodeId = nodeid
58 addnode.BrowseName = bname
59 addnode.ParentNodeId = parentid
60 addnode.ReferenceTypeId = rdesc.ReferenceTypeId
61 addnode.TypeDefinition = rdesc.TypeDefinition
62
63 if rdesc.NodeClass in (ua.NodeClass.Object, ua.NodeClass.ObjectType):
64 addnode.NodeClass = ua.NodeClass.Object
65 _read_and_copy_attrs(node_type, ua.ObjectAttributes(), addnode)
66
67 elif rdesc.NodeClass in (ua.NodeClass.Variable, ua.NodeClass.VariableType):
68 addnode.NodeClass = ua.NodeClass.Variable
69 _read_and_copy_attrs(node_type, ua.VariableAttributes(), addnode)
70 elif rdesc.NodeClass in (ua.NodeClass.Method, ):
71 addnode.NodeClass = ua.NodeClass.Method
72 _read_and_copy_attrs(node_type, ua.MethodAttributes(), addnode)
73 elif rdesc.NodeClass in (ua.NodeClass.DataType, ):
74 addnode.NodeClass = ua.NodeClass.DataType
75 _read_and_copy_attrs(node_type, ua.DataTypeAttributes(), addnode)
76 else:
77 logger.error("Instantiate: Node class not supported: %s", rdesc.NodeClass)
78 raise RuntimeError("Instantiate: Node class not supported")
79 return
80 if dname is not None:
81 addnode.NodeAttributes.DisplayName = dname
82
83 res = server.add_nodes([addnode])[0]
84 res.StatusCode.check()
85 added_nodes = [res.AddedNodeId]
86
87 if recursive:
88 parents = ua_utils.get_node_supertypes(node_type, includeitself=True)
89 node = Node(server, res.AddedNodeId)
90 for parent in parents:
91 descs = parent.get_children_descriptions(includesubtypes=False)
92 for c_rdesc in descs:
93 # skip items that already exists, prefer the 'lowest' one in object hierarchy
94 if not ua_utils.is_child_present(node, c_rdesc.BrowseName):
95
96 c_node_type = Node(server, c_rdesc.NodeId)
97 refs = c_node_type.get_referenced_nodes(refs=ua.ObjectIds.HasModellingRule)
98 if not refs:
99 # spec says to ignore nodes without modelling rules
100 logger.info("Instantiate: Skip node without modelling rule %s as part of %s", c_rdesc.BrowseName, addnode.BrowseName)
101 continue

Callers 1

instantiateFunction · 0.85

Calls 6

get_referenced_nodesMethod · 0.95
_read_and_copy_attrsFunction · 0.90
NodeClass · 0.90
checkMethod · 0.80
add_nodesMethod · 0.45

Tested by

no test coverage detected