/// <summary>
        /// 读取一个节点的所有属性
        /// </summary>
        /// <param name="tag">节点信息</param>
        /// <returns>节点的特性值</returns>
        public OpcNodeAttribute[] ReadNoteAttributes(string tag)
        {
            NodeId sourceId = new NodeId(tag);
            ReadValueIdCollection nodesToRead = new ReadValueIdCollection();

            // attempt to read all possible attributes.
            // 尝试着去读取所有可能的特性
            for (uint ii = Attributes.NodeClass; ii <= Attributes.UserExecutable; ii++)
            {
                ReadValueId nodeToRead = new ReadValueId();
                nodeToRead.NodeId      = sourceId;
                nodeToRead.AttributeId = ii;
                nodesToRead.Add(nodeToRead);
            }

            int startOfProperties = nodesToRead.Count;

            // find all of the pror of the node.
            BrowseDescription nodeToBrowse1 = new BrowseDescription();

            nodeToBrowse1.NodeId          = sourceId;
            nodeToBrowse1.BrowseDirection = BrowseDirection.Forward;
            nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.HasProperty;
            nodeToBrowse1.IncludeSubtypes = true;
            nodeToBrowse1.NodeClassMask   = 0;
            nodeToBrowse1.ResultMask      = (uint)BrowseResultMask.All;

            BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection();

            nodesToBrowse.Add(nodeToBrowse1);

            // fetch property references from the server.
            ReferenceDescriptionCollection references = FormUtils.Browse(m_session, nodesToBrowse, false);

            if (references == null)
            {
                return(new OpcNodeAttribute[0]);
            }

            for (int ii = 0; ii < references.Count; ii++)
            {
                // ignore external references.
                if (references[ii].NodeId.IsAbsolute)
                {
                    continue;
                }

                ReadValueId nodeToRead = new ReadValueId();
                nodeToRead.NodeId      = (NodeId)references[ii].NodeId;
                nodeToRead.AttributeId = Attributes.Value;
                nodesToRead.Add(nodeToRead);
            }

            // read all values.
            DataValueCollection      results         = null;
            DiagnosticInfoCollection diagnosticInfos = null;

            m_session.Read(
                null,
                0,
                TimestampsToReturn.Neither,
                nodesToRead,
                out results,
                out diagnosticInfos);

            ClientBase.ValidateResponse(results, nodesToRead);
            ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead);

            // process results.


            List <OpcNodeAttribute> nodeAttribute = new List <OpcNodeAttribute>();

            for (int ii = 0; ii < results.Count; ii++)
            {
                OpcNodeAttribute item = new OpcNodeAttribute();

                // process attribute value.
                if (ii < startOfProperties)
                {
                    // ignore attributes which are invalid for the node.
                    if (results[ii].StatusCode == StatusCodes.BadAttributeIdInvalid)
                    {
                        continue;
                    }

                    // get the name of the attribute.
                    item.Name = Attributes.GetBrowseName(nodesToRead[ii].AttributeId);

                    // display any unexpected error.
                    if (StatusCode.IsBad(results[ii].StatusCode))
                    {
                        item.Type  = Utils.Format("{0}", Attributes.GetDataTypeId(nodesToRead[ii].AttributeId));
                        item.Value = Utils.Format("{0}", results[ii].StatusCode);
                    }

                    // display the value.
                    else
                    {
                        TypeInfo typeInfo = TypeInfo.Construct(results[ii].Value);

                        item.Type = typeInfo.BuiltInType.ToString();

                        if (typeInfo.ValueRank >= ValueRanks.OneOrMoreDimensions)
                        {
                            item.Type += "[]";
                        }

                        item.Value = results[ii].Value;//Utils.Format("{0}", results[ii].Value);
                    }
                }

                // process property value.
                else
                {
                    // ignore properties which are invalid for the node.
                    if (results[ii].StatusCode == StatusCodes.BadNodeIdUnknown)
                    {
                        continue;
                    }

                    // get the name of the property.
                    item.Name = Utils.Format("{0}", references[ii - startOfProperties]);

                    // display any unexpected error.
                    if (StatusCode.IsBad(results[ii].StatusCode))
                    {
                        item.Type  = String.Empty;
                        item.Value = Utils.Format("{0}", results[ii].StatusCode);
                    }

                    // display the value.
                    else
                    {
                        TypeInfo typeInfo = TypeInfo.Construct(results[ii].Value);

                        item.Type = typeInfo.BuiltInType.ToString();

                        if (typeInfo.ValueRank >= ValueRanks.OneOrMoreDimensions)
                        {
                            item.Type += "[]";
                        }

                        item.Value = results[ii].Value; //Utils.Format("{0}", results[ii].Value);
                    }
                }

                nodeAttribute.Add(item);
            }

            return(nodeAttribute.ToArray());
        }
