/// <summary> /// Creates a new variable type. /// </summary> private BaseVariableTypeState CreateVariableType(NodeState parent, IDictionary <NodeId, IList <IReference> > externalReferences, string path, string name, BuiltInType dataType, int valueRank) { BaseDataVariableTypeState type = new BaseDataVariableTypeState(); type.SymbolicName = name; type.SuperTypeId = VariableTypeIds.BaseDataVariableType; type.NodeId = new NodeId(path, NamespaceIndex); type.BrowseName = new QualifiedName(name, NamespaceIndex); type.DisplayName = type.BrowseName.Name; type.WriteMask = AttributeWriteMask.None; type.UserWriteMask = AttributeWriteMask.None; type.IsAbstract = false; type.DataType = (uint)dataType; type.ValueRank = valueRank; type.Value = null; IList <IReference> references = null; if (!externalReferences.TryGetValue(VariableTypeIds.BaseDataVariableType, out references)) { externalReferences[VariableTypeIds.BaseDataVariableType] = references = new List <IReference>(); } references.Add(new NodeStateReference(ReferenceTypes.HasSubtype, false, type.NodeId)); if (parent != null) { parent.AddReference(ReferenceTypes.Organizes, false, type.NodeId); type.AddReference(ReferenceTypes.Organizes, true, parent.NodeId); } AddPredefinedNode(SystemContext, type); return(type); }
/// <summary> /// Converts a ReferenceDescription to an IReference. /// </summary> private IReference ToReference(ReferenceDescription reference) { if (reference.NodeId.IsAbsolute || reference.TypeDefinition.IsAbsolute) { return(new NodeStateReference(reference.ReferenceTypeId, !reference.IsForward, reference.NodeId)); } if (m_source != null && (reference.NodeId == ObjectIds.ObjectsFolder || reference.NodeId == ObjectIds.Server)) { return(new NodeStateReference(reference.ReferenceTypeId, !reference.IsForward, m_rootId)); } NodeState target = null; switch (reference.NodeClass) { case NodeClass.DataType: { target = new DataTypeState(); break; } case NodeClass.Method: { target = new MethodState(null); break; } case NodeClass.Object: { target = new BaseObjectState(null); break; } case NodeClass.ObjectType: { target = new BaseObjectTypeState(); break; } case NodeClass.ReferenceType: { target = new ReferenceTypeState(); break; } case NodeClass.Variable: { target = new BaseDataVariableState(null); break; } case NodeClass.VariableType: { target = new BaseDataVariableTypeState(); break; } case NodeClass.View: { target = new ViewState(); break; } } target.NodeId = m_mapper.ToLocalId((NodeId)reference.NodeId); target.BrowseName = m_mapper.ToLocalName(reference.BrowseName); target.DisplayName = reference.DisplayName; if (target is BaseInstanceState) { ((BaseInstanceState)target).TypeDefinitionId = m_mapper.ToLocalId((NodeId)reference.TypeDefinition); } return(new NodeStateReference(reference.ReferenceTypeId, !reference.IsForward, target)); }
/// <summary> /// Imports a node from the set. /// </summary> private NodeState Import(ISystemContext context, UANode node) { NodeState importedNode = null; NodeClass nodeClass = NodeClass.Unspecified; if (node is UAObject) { nodeClass = NodeClass.Object; } else if (node is UAVariable) { nodeClass = NodeClass.Variable; } else if (node is UAMethod) { nodeClass = NodeClass.Method; } else if (node is UAObjectType) { nodeClass = NodeClass.ObjectType; } else if (node is UAVariableType) { nodeClass = NodeClass.VariableType; } else if (node is UADataType) { nodeClass = NodeClass.DataType; } else if (node is UAReferenceType) { nodeClass = NodeClass.ReferenceType; } else if (node is UAView) { nodeClass = NodeClass.View; } switch (nodeClass) { case NodeClass.Object: { UAObject o = (UAObject)node; BaseObjectState value = new BaseObjectState(null); value.EventNotifier = o.EventNotifier; importedNode = value; break; } case NodeClass.Variable: { UAVariable o = (UAVariable)node; NodeId typeDefinitionId = null; if (node.References != null) { for (int ii = 0; ii < node.References.Length; ii++) { Opc.Ua.NodeId referenceTypeId = ImportNodeId(node.References[ii].ReferenceType, context.NamespaceUris, true); bool isInverse = !node.References[ii].IsForward; Opc.Ua.ExpandedNodeId targetId = ImportExpandedNodeId(node.References[ii].Value, context.NamespaceUris, context.ServerUris); if (referenceTypeId == ReferenceTypeIds.HasTypeDefinition && !isInverse) { typeDefinitionId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris); break; } } } BaseVariableState value = null; if (typeDefinitionId == Opc.Ua.VariableTypeIds.PropertyType) { value = new PropertyState(null); } else { value = new BaseDataVariableState(null); } value.DataType = ImportNodeId(o.DataType, context.NamespaceUris, true); value.ValueRank = o.ValueRank; value.ArrayDimensions = ImportArrayDimensions(o.ArrayDimensions); value.AccessLevelEx = o.AccessLevel; value.UserAccessLevel = (byte)(o.AccessLevel & 0xFF); value.MinimumSamplingInterval = o.MinimumSamplingInterval; value.Historizing = o.Historizing; if (o.Value != null) { XmlDecoder decoder = CreateDecoder(context, o.Value); TypeInfo typeInfo = null; value.Value = decoder.ReadVariantContents(out typeInfo); decoder.Close(); } importedNode = value; break; } case NodeClass.Method: { UAMethod o = (UAMethod)node; MethodState value = new MethodState(null); value.Executable = o.Executable; value.UserExecutable = o.Executable; value.TypeDefinitionId = ImportNodeId(o.MethodDeclarationId, context.NamespaceUris, true); importedNode = value; break; } case NodeClass.View: { UAView o = (UAView)node; ViewState value = new ViewState(); value.ContainsNoLoops = o.ContainsNoLoops; importedNode = value; break; } case NodeClass.ObjectType: { UAObjectType o = (UAObjectType)node; BaseObjectTypeState value = new BaseObjectTypeState(); value.IsAbstract = o.IsAbstract; importedNode = value; break; } case NodeClass.VariableType: { UAVariableType o = (UAVariableType)node; BaseVariableTypeState value = new BaseDataVariableTypeState(); value.IsAbstract = o.IsAbstract; value.DataType = ImportNodeId(o.DataType, context.NamespaceUris, true); value.ValueRank = o.ValueRank; value.ArrayDimensions = ImportArrayDimensions(o.ArrayDimensions); if (o.Value != null) { XmlDecoder decoder = CreateDecoder(context, o.Value); TypeInfo typeInfo = null; value.Value = decoder.ReadVariantContents(out typeInfo); decoder.Close(); } importedNode = value; break; } case NodeClass.DataType: { UADataType o = (UADataType)node; DataTypeState value = new DataTypeState(); value.IsAbstract = o.IsAbstract; Opc.Ua.DataTypeDefinition dataTypeDefinition = Import(o, o.Definition, context.NamespaceUris); value.DataTypeDefinition = new ExtensionObject(dataTypeDefinition); value.Purpose = o.Purpose; value.DataTypeModifier = DataTypeModifier.None; if (o.Definition != null) { if (o.Definition.IsOptionSet) { value.DataTypeModifier = DataTypeModifier.OptionSet; } else if (o.Definition.IsUnion) { value.DataTypeModifier = DataTypeModifier.Union; } } importedNode = value; break; } case NodeClass.ReferenceType: { UAReferenceType o = (UAReferenceType)node; ReferenceTypeState value = new ReferenceTypeState(); value.IsAbstract = o.IsAbstract; value.InverseName = Import(o.InverseName); value.Symmetric = o.Symmetric; importedNode = value; break; } } importedNode.NodeId = ImportNodeId(node.NodeId, context.NamespaceUris, false); importedNode.BrowseName = ImportQualifiedName(node.BrowseName, context.NamespaceUris); importedNode.DisplayName = Import(node.DisplayName); if (importedNode.DisplayName == null) { importedNode.DisplayName = new Ua.LocalizedText(importedNode.BrowseName.Name); } importedNode.Description = Import(node.Description); importedNode.Categories = (node.Category != null && node.Category.Length > 0) ? node.Category : null; importedNode.ReleaseStatus = node.ReleaseStatus; importedNode.WriteMask = (AttributeWriteMask)node.WriteMask; importedNode.UserWriteMask = (AttributeWriteMask)node.UserWriteMask; importedNode.Extensions = node.Extensions; if (!String.IsNullOrEmpty(node.SymbolicName)) { importedNode.SymbolicName = node.SymbolicName; } if (node.References != null) { BaseInstanceState instance = importedNode as BaseInstanceState; BaseTypeState type = importedNode as BaseTypeState; for (int ii = 0; ii < node.References.Length; ii++) { Opc.Ua.NodeId referenceTypeId = ImportNodeId(node.References[ii].ReferenceType, context.NamespaceUris, true); bool isInverse = !node.References[ii].IsForward; Opc.Ua.ExpandedNodeId targetId = ImportExpandedNodeId(node.References[ii].Value, context.NamespaceUris, context.ServerUris); if (instance != null) { if (referenceTypeId == ReferenceTypeIds.HasModellingRule && !isInverse) { instance.ModellingRuleId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris); continue; } if (referenceTypeId == ReferenceTypeIds.HasTypeDefinition && !isInverse) { instance.TypeDefinitionId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris); continue; } } if (type != null) { if (referenceTypeId == ReferenceTypeIds.HasSubtype && isInverse) { type.SuperTypeId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris); continue; } } importedNode.AddReference(referenceTypeId, isInverse, targetId); } } return(importedNode); }
/// <summary> /// Convert to stack object /// </summary> /// <param name="nodeModel"></param> /// <param name="context"></param> /// <param name="parent"></param> /// <returns></returns> public static NodeState ToNodeState(this BaseNodeModel nodeModel, ISystemContext context, NodeState parent = null) { NodeState state; switch (nodeModel) { case ViewNodeModel viewState: state = new ViewState { ContainsNoLoops = viewState.ContainsNoLoops ?? false, EventNotifier = viewState.EventNotifier ?? EventNotifiers.None, }; break; case TypeNodeModel typeState: switch (typeState) { case VariableTypeNodeModel variableType: switch (variableType) { case DataVariableTypeNodeModel data: state = new BaseDataVariableTypeState(); break; case PropertyTypeNodeModel property: state = new PropertyTypeState(); break; default: return(null); } var baseVariableTypeState = state as BaseVariableTypeState; baseVariableTypeState.ArrayDimensions = variableType.ArrayDimensions; baseVariableTypeState.DataType = variableType.DataType; baseVariableTypeState.ValueRank = variableType.ValueRank ?? ValueRanks.Scalar; baseVariableTypeState.WrappedValue = variableType.Value ?? Variant.Null; break; case ObjectTypeNodeModel objectType: state = new BaseObjectTypeState(); break; case ReferenceTypeNodeModel referenceType: state = new ReferenceTypeState { Symmetric = referenceType.Symmetric ?? false, InverseName = referenceType.InverseName }; break; case DataTypeNodeModel dataType: state = new DataTypeState { // Definition = dataType.Definition.ToDataTypeDefinition() }; break; default: return(null); } var baseTypeState = state as BaseTypeState; baseTypeState.IsAbstract = typeState.IsAbstract ?? false; break; case InstanceNodeModel instanceState: switch (instanceState) { case VariableNodeModel variable: switch (variable) { case DataVariableNodeModel data: state = new BaseDataVariableState(parent); break; case PropertyNodeModel property: state = new PropertyState(parent); break; default: return(null); } var baseVariableState = state as BaseVariableState; baseVariableState.ArrayDimensions = variable.ArrayDimensions; baseVariableState.DataType = variable.DataType; baseVariableState.ValueRank = variable.ValueRank ?? ValueRanks.Scalar; baseVariableState.Value = variable.Value?.Value; baseVariableState.AccessLevel = variable.AccessLevel ?? AccessLevels.CurrentRead; baseVariableState.UserAccessLevel = variable.UserAccessLevel ?? AccessLevels.CurrentRead; baseVariableState.IsValueType = variable.IsValueType; baseVariableState.Historizing = variable.Historizing ?? false; baseVariableState.MinimumSamplingInterval = variable.MinimumSamplingInterval ?? 0.0; baseVariableState.ModellingRuleId = variable.ModellingRuleId; baseVariableState.NumericId = variable.NumericId; baseVariableState.ReferenceTypeId = variable.ReferenceTypeId; baseVariableState.StatusCode = variable.StatusCode ?? StatusCodes.Good; baseVariableState.Timestamp = variable.Timestamp ?? DateTime.MinValue; baseVariableState.TypeDefinitionId = variable.TypeDefinitionId; break; case ObjectNodeModel obj: state = new BaseObjectState(parent) { EventNotifier = obj.EventNotifier ?? EventNotifiers.None }; break; case MethodNodeModel method: state = new MethodState(parent) { UserExecutable = method.UserExecutable, Executable = method.Executable, MethodDeclarationId = method.MethodDeclarationId }; break; default: return(null); } break; default: return(null); } state.BrowseName = nodeModel.BrowseName; state.Description = nodeModel.Description; state.DisplayName = nodeModel.DisplayName; state.Handle = nodeModel.Handle; state.NodeId = nodeModel.NodeId; state.SymbolicName = nodeModel.SymbolicName; state.WriteMask = nodeModel.WriteMask ?? AttributeWriteMask.None; state.UserWriteMask = nodeModel.UserWriteMask ?? AttributeWriteMask.None; state.Initialized = true; foreach (var child in nodeModel.GetChildren(context)) { state.AddChild(child.ToNodeState(context, state) as BaseInstanceState); } foreach (var reference in nodeModel.References) { state.AddReference(reference.ReferenceTypeId, reference.IsInverse, reference.TargetId); } return(state); }
/// <summary> /// Imports a node from the set. /// </summary> private NodeState Import(ISystemContext context, UANode node) { NodeState importedNode = null; NodeClass nodeClass = NodeClass.Unspecified; if (node is UAObject) nodeClass = NodeClass.Object; else if (node is UAVariable) nodeClass = NodeClass.Variable; else if (node is UAMethod) nodeClass = NodeClass.Method; else if (node is UAObjectType) nodeClass = NodeClass.ObjectType; else if (node is UAVariableType) nodeClass = NodeClass.VariableType; else if (node is UADataType) nodeClass = NodeClass.DataType; else if (node is UAReferenceType) nodeClass = NodeClass.ReferenceType; else if (node is UAView) nodeClass = NodeClass.View; switch (nodeClass) { case NodeClass.Object: { UAObject o = (UAObject)node; BaseObjectState value = new BaseObjectState(null); value.EventNotifier = o.EventNotifier; importedNode = value; break; } case NodeClass.Variable: { UAVariable o = (UAVariable)node; NodeId typeDefinitionId = null; if (node.References != null) { for (int ii = 0; ii < node.References.Length; ii++) { Opc.Ua.NodeId referenceTypeId = ImportNodeId(node.References[ii].ReferenceType, context.NamespaceUris, true); bool isInverse = !node.References[ii].IsForward; Opc.Ua.ExpandedNodeId targetId = ImportExpandedNodeId(node.References[ii].Value, context.NamespaceUris, context.ServerUris); if (referenceTypeId == ReferenceTypeIds.HasTypeDefinition && !isInverse) { typeDefinitionId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris); break; } } } BaseVariableState value = null; if (typeDefinitionId == Opc.Ua.VariableTypeIds.PropertyType) { value = new PropertyState(null); } else { value = new BaseDataVariableState(null); } value.DataType = ImportNodeId(o.DataType, context.NamespaceUris, true); value.ValueRank = o.ValueRank; value.ArrayDimensions = ImportArrayDimensions(o.ArrayDimensions); value.AccessLevel = o.AccessLevel; value.UserAccessLevel = o.UserAccessLevel; value.MinimumSamplingInterval = o.MinimumSamplingInterval; value.Historizing = o.Historizing; if (o.Value != null) { XmlDecoder decoder = CreateDecoder(context, o.Value); TypeInfo typeInfo = null; value.Value = decoder.ReadVariantContents(out typeInfo); decoder.Close(); } importedNode = value; break; } case NodeClass.Method: { UAMethod o = (UAMethod)node; MethodState value = new MethodState(null); value.Executable = o.Executable; value.UserExecutable = o.UserExecutable; importedNode = value; break; } case NodeClass.View: { UAView o = (UAView)node; ViewState value = new ViewState(); value.ContainsNoLoops = o.ContainsNoLoops; importedNode = value; break; } case NodeClass.ObjectType: { UAObjectType o = (UAObjectType)node; BaseObjectTypeState value = new BaseObjectTypeState(); value.IsAbstract = o.IsAbstract; importedNode = value; break; } case NodeClass.VariableType: { UAVariableType o = (UAVariableType)node; BaseVariableTypeState value = new BaseDataVariableTypeState(); value.IsAbstract = o.IsAbstract; value.DataType = ImportNodeId(o.DataType, context.NamespaceUris, true); value.ValueRank = o.ValueRank; value.ArrayDimensions = ImportArrayDimensions(o.ArrayDimensions); if (o.Value != null) { XmlDecoder decoder = CreateDecoder(context, o.Value); TypeInfo typeInfo = null; value.Value = decoder.ReadVariantContents(out typeInfo); decoder.Close(); } importedNode = value; break; } case NodeClass.DataType: { UADataType o = (UADataType)node; DataTypeState value = new DataTypeState(); value.IsAbstract = o.IsAbstract; value.Definition = Import(o.Definition, context.NamespaceUris); importedNode = value; break; } case NodeClass.ReferenceType: { UAReferenceType o = (UAReferenceType)node; ReferenceTypeState value = new ReferenceTypeState(); value.IsAbstract = o.IsAbstract; value.InverseName = Import(o.InverseName); value.Symmetric = o.Symmetric; importedNode = value; break; } } importedNode.NodeId = ImportNodeId(node.NodeId, context.NamespaceUris, false); importedNode.BrowseName = ImportQualifiedName(node.BrowseName, context.NamespaceUris); importedNode.DisplayName = Import(node.DisplayName); importedNode.Description = Import(node.Description); importedNode.WriteMask = (AttributeWriteMask)node.WriteMask; importedNode.UserWriteMask = (AttributeWriteMask)node.UserWriteMask; if (!String.IsNullOrEmpty(node.SymbolicName)) { importedNode.SymbolicName = node.SymbolicName; } if (node.References != null) { BaseInstanceState instance = importedNode as BaseInstanceState; BaseTypeState type = importedNode as BaseTypeState; for (int ii = 0; ii < node.References.Length; ii++) { Opc.Ua.NodeId referenceTypeId = ImportNodeId(node.References[ii].ReferenceType, context.NamespaceUris, true); bool isInverse = !node.References[ii].IsForward; Opc.Ua.ExpandedNodeId targetId = ImportExpandedNodeId(node.References[ii].Value, context.NamespaceUris, context.ServerUris); if (instance != null) { if (referenceTypeId == ReferenceTypeIds.HasModellingRule && !isInverse) { instance.ModellingRuleId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris); continue; } if (referenceTypeId == ReferenceTypeIds.HasTypeDefinition && !isInverse) { instance.TypeDefinitionId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris); continue; } } if (type != null) { if (referenceTypeId == ReferenceTypeIds.HasSubtype && isInverse) { type.SuperTypeId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris); continue; } } importedNode.AddReference(referenceTypeId, isInverse, targetId); } } return importedNode; }