instantiate.py
Go to the documentation of this file.
1 """
2 Instantiate a new node and its child nodes from a node type.
3 """
4 
5 
6 from opcua import Node
7 from opcua import ua
8 from opcua.common import ua_utils
9 from opcua.common.copy_node import _rdesc_from_node, _read_and_copy_attrs
10 
11 
12 def instantiate(parent, node_type, nodeid=None, bname=None, idx=0):
13  """
14  instantiate a node type under a parent node.
15  nodeid and browse name of new node can be specified, or just namespace index
16  If they exists children of the node type, such as components, variables and
17  properties are also instantiated
18  """
19  rdesc = _rdesc_from_node(parent, node_type)
20  rdesc.TypeDefinition = node_type.nodeid
21 
22  if nodeid is None:
23  nodeid = ua.NodeId(namespaceidx=idx) # will trigger automatic node generation in namespace idx
24  if bname is None:
25  bname = rdesc.BrowseName
26  elif isinstance(bname, str):
27  bname = ua.QualifiedName.from_string(bname)
28 
29  nodeids = _instantiate_node(parent.server, parent.nodeid, rdesc, nodeid, bname)
30  return [Node(parent.server, nid) for nid in nodeids]
31 
32 
33 def _instantiate_node(server, parentid, rdesc, nodeid, bname, recursive=True):
34  """
35  instantiate a node type under parent
36  """
37  node_type = Node(server, rdesc.NodeId)
38  refs = node_type.get_referenced_nodes(refs=ua.ObjectIds.HasModellingRule)
39 
40  # skip optional elements
41  if len(refs) == 1 and refs[0].nodeid == ua.NodeId(ua.ObjectIds.ModellingRule_Optional):
42  return []
43 
44  addnode = ua.AddNodesItem()
45  addnode.RequestedNewNodeId = nodeid
46  addnode.BrowseName = bname
47  addnode.ParentNodeId = parentid
48  addnode.ReferenceTypeId = rdesc.ReferenceTypeId
49  addnode.TypeDefinition = rdesc.TypeDefinition
50 
51  if rdesc.NodeClass in (ua.NodeClass.Object, ua.NodeClass.ObjectType):
52  addnode.NodeClass = ua.NodeClass.Object
53  _read_and_copy_attrs(node_type, ua.ObjectAttributes(), addnode)
54 
55  elif rdesc.NodeClass in (ua.NodeClass.Variable, ua.NodeClass.VariableType):
56  addnode.NodeClass = ua.NodeClass.Variable
57  _read_and_copy_attrs(node_type, ua.VariableAttributes(), addnode)
58  elif rdesc.NodeClass in (ua.NodeClass.Method,):
59  addnode.NodeClass = ua.NodeClass.Method
60  _read_and_copy_attrs(node_type, ua.MethodAttributes(), addnode)
61  else:
62  print("Instantiate: Node class not supported: ", rdesc.NodeClass)
63  return
64 
65  res = server.add_nodes([addnode])[0]
66  added_nodes = [res.AddedNodeId]
67 
68  if recursive:
69  parents = ua_utils.get_node_supertypes(node_type, includeitself=True)
70  node = Node(server, res.AddedNodeId)
71  for parent in parents:
72  descs = parent.get_children_descriptions(includesubtypes=False)
73  for c_rdesc in descs:
74  # skip items that already exists, prefer the 'lowest' one in object hierarchy
75  if not ua_utils.is_child_present(node, c_rdesc.BrowseName):
76  # if root node being instantiated has a String NodeId, create the children with a String NodeId
77  if res.AddedNodeId.NodeIdType is ua.NodeIdType.String:
78  inst_nodeid = res.AddedNodeId.Identifier + "." + c_rdesc.BrowseName.Name
79  nodeids = _instantiate_node(server, res.AddedNodeId, c_rdesc, nodeid=ua.NodeId(identifier=inst_nodeid, namespaceidx=res.AddedNodeId.NamespaceIndex), bname=c_rdesc.BrowseName)
80  else:
81  nodeids = _instantiate_node(server, res.AddedNodeId, c_rdesc, nodeid=ua.NodeId(namespaceidx=res.AddedNodeId.NamespaceIndex), bname=c_rdesc.BrowseName)
82  added_nodes.extend(nodeids)
83 
84  return added_nodes
85 
86 
def _rdesc_from_node(parent, node)
Definition: copy_node.py:45
def instantiate(parent, node_type, nodeid=None, bname=None, idx=0)
Definition: instantiate.py:12
def _instantiate_node(server, parentid, rdesc, nodeid, bname, recursive=True)
Definition: instantiate.py:33
def _read_and_copy_attrs(node_type, struct, addnode)
Definition: copy_node.py:64


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Tue Jan 19 2021 03:12:44