def instantiate(parent, node_type, nodeid=None, bname=None, idx=0): """ instantiate a node type under a parent node. nodeid and browse name of new node can be specified, or just namespace index If they exists children of the node type, such as components, variables and properties are also instantiated """ results = node_type.get_attributes([ua.AttributeIds.NodeClass, ua.AttributeIds.BrowseName, ua.AttributeIds.DisplayName]) nclass, qname, dname = [res.Value.Value for res in results] rdesc = ua.ReferenceDescription() rdesc.NodeId = node_type.nodeid rdesc.BrowseName = qname rdesc.DisplayName = dname rdesc.NodeClass = nclass if parent.get_type_definition() == ua.NodeId(ua.ObjectIds.FolderType): rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.Organizes) else: rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasComponent) rdesc.TypeDefinition = node_type.nodeid if nodeid is None: nodeid = ua.NodeId(namespaceidx=idx) # will trigger automatic node generation in namespace idx if bname is None: bname = rdesc.BrowseName elif isinstance(bname, str): bname = ua.QualifiedName.from_string(bname) return _instantiate_node(parent.server, parent.nodeid, rdesc, nodeid, bname)
def instanciate_node(parent, node_type, idx): """ Instanciate a new node under 'parent' using a type """ results = node_type.get_attributes([ ua.AttributeIds.NodeClass, ua.AttributeIds.BrowseName, ua.AttributeIds.DisplayName ]) nclass, bname, dname = [res.Value.Value for res in results] #descs = node_type.get_children_descriptions(refs=ua.ObjectIds.HasTypeDefinition) typedef = ua.FourByteNodeId(ua.ObjectIds.BaseObjectType) #if len(descs) > 1: #print("DESCS", descs) #typedef = descs[0].TypeDefinition rdesc = ua.ReferenceDescription() rdesc.NodeId = node_type.nodeid rdesc.BrowseName = bname rdesc.DisplayName = dname rdesc.NodeClass = nclass rdesc.ReferenceTypeId = ua.TwoByteNodeId(ua.ObjectIds.HasComponent) rdesc.TypeDefinition = typedef print("MYRDESC", rdesc) return _instanciate_node(parent.server, parent.nodeid, rdesc, idx)
def _get_node_desc(self, node): attrs = node.get_attributes([ua.AttributeIds.DisplayName, ua.AttributeIds.BrowseName, ua.AttributeIds.NodeId, ua.AttributeIds.NodeClass]) desc = ua.ReferenceDescription() desc.DisplayName = attrs[0].Value.Value desc.BrowseName = attrs[1].Value.Value desc.NodeId = attrs[2].Value.Value desc.NodeClass = attrs[3].Value.Value desc.TypeDefinition = ua.TwoByteNodeId(ua.ObjectIds.FolderType) return desc
def _add_ref_from_parent(self, nodedata, item, parentdata): desc = ua.ReferenceDescription() desc.ReferenceTypeId = item.ReferenceTypeId desc.NodeId = nodedata.nodeid desc.NodeClass = item.NodeClass desc.BrowseName = item.BrowseName desc.DisplayName = item.NodeAttributes.DisplayName desc.TypeDefinition = item.TypeDefinition desc.IsForward = True self._add_unique_reference(parentdata, desc)
def _add_ref_from_parent(self, nodedata, item): desc = ua.ReferenceDescription() desc.ReferenceTypeId = item.ReferenceTypeId desc.NodeId = nodedata.nodeid desc.NodeClass = item.NodeClass desc.BrowseName = item.BrowseName desc.DisplayName = item.NodeAttributes.DisplayName desc.TypeDefinition = item.TypeDefinition desc.IsForward = True self._aspace[item.ParentNodeId].references.append(desc)
def get_root_node_and_desc(self): node = self.client.get_root_node() attrs = node.get_attributes([ua.AttributeIds.DisplayName, ua.AttributeIds.BrowseName, ua.AttributeIds.NodeId, ua.AttributeIds.NodeClass]) desc = ua.ReferenceDescription() desc.DisplayName = attrs[0].Value.Value desc.BrowseName = attrs[1].Value.Value desc.NodeId = attrs[2].Value.Value desc.NodeClass = attrs[3].Value.Value desc.TypeDefinition = ua.TwoByteNodeId(ua.ObjectIds.FolderType) return node, desc
def _add_node(self, item, user): result = ua.AddNodesResult() if item.RequestedNewNodeId in self._aspace: self.logger.warning("AddNodeItem: node already exists") result.StatusCode = ua.StatusCode(ua.StatusCodes.BadNodeIdExists) return result nodedata = NodeData(item.RequestedNewNodeId) # add common attrs nodedata.attributes[ua.AttributeIds.NodeId] = AttributeValue(ua.DataValue(ua.Variant(item.RequestedNewNodeId, ua.VariantType.NodeId))) nodedata.attributes[ua.AttributeIds.BrowseName] = AttributeValue(ua.DataValue(ua.Variant(item.BrowseName, ua.VariantType.QualifiedName))) nodedata.attributes[ua.AttributeIds.NodeClass] = AttributeValue(ua.DataValue(ua.Variant(item.NodeClass, ua.VariantType.Int32))) # add requested attrs self._add_nodeattributes(item.NodeAttributes, nodedata) # add parent if item.ParentNodeId == ua.NodeId(): #self.logger.warning("add_node: creating node %s without parent", item.RequestedNewNodeId) pass elif item.ParentNodeId not in self._aspace: #self.logger.warning("add_node: while adding node %s, requested parent node %s does not exists", item.RequestedNewNodeId, item.ParentNodeId) result.StatusCode = ua.StatusCode(ua.StatusCodes.BadParentNodeIdInvalid) return result else: if not user == User.Admin: result.StatusCode = ua.StatusCode(ua.StatusCodes.BadUserAccessDenied) return result desc = ua.ReferenceDescription() desc.ReferenceTypeId = item.ReferenceTypeId desc.NodeId = item.RequestedNewNodeId desc.NodeClass = item.NodeClass desc.BrowseName = item.BrowseName desc.DisplayName = ua.LocalizedText(item.BrowseName.Name) desc.TypeDefinition = item.TypeDefinition desc.IsForward = True self._aspace[item.ParentNodeId].references.append(desc) # now add our node to db self._aspace[item.RequestedNewNodeId] = nodedata # add type definition if item.TypeDefinition != ua.NodeId(): addref = ua.AddReferencesItem() addref.SourceNodeId = item.RequestedNewNodeId addref.IsForward = True addref.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasTypeDefinition) addref.TargetNodeId = item.TypeDefinition addref.TargetNodeClass = ua.NodeClass.DataType self._add_reference(addref, user) result.StatusCode = ua.StatusCode() result.AddedNodeId = item.RequestedNewNodeId return result
def _add_reference_no_check(self, sourcedata, addref): rdesc = ua.ReferenceDescription() rdesc.ReferenceTypeId = addref.ReferenceTypeId rdesc.IsForward = addref.IsForward rdesc.NodeId = addref.TargetNodeId rdesc.NodeClass = addref.TargetNodeClass bname = self._aspace.get_attribute_value(addref.TargetNodeId, ua.AttributeIds.BrowseName).Value.Value if bname: rdesc.BrowseName = bname dname = self._aspace.get_attribute_value(addref.TargetNodeId, ua.AttributeIds.DisplayName).Value.Value if dname: rdesc.DisplayName = dname return self._add_unique_reference(sourcedata, rdesc)
def _rdesc_from_node(parent, node): results = node.get_attributes([ua.AttributeIds.NodeClass, ua.AttributeIds.BrowseName, ua.AttributeIds.DisplayName]) nclass, qname, dname = [res.Value.Value for res in results] rdesc = ua.ReferenceDescription() rdesc.NodeId = node.nodeid rdesc.BrowseName = qname rdesc.DisplayName = dname rdesc.NodeClass = nclass if parent.get_type_definition() == ua.NodeId(ua.ObjectIds.FolderType): rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.Organizes) else: rdesc.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasComponent) rdesc.TypeDefinition = node.nodeid return rdesc
def _add_reference(self, addref, user): if addref.SourceNodeId not in self._aspace: return ua.StatusCode(ua.StatusCodes.BadSourceNodeIdInvalid) if addref.TargetNodeId not in self._aspace: return ua.StatusCode(ua.StatusCodes.BadTargetNodeIdInvalid) if user != User.Admin: return ua.StatusCode(ua.StatusCodes.BadUserAccessDenied) rdesc = ua.ReferenceDescription() rdesc.ReferenceTypeId = addref.ReferenceTypeId rdesc.IsForward = addref.IsForward rdesc.NodeId = addref.TargetNodeId rdesc.NodeClass = addref.TargetNodeClass bname = self._aspace.get_attribute_value(addref.TargetNodeId, ua.AttributeIds.BrowseName).Value.Value if bname: rdesc.BrowseName = bname dname = self._aspace.get_attribute_value(addref.TargetNodeId, ua.AttributeIds.DisplayName).Value.Value if dname: rdesc.DisplayName = dname return self._add_unique_reference(addref.SourceNodeId, rdesc)
def _make_default_ref(self): #FIXME: remeber last choosen values or use values that make sense ref = ua.ReferenceDescription() return ref
def _add_node(self, item, user): result = ua.AddNodesResult() # If Identifier of requested NodeId is null we generate a new NodeId using # the namespace of the nodeid, this is an extention of the spec to allow # to requests the server to generate a new nodeid in a specified namespace if item.RequestedNewNodeId.has_null_identifier(): self.logger.debug( "RequestedNewNodeId has null identifier, generating Identifier" ) nodedata = NodeData( self._aspace.generate_nodeid( item.RequestedNewNodeId.NamespaceIndex)) else: nodedata = NodeData(item.RequestedNewNodeId) if nodedata.nodeid in self._aspace: self.logger.warning("AddNodesItem: node already exists") result.StatusCode = ua.StatusCode(ua.StatusCodes.BadNodeIdExists) return result if item.ParentNodeId.is_null(): # self.logger.warning("add_node: creating node %s without parent", nodedata.nodeid) # We should return Error here, but the standard namespace seems to define many nodes # without parents, so ignore... pass elif item.ParentNodeId not in self._aspace: self.logger.warning( "add_node: while adding node %s, requested parent node %s does not exists", nodedata.nodeid, item.ParentNodeId) result.StatusCode = ua.StatusCode( ua.StatusCodes.BadParentNodeIdInvalid) return result if not user == User.Admin: result.StatusCode = ua.StatusCode( ua.StatusCodes.BadUserAccessDenied) return result # add common attrs nodedata.attributes[ua.AttributeIds.NodeId] = AttributeValue( ua.DataValue(ua.Variant(nodedata.nodeid, ua.VariantType.NodeId))) nodedata.attributes[ua.AttributeIds.BrowseName] = AttributeValue( ua.DataValue( ua.Variant(item.BrowseName, ua.VariantType.QualifiedName))) nodedata.attributes[ua.AttributeIds.NodeClass] = AttributeValue( ua.DataValue(ua.Variant(item.NodeClass, ua.VariantType.Int32))) # add requested attrs self._add_nodeattributes(item.NodeAttributes, nodedata) if not item.ParentNodeId.is_null(): desc = ua.ReferenceDescription() desc.ReferenceTypeId = item.ReferenceTypeId desc.NodeId = nodedata.nodeid desc.NodeClass = item.NodeClass desc.BrowseName = item.BrowseName desc.DisplayName = ua.LocalizedText(item.BrowseName.Name) desc.TypeDefinition = item.TypeDefinition desc.IsForward = True self._aspace[item.ParentNodeId].references.append(desc) # now add our node to db self._aspace[nodedata.nodeid] = nodedata # add type definition if item.TypeDefinition != ua.NodeId(): addref = ua.AddReferencesItem() addref.SourceNodeId = nodedata.nodeid addref.IsForward = True addref.ReferenceTypeId = ua.NodeId(ua.ObjectIds.HasTypeDefinition) addref.TargetNodeId = item.TypeDefinition addref.TargetNodeClass = ua.NodeClass.DataType self._add_reference(addref, user) result.StatusCode = ua.StatusCode() result.AddedNodeId = nodedata.nodeid return result