Exemple #2
0
        /// <summary>
        /// 根据ID号查询节点属性
        /// 0:NodeClass  1:Value  2:AccessLevel  3:DisplayName  4:Description
        /// </summary>
        /// <param name="nodeIds"></param>
        /// <returns></returns>
        private OpcNodeAttribute ReadOneNodeFiveAttribute(NodeId nodeId)
        {
            ReadValueIdCollection nodesToRead = new ReadValueIdCollection();

            NodeId sourceId = nodeId;

            nodesToRead.Add(new ReadValueId()
            {
                NodeId      = sourceId,
                AttributeId = Attributes.NodeClass
            });
            nodesToRead.Add(new ReadValueId()
            {
                NodeId      = sourceId,
                AttributeId = Attributes.Value
            });
            nodesToRead.Add(new ReadValueId()
            {
                NodeId      = sourceId,
                AttributeId = Attributes.AccessLevel
            });
            nodesToRead.Add(new ReadValueId()
            {
                NodeId      = sourceId,
                AttributeId = Attributes.DisplayName
            });
            nodesToRead.Add(new ReadValueId()
            {
                NodeId      = sourceId,
                AttributeId = Attributes.Description
            });
            // read all values.
            base.Session.Read(
                null,
                0,
                TimestampsToReturn.Neither,
                nodesToRead,
                out DataValueCollection results,
                out DiagnosticInfoCollection diagnosticInfos);
            try
            {
                ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead);
                DataValue[]      dataValues = results.ToArray();
                OpcNodeAttribute attribute  = new OpcNodeAttribute();
                NodeClass?       nodeclass  = dataValues[0].WrappedValue.Value == null ? new NodeClass() : ((NodeClass)dataValues[0].WrappedValue.Value);

                ClientBase.ValidateResponse(results, nodesToRead);

                var    description = "";
                var    name        = dataValues[3].WrappedValue.Value.ToString();
                var    accessLevel = GetDiscriptionFromAccessLevel(dataValues[2]);
                object type;
                object value;
                if (nodeclass == NodeClass.Object)
                {
                    value = "";
                    type  = nodeclass.ToString();
                }
                else if (nodeclass == NodeClass.Method)
                {
                    value = "";
                    type  = nodeclass.ToString();
                }
                else if (nodeclass == NodeClass.Variable)
                {
                    DataValue dataValue = dataValues[1];

                    if (dataValue.WrappedValue.TypeInfo != null)
                    {
                        type = dataValue.WrappedValue.TypeInfo.BuiltInType;
                        // type = dataValue.Value.GetType().ToString();
                        if (dataValue.WrappedValue.TypeInfo.ValueRank == ValueRanks.Scalar)
                        {
                            value = dataValue.WrappedValue.Value;
                        }
                        else if (dataValue.WrappedValue.TypeInfo.ValueRank == ValueRanks.OneDimension)
                        {
                            value = dataValue.Value.GetType().ToString();
                        }
                        else if (dataValue.WrappedValue.TypeInfo.ValueRank == ValueRanks.TwoDimensions)
                        {
                            value = dataValue.Value.GetType().ToString();
                        }
                        else
                        {
                            value = dataValue.Value.GetType().ToString();
                        }
                    }
                    else
                    {
                        value = dataValue.Value;
                        type  = "null";
                    }
                }
                else
                {
                    value = "";
                    type  = nodeclass.ToString();
                }


                if (dataValues[4].WrappedValue.Value != null)
                {
                    description = dataValues[4].WrappedValue.Value.ToString();
                }
                return(new OpcNodeAttribute {
                    Name = name, Value = value, Type = type, Description = description, NodeClass = nodeclass.ToString(), AccessLevel = accessLevel
                });
            }
            catch (Exception e)
            {
                return(null);
            }
        }