copy_node.py
Go to the documentation of this file.
1 from opcua import ua
2 from opcua.common.node import Node
3 
4 
5 def copy_node(parent, node, nodeid=None, recursive=True):
6  """
7  Copy a node or node tree as child of parent node
8  """
9  print("Copying", node, "into ", parent)
10  rdesc = _rdesc_from_node(parent, node)
11 
12  if nodeid is None:
13  nodeid = ua.NodeId(namespaceidx=node.nodeid.NamespaceIndex)
14  added_nodeids = _copy_node(parent.server, parent.nodeid, rdesc, nodeid, recursive)
15  return [Node(parent.server, nid) for nid in added_nodeids]
16 
17 
18 def _copy_node(server, parent_nodeid, rdesc, nodeid, recursive):
19  addnode = ua.AddNodesItem()
20  addnode.RequestedNewNodeId = nodeid
21  addnode.BrowseName = rdesc.BrowseName
22  addnode.ParentNodeId = parent_nodeid
23  addnode.ReferenceTypeId = rdesc.ReferenceTypeId
24  addnode.TypeDefinition = rdesc.TypeDefinition
25  addnode.NodeClass = rdesc.NodeClass
26 
27  node_to_copy = Node(server, rdesc.NodeId)
28 
29  attrObj = getattr(ua, rdesc.NodeClass.name + "Attributes")
30  _read_and_copy_attrs(node_to_copy, attrObj(), addnode)
31 
32  res = server.add_nodes([addnode])[0]
33 
34  added_nodes = [res.AddedNodeId]
35 
36  if recursive:
37  descs = node_to_copy.get_children_descriptions()
38  for desc in descs:
39  nodes = _copy_node(server, res.AddedNodeId, desc, nodeid=ua.NodeId(namespaceidx=desc.NodeId.NamespaceIndex), recursive=True)
40  added_nodes.extend(nodes)
41 
42  return added_nodes
43 
44 
45 def _rdesc_from_node(parent, node):
46  results = node.get_attributes([ua.AttributeIds.NodeClass, ua.AttributeIds.BrowseName, ua.AttributeIds.DisplayName])
47  nclass, qname, dname = [res.Value.Value for res in results]
48 
49  rdesc = ua.ReferenceDescription()
50  rdesc.NodeId = node.nodeid
51  rdesc.BrowseName = qname
52  rdesc.DisplayName = dname
53  rdesc.NodeClass = nclass
54  if parent.get_type_definition() == ua.NodeId(ua.ObjectIds.FolderType):
55  rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.Organizes)
56  else:
57  rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasComponent)
58  typedef = node.get_type_definition()
59  if typedef:
60  rdesc.TypeDefinition = typedef
61  return rdesc
62 
63 
64 def _read_and_copy_attrs(node_type, struct, addnode):
65  names = [name for name in struct.__dict__.keys() if not name.startswith("_") and name not in ("BodyLength", "TypeId", "SpecifiedAttributes", "Encoding", "IsAbstract", "EventNotifier")]
66  attrs = [getattr(ua.AttributeIds, name) for name in names]
67  for name in names:
68  results = node_type.get_attributes(attrs)
69  for idx, name in enumerate(names):
70  if results[idx].StatusCode.is_good():
71  if name == "Value":
72  setattr(struct, name, results[idx].Value)
73  else:
74  setattr(struct, name, results[idx].Value.Value)
75  else:
76  print("Instantiate: while copying attributes from node type {0!s}, attribute {1!s}, statuscode is {2!s}".format(node_type, name, results[idx].StatusCode))
77  addnode.NodeAttributes = struct
def _rdesc_from_node(parent, node)
Definition: copy_node.py:45
def _copy_node(server, parent_nodeid, rdesc, nodeid, recursive)
Definition: copy_node.py:18
def copy_node(parent, node, nodeid=None, recursive=True)
Definition: copy_node.py:5
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:43