예제 #1
0
        /// <summary>
        /// Sets the value shown in the control.
        /// </summary>
        private void SetValue(Variant value)
        {
            ValueTB.ForeColor = Color.Empty;
            ValueTB.Font      = new Font(ValueTB.Font, FontStyle.Regular);

            m_textChanged = false;

            // check for null.
            if (Variant.Null == value)
            {
                ValueTB.Text = String.Empty;
                m_value      = Variant.Null;
                return;
            }

            // get the source type.
            TypeInfo sourceType = value.TypeInfo;

            if (sourceType == null)
            {
                sourceType = TypeInfo.Construct(value.Value);
            }

            m_value = new Variant(value.Value, sourceType);

            // display value as text.
            StringBuilder buffer = new StringBuilder();
            XmlWriter     writer = XmlWriter.Create(buffer, new XmlWriterSettings()
            {
                Indent = true, OmitXmlDeclaration = true
            });
            XmlEncoder encoder = new XmlEncoder(new XmlQualifiedName("Value", Namespaces.OpcUaXsd), writer, m_session.MessageContext);

            encoder.WriteVariantContents(m_value.Value, m_value.TypeInfo);
            writer.Close();

            ValueTB.Text = buffer.ToString();

            // extract the encoding id from the value.
            ExpandedNodeId          encodingId = null;
            ExtensionObjectEncoding encoding   = ExtensionObjectEncoding.None;

            if (sourceType.BuiltInType == BuiltInType.ExtensionObject)
            {
                ExtensionObject extension = null;

                if (sourceType.ValueRank == ValueRanks.Scalar)
                {
                    extension = (ExtensionObject)m_value.Value;
                }
                else
                {
                    // only use the first item in the list for arrays.
                    ExtensionObject[] list = (ExtensionObject[])m_value.Value;

                    if (list.Length > 0)
                    {
                        extension = list[0];
                    }
                }

                encodingId = extension.TypeId;
                encoding   = extension.Encoding;
            }

            if (encodingId == null)
            {
                StatusCTRL.Visible = false;
                return;
            }

            // check if the encoding is known.
            IObject encodingNode = m_session.NodeCache.Find(encodingId) as IObject;

            if (encodingNode == null)
            {
                StatusCTRL.Visible = false;
                return;
            }

            // update the encoding shown.
            if (encoding == ExtensionObjectEncoding.EncodeableObject)
            {
                EncodingCB.Text = "(Converted to XML by Client)";
            }
            else
            {
                EncodingCB.Text = m_session.NodeCache.GetDisplayText(encodingNode);
            }

            m_encodingName = encodingNode.BrowseName;

            // find the data type for the encoding.
            IDataType dataTypeNode = null;

            foreach (INode node in m_session.NodeCache.Find(encodingNode.NodeId, Opc.Ua.ReferenceTypeIds.HasEncoding, true, false))
            {
                dataTypeNode = node as IDataType;

                if (dataTypeNode != null)
                {
                    break;
                }
            }

            if (dataTypeNode == null)
            {
                StatusCTRL.Visible = false;
                return;
            }

            // update data type display.
            DataTypeTB.Text = m_session.NodeCache.GetDisplayText(dataTypeNode);
            DataTypeTB.Tag  = dataTypeNode;

            // update encoding drop down.
            EncodingCB.DropDownItems.Clear();

            foreach (INode node in m_session.NodeCache.Find(dataTypeNode.NodeId, Opc.Ua.ReferenceTypeIds.HasEncoding, false, false))
            {
                IObject encodingNode2 = node as IObject;

                if (encodingNode2 != null)
                {
                    ToolStripMenuItem item = new ToolStripMenuItem(m_session.NodeCache.GetDisplayText(encodingNode2));
                    item.Tag    = encodingNode2;
                    item.Click += new EventHandler(EncodingCB_Item_Click);
                    EncodingCB.DropDownItems.Add(item);
                }
            }

            StatusCTRL.Visible = true;
        }
예제 #2
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);
            }
        }