Exemplo n.º 1
0
        /// <summary>
        /// Creates a new data type.
        /// </summary>
        private DataTypeState CreateDataType(NodeState parent, IDictionary <NodeId, IList <IReference> > externalReferences, string path, string name)
        {
            DataTypeState type = new DataTypeState();

            type.SymbolicName  = name;
            type.SuperTypeId   = DataTypeIds.Structure;
            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;

            IList <IReference> references = null;

            if (!externalReferences.TryGetValue(DataTypeIds.Structure, out references))
            {
                externalReferences[DataTypeIds.Structure] = references = new List <IReference>();
            }

            references.Add(new NodeStateReference(ReferenceTypeIds.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);
        }
        //// DataTypes
        //

        public DataTypeState CreateAddDataType(string browseDisplayName, NodeId superTypeId, uint preferredNumId = 0)
        {
            var x = new DataTypeState();

            x.BrowseName  = "" + browseDisplayName;
            x.DisplayName = "" + browseDisplayName;
            x.Description = new LocalizedText("en", browseDisplayName);
            x.SuperTypeId = superTypeId;
            x.NodeId      = nodeMgr.NewType(nodeMgr.SystemContext, x, preferredNumId);
            nodeMgr.AddPredefinedNode(nodeMgr.SystemContext, x);
            return(x);
        }
Exemplo n.º 3
0
        /// <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));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Exports a DataTypeDefinition
        /// </summary>
        private Opc.Ua.Export.DataTypeDefinition Export(
            DataTypeState dataType,
            ExtensionObject source,
            NamespaceTable namespaceUris,
            bool outputRedundantNames)
        {
            if (source == null || source.Body == null)
            {
                return(null);
            }

            DataTypeDefinition definition = new DataTypeDefinition();

            if (outputRedundantNames || dataType.BrowseName != null)
            {
                definition.Name = Export(dataType.BrowseName, namespaceUris);
            }

            if (dataType.BrowseName.Name != dataType.SymbolicName)
            {
                definition.SymbolicName = dataType.SymbolicName;
            }

            switch (dataType.DataTypeModifier)
            {
            case DataTypeModifier.Union: { definition.IsUnion = true; break; }

            case DataTypeModifier.OptionSet: { definition.IsOptionSet = true; break; }
            }

            StructureDefinition structureDefinition = source.Body as StructureDefinition;

            if (structureDefinition != null)
            {
                if (structureDefinition.StructureType == StructureType.Union)
                {
                    definition.IsUnion = true;
                }

                if (structureDefinition.Fields != null)
                {
                    List <Opc.Ua.Export.DataTypeField> fields = new List <DataTypeField>();

                    foreach (StructureField field in structureDefinition.Fields)
                    {
                        Opc.Ua.Export.DataTypeField output = new Opc.Ua.Export.DataTypeField();

                        output.Name        = field.Name;
                        output.Description = Export(new Opc.Ua.LocalizedText[] { field.Description });
                        output.IsOptional  = field.IsOptional;

                        if (NodeId.IsNull(field.DataType))
                        {
                            output.DataType = Export(DataTypeIds.BaseDataType, namespaceUris);
                        }
                        else
                        {
                            output.DataType = Export(field.DataType, namespaceUris);
                        }

                        output.ValueRank = field.ValueRank;

                        fields.Add(output);
                    }

                    definition.Field = fields.ToArray();
                }
            }

            EnumDefinition enumDefinition = source.Body as EnumDefinition;

            if (enumDefinition != null)
            {
                if (enumDefinition.Fields != null)
                {
                    List <Opc.Ua.Export.DataTypeField> fields = new List <DataTypeField>();

                    foreach (EnumField field in enumDefinition.Fields)
                    {
                        Opc.Ua.Export.DataTypeField output = new Opc.Ua.Export.DataTypeField();

                        output.Name        = field.Name;
                        output.DisplayName = Export(new Opc.Ua.LocalizedText[] { field.Name });
                        output.Description = Export(new Opc.Ua.LocalizedText[] { field.Description });
                        output.ValueRank   = ValueRanks.Scalar;
                        output.Value       = (int)field.Value;

                        fields.Add(output);
                    }

                    definition.Field = fields.ToArray();
                }
            }

            return(definition);
        }
Exemplo n.º 5
0
        /// <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);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds a node to the set.
        /// </summary>
        public void Export(ISystemContext context, NodeState node, bool outputRedundantNames = true)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (Opc.Ua.NodeId.IsNull(node.NodeId))
            {
                throw new ArgumentException("A non-null NodeId must be specified.");
            }

            UANode exportedNode = null;

            switch (node.NodeClass)
            {
            case NodeClass.Object:
            {
                BaseObjectState o     = (BaseObjectState)node;
                UAObject        value = new UAObject();
                value.EventNotifier = o.EventNotifier;

                if (o.Parent != null)
                {
                    value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
                }

                exportedNode = value;
                break;
            }

            case NodeClass.Variable:
            {
                BaseVariableState o     = (BaseVariableState)node;
                UAVariable        value = new UAVariable();
                value.DataType                = ExportAlias(o.DataType, context.NamespaceUris);
                value.ValueRank               = o.ValueRank;
                value.ArrayDimensions         = Export(o.ArrayDimensions);
                value.AccessLevel             = o.AccessLevelEx;
                value.MinimumSamplingInterval = o.MinimumSamplingInterval;
                value.Historizing             = o.Historizing;

                if (o.Parent != null)
                {
                    value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
                }

                if (o.Value != null)
                {
                    XmlEncoder encoder = CreateEncoder(context);

                    Variant variant = new Variant(o.Value);
                    encoder.WriteVariantContents(variant.Value, variant.TypeInfo);

                    XmlDocument document = new XmlDocument();
                    document.InnerXml = encoder.Close();
                    value.Value       = document.DocumentElement;
                }

                exportedNode = value;
                break;
            }

            case NodeClass.Method:
            {
                MethodState o     = (MethodState)node;
                UAMethod    value = new UAMethod();
                value.Executable = o.Executable;

                if (o.TypeDefinitionId != null && !o.TypeDefinitionId.IsNullNodeId && o.TypeDefinitionId != o.NodeId)
                {
                    value.MethodDeclarationId = Export(o.TypeDefinitionId, context.NamespaceUris);
                }

                if (o.Parent != null)
                {
                    value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
                }

                exportedNode = value;
                break;
            }

            case NodeClass.View:
            {
                ViewState o     = (ViewState)node;
                UAView    value = new UAView();
                value.ContainsNoLoops = o.ContainsNoLoops;
                exportedNode          = value;
                break;
            }

            case NodeClass.ObjectType:
            {
                BaseObjectTypeState o     = (BaseObjectTypeState)node;
                UAObjectType        value = new UAObjectType();
                value.IsAbstract = o.IsAbstract;
                exportedNode     = value;
                break;
            }

            case NodeClass.VariableType:
            {
                BaseVariableTypeState o     = (BaseVariableTypeState)node;
                UAVariableType        value = new UAVariableType();
                value.IsAbstract      = o.IsAbstract;
                value.DataType        = ExportAlias(o.DataType, context.NamespaceUris);
                value.ValueRank       = o.ValueRank;
                value.ArrayDimensions = Export(o.ArrayDimensions);

                if (o.Value != null)
                {
                    XmlEncoder encoder = CreateEncoder(context);

                    Variant variant = new Variant(o.Value);
                    encoder.WriteVariantContents(variant.Value, variant.TypeInfo);

                    XmlDocument document = new XmlDocument();
                    document.InnerXml = encoder.Close();
                    value.Value       = document.DocumentElement;
                }

                exportedNode = value;
                break;
            }

            case NodeClass.DataType:
            {
                DataTypeState o     = (DataTypeState)node;
                UADataType    value = new UADataType();
                value.IsAbstract = o.IsAbstract;
                value.Definition = Export(o, o.DataTypeDefinition, context.NamespaceUris, outputRedundantNames);
                value.Purpose    = o.Purpose;
                exportedNode     = value;
                break;
            }

            case NodeClass.ReferenceType:
            {
                ReferenceTypeState o     = (ReferenceTypeState)node;
                UAReferenceType    value = new UAReferenceType();
                value.IsAbstract = o.IsAbstract;

                if (!Opc.Ua.LocalizedText.IsNullOrEmpty(o.InverseName))
                {
                    value.InverseName = Export(new Opc.Ua.LocalizedText[] { o.InverseName });
                }

                value.Symmetric = o.Symmetric;
                exportedNode    = value;
                break;
            }
            }

            exportedNode.NodeId     = Export(node.NodeId, context.NamespaceUris);
            exportedNode.BrowseName = Export(node.BrowseName, context.NamespaceUris);

            if (outputRedundantNames || node.DisplayName.Text != node.BrowseName.Name)
            {
                exportedNode.DisplayName = Export(new Opc.Ua.LocalizedText[] { node.DisplayName });
            }
            else
            {
                exportedNode.DisplayName = null;
            }

            if (node.Description != null && !String.IsNullOrEmpty(node.Description.Text))
            {
                exportedNode.Description = Export(new Opc.Ua.LocalizedText[] { node.Description });
            }
            else
            {
                exportedNode.Description = new LocalizedText[0];
            }

            exportedNode.Category      = (node.Categories != null && node.Categories.Count > 0) ? new List <string>(node.Categories).ToArray() : null;
            exportedNode.ReleaseStatus = node.ReleaseStatus;
            exportedNode.WriteMask     = (uint)node.WriteMask;
            exportedNode.UserWriteMask = (uint)node.UserWriteMask;
            exportedNode.Extensions    = node.Extensions;

            if (!String.IsNullOrEmpty(node.SymbolicName) && node.SymbolicName != node.BrowseName.Name)
            {
                exportedNode.SymbolicName = node.SymbolicName;
            }

            // export references.
            INodeBrowser     browser            = node.CreateBrowser(context, null, null, true, BrowseDirection.Both, null, null, true);
            List <Reference> exportedReferences = new List <Reference>();
            IReference       reference          = browser.Next();

            while (reference != null)
            {
                if (node.NodeClass == NodeClass.Method)
                {
                    if (!reference.IsInverse && reference.ReferenceTypeId == ReferenceTypeIds.HasTypeDefinition)
                    {
                        reference = browser.Next();
                        continue;
                    }
                }

                Reference exportedReference = new Reference();

                exportedReference.ReferenceType = ExportAlias(reference.ReferenceTypeId, context.NamespaceUris);
                exportedReference.IsForward     = !reference.IsInverse;
                exportedReference.Value         = Export(reference.TargetId, context.NamespaceUris, context.ServerUris);
                exportedReferences.Add(exportedReference);

                reference = browser.Next();
            }

            exportedNode.References = exportedReferences.ToArray();

            // add node to list.
            UANode[] nodes = null;

            int count = 1;

            if (this.Items == null)
            {
                nodes = new UANode[count];
            }
            else
            {
                count += this.Items.Length;
                nodes  = new UANode[count];
                Array.Copy(this.Items, nodes, this.Items.Length);
            }

            nodes[count - 1] = exportedNode;

            this.Items = nodes;

            // recusively process children.
            List <BaseInstanceState> children = new List <BaseInstanceState>();

            node.GetChildren(context, children);

            for (int ii = 0; ii < children.Count; ii++)
            {
                Export(context, children[ii], outputRedundantNames);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Exports a DataTypeDefinition
        /// </summary>
        private Opc.Ua.Export.DataTypeDefinition Export(
            DataTypeState dataType,
            ExtensionObject source,
            NamespaceTable namespaceUris,
            bool outputRedundantNames)
        {
            if (source == null || source.Body == null)
            {
                return(null);
            }

            DataTypeDefinition definition = new DataTypeDefinition();

            if (outputRedundantNames || dataType.BrowseName != null)
            {
                definition.Name = Export(dataType.BrowseName, namespaceUris);
            }

            if (dataType.BrowseName.Name != dataType.SymbolicName)
            {
                definition.SymbolicName = dataType.SymbolicName;
            }

            StructureDefinition sd = source.Body as StructureDefinition;

            if (sd != null)
            {
                if (sd.StructureType == StructureType.Union || sd.StructureType == (StructureType)4) // StructureType.UnionWithSubtypedValues)
                {
                    definition.IsUnion = true;
                }

                if (sd.Fields != null)
                {
                    List <Opc.Ua.Export.DataTypeField> fields = new List <DataTypeField>();

                    for (int ii = sd.FirstExplicitFieldIndex; ii < sd.Fields.Count; ii++)
                    {
                        StructureField field = sd.Fields[ii];

                        Opc.Ua.Export.DataTypeField output = new Opc.Ua.Export.DataTypeField();

                        output.Name        = field.Name;
                        output.Description = Export(new Opc.Ua.LocalizedText[] { field.Description });

                        if (sd.StructureType == StructureType.StructureWithOptionalFields)
                        {
                            output.IsOptional    = field.IsOptional;
                            output.AllowSubTypes = false;
                        }
                        else if (sd.StructureType == (StructureType)3 || // StructureType.StructureWithSubtypedValues ||
                                 sd.StructureType == (StructureType)4)   // StructureType.UnionWithSubtypedValues)
                        {
                            output.IsOptional    = false;
                            output.AllowSubTypes = field.IsOptional;
                        }
                        else
                        {
                            output.IsOptional    = false;
                            output.AllowSubTypes = false;
                        }

                        if (NodeId.IsNull(field.DataType))
                        {
                            output.DataType = Export(DataTypeIds.BaseDataType, namespaceUris);
                        }
                        else
                        {
                            output.DataType = Export(field.DataType, namespaceUris);
                        }

                        output.ValueRank = field.ValueRank;

                        fields.Add(output);
                    }

                    definition.Field = fields.ToArray();
                }
            }

            EnumDefinition ed = source.Body as EnumDefinition;

            if (ed != null)
            {
                definition.IsOptionSet = ed.IsOptionSet;

                if (ed.Fields != null)
                {
                    List <Opc.Ua.Export.DataTypeField> fields = new List <DataTypeField>();

                    foreach (EnumField field in ed.Fields)
                    {
                        Opc.Ua.Export.DataTypeField output = new Opc.Ua.Export.DataTypeField();

                        output.Name = field.Name;

                        if (field.DisplayName != null && output.Name != field.DisplayName.Text)
                        {
                            output.DisplayName = Export(new Opc.Ua.LocalizedText[] { field.DisplayName });
                        }
                        else
                        {
                            output.DisplayName = Array.Empty <LocalizedText>();
                        }

                        output.Description = Export(new Opc.Ua.LocalizedText[] { field.Description });
                        output.ValueRank   = ValueRanks.Scalar;
                        output.Value       = (int)field.Value;

                        fields.Add(output);
                    }

                    definition.Field = fields.ToArray();
                }
            }

            return(definition);
        }
Exemplo n.º 8
0
        /// <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);
        }
Exemplo n.º 9
0
        /// <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;
        }
Exemplo n.º 10
0
        public virtual NodeState BindNodeStates(IDictionary <NodeId, IList <IReference> > externalReferences, NodeState nodeState, ref NodeStateCollection nodeStateCollectionToBind)
        {
            switch (nodeState.NodeClass)
            {
            case NodeClass.Object:
                if (!(nodeState is BaseObjectState baseObjectState))
                {
                    return(nodeState);
                }
                _previousBaseNode = baseObjectState;
                //Bind previous method now since it will be cleared
                if (_previousMethod != null)
                {
                    int index = nodeStateCollectionToBind.FindIndex(x => x.NodeId == _previousMethod.NodeId);
                    if (index == -1)
                    {
                        nodeStateCollectionToBind.Add(_previousMethod);
                    }
                    else
                    {
                        nodeStateCollectionToBind[index] = _previousMethod;
                    }
                }
                // ensure the process object can be found via the server object.
                if (!externalReferences.TryGetValue(ObjectIds.ObjectsFolder, out IList <IReference> references))
                {
                    externalReferences[ObjectIds.ObjectsFolder] = references = new List <IReference>();
                }
                references.Add(new NodeStateReference(ReferenceTypeIds.Organizes, false, _previousBaseNode.NodeId));
                _previousMethod = null;
                break;

            case NodeClass.DataType:
                if (!(nodeState is DataTypeState dataTypeState))
                {
                    return(nodeState);
                }
                BindNodeStateActions(dataTypeState);
                _previousDataType = dataTypeState;
                int index2 = nodeStateCollectionToBind.FindIndex(x => x.NodeId == _previousDataType.NodeId);
                if (index2 == -1)
                {
                    nodeStateCollectionToBind.Add(_previousDataType);
                }
                else
                {
                    nodeStateCollectionToBind[index2] = _previousDataType;
                }
                break;

            case NodeClass.Method:
                if (!(nodeState is MethodState methodState))
                {
                    return(nodeState);
                }
                BindNodeStateActions(methodState);
                _previousBaseNode?.AddChild(methodState);
                if (_previousMethod != null)
                {
                    int index = nodeStateCollectionToBind.FindIndex(x => x.NodeId == _previousMethod.NodeId);
                    if (index == -1)
                    {
                        nodeStateCollectionToBind.Add(_previousMethod);
                    }
                    else
                    {
                        nodeStateCollectionToBind[index] = _previousMethod;
                    }
                }
                _previousMethod = methodState;
                return(methodState);

            case NodeClass.Variable:
                if (_previousMethod != null)
                {
                    if (!(nodeState is PropertyState propertyState))
                    {
                        return(nodeState);
                    }
                    BindNodeStateActions(propertyState);
                    if (propertyState.DisplayName == BrowseNames.InputArguments)
                    {
                        _previousMethod.InputArguments = new PropertyState <Argument[]>(_previousMethod)
                        {
                            NodeId                = propertyState.NodeId,
                            BrowseName            = propertyState.BrowseName,
                            DisplayName           = propertyState.DisplayName,
                            TypeDefinitionId      = propertyState.TypeDefinitionId,
                            ReferenceTypeId       = propertyState.ReferenceTypeId,
                            DataType              = propertyState.DataType,
                            ValueRank             = propertyState.ValueRank,
                            Value                 = ExtensionObject.ToArray(propertyState.Value, typeof(Argument)) as Argument[],
                            OnReadUserAccessLevel = OnReadUserAccessLevel,
                            OnSimpleWriteValue    = OnWriteValue
                        };
                    }
                    else if (propertyState.DisplayName == BrowseNames.OutputArguments)
                    {
                        _previousMethod.OutputArguments = new PropertyState <Argument[]>(_previousMethod)
                        {
                            NodeId                = propertyState.NodeId,
                            BrowseName            = propertyState.BrowseName,
                            DisplayName           = propertyState.DisplayName,
                            TypeDefinitionId      = propertyState.TypeDefinitionId,
                            ReferenceTypeId       = propertyState.ReferenceTypeId,
                            DataType              = propertyState.DataType,
                            ValueRank             = propertyState.ValueRank,
                            Value                 = ExtensionObject.ToArray(propertyState.Value, typeof(Argument)) as Argument[],
                            OnReadUserAccessLevel = OnReadUserAccessLevel,
                            OnSimpleWriteValue    = OnWriteValue
                        };
                    }
                }
                break;

            default:
                if (_previousBaseNode != null)
                {
                    int index = nodeStateCollectionToBind.FindIndex(x => x.NodeId == _previousBaseNode.NodeId);
                    if (index == -1)
                    {
                        nodeStateCollectionToBind.Add(_previousBaseNode);
                    }
                    else
                    {
                        nodeStateCollectionToBind[index] = _previousBaseNode;
                    }
                }
                if (_previousMethod != null)
                {
                    int index = nodeStateCollectionToBind.FindIndex(x => x.NodeId == _previousMethod.NodeId);
                    if (index == -1)
                    {
                        nodeStateCollectionToBind.Add(_previousMethod);
                    }
                    else
                    {
                        nodeStateCollectionToBind[index] = _previousMethod;
                    }
                }
                _previousBaseNode = null;
                _previousMethod   = null;
                break;
            }
            return(nodeState);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Exports a DataTypeDefinition
        /// </summary>
        private Opc.Ua.Export.DataTypeDefinition Export(
            DataTypeState dataType,
            Opc.Ua.DataTypeDefinition2 source,
            NamespaceTable namespaceUris,
            bool outputRedundantNames)
        {
            if (source == null)
            {
                return(null);
            }

            DataTypeDefinition definition = new DataTypeDefinition();

            if (outputRedundantNames || dataType.BrowseName != source.Name)
            {
                definition.Name = Export(source.Name, namespaceUris);
            }

            if (source.Name.Name != source.SymbolicName)
            {
                definition.SymbolicName = source.SymbolicName;
            }

            switch (source.DataTypeModifier)
            {
            case DataTypeModifier.Union: { definition.IsUnion = true; break; }

            case DataTypeModifier.OptionSet: { definition.IsOptionSet = true; break; }
            }

            if (source.Fields != null)
            {
                List <Opc.Ua.Export.DataTypeField> fields = new List <DataTypeField>();

                foreach (DataTypeDefinitionField field in source.Fields)
                {
                    Opc.Ua.Export.DataTypeField output = new Opc.Ua.Export.DataTypeField();

                    output.Name         = field.Name;
                    output.SymbolicName = field.SymbolicName;
                    output.Description  = Export(new Opc.Ua.LocalizedText[] { field.Description });

                    if (NodeId.IsNull(field.DataType))
                    {
                        output.DataType = Export(DataTypeIds.BaseDataType, namespaceUris);
                    }
                    else
                    {
                        output.DataType = Export(field.DataType, namespaceUris);
                    }

                    output.ValueRank = field.ValueRank;
                    output.Value     = field.Value;

                    fields.Add(output);
                }

                definition.Field = fields.ToArray();
            }

            return(definition);
        }