Example #1
0
        /// <summary>
        /// Updates the aggregate descriptions.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="aggregates">The aggregates.</param>
        private void UpdateAggregateDescriptions(Session session, List<HdaAggregate> aggregates)
        {
            ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); ;

            for (int ii = 0; ii < aggregates.Count; ii++)
            {
                HdaAggregate aggregate = aggregates[ii];

                ReadValueId nodeToRead = new ReadValueId();
                nodeToRead.NodeId = aggregate.RemoteId;
                nodeToRead.AttributeId = Attributes.Description;
                nodesToRead.Add(nodeToRead);
            }

            DataValueCollection values = null;
            DiagnosticInfoCollection diagnosticInfos = null;

            // read values from the UA server.
            ResponseHeader responseHeader = session.Read(
                null,
                0,
                TimestampsToReturn.Neither,
                nodesToRead,
                out values,
                out diagnosticInfos);

            // validate response from the UA server.
            ClientBase.ValidateResponse(values, nodesToRead);
            ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead);

            for (int ii = 0; ii < aggregates.Count; ii++)
            {
                HdaAggregate aggregate = aggregates[ii];

                if (StatusCode.IsBad(values[ii].StatusCode))
                {
                    aggregate.Description = null;
                    continue;
                }

                aggregate.Description = values[ii].WrappedValue.ToString();
            }
        }
Example #2
0
        /// <summary>
        /// Finds the targets for the specified reference.
        /// </summary>
        private static void UpdateInstanceDescriptions(Session session, List<InstanceDeclaration> instances, bool throwOnError)
        {
            try
            {
                ReadValueIdCollection nodesToRead = new ReadValueIdCollection();

                for (int ii = 0; ii < instances.Count; ii++)
                {
                    ReadValueId nodeToRead = new ReadValueId();
                    nodeToRead.NodeId = instances[ii].NodeId;
                    nodeToRead.AttributeId = Attributes.Description;
                    nodesToRead.Add(nodeToRead);

                    nodeToRead = new ReadValueId();
                    nodeToRead.NodeId = instances[ii].NodeId;
                    nodeToRead.AttributeId = Attributes.DataType;
                    nodesToRead.Add(nodeToRead);

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

                // start the browse operation.
                DataValueCollection results = null;
                DiagnosticInfoCollection diagnosticInfos = null;

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

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

                // update the instances.
                for (int ii = 0; ii < nodesToRead.Count; ii += 3)
                {
                    InstanceDeclaration instance = instances[ii / 3];

                    instance.Description = results[ii].GetValue<LocalizedText>(LocalizedText.Null).Text;
                    instance.DataType = results[ii + 1].GetValue<NodeId>(NodeId.Null);
                    instance.ValueRank = results[ii + 2].GetValue<int>(ValueRanks.Any);

                    if (!NodeId.IsNull(instance.DataType))
                    {
                        instance.BuiltInType = DataTypes.GetBuiltInType(instance.DataType, session.TypeTree);
                        instance.DataTypeDisplayText = session.NodeCache.GetDisplayText(instance.DataType);

                        if (instance.ValueRank >= 0)
                        {
                            instance.DataTypeDisplayText += "[]";
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (throwOnError)
                {
                    throw new ServiceResultException(exception, StatusCodes.BadUnexpectedError);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Validates the items by reading the attributes required to add them to the group.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="group">The group.</param>
        /// <param name="requests">The requests.</param>
        /// <param name="items">The items.</param>
        /// <param name="start">The start index.</param>
        /// <param name="count">The number of items to process.</param>
        private void ValidateItems(
            Session session,
            ComDaGroup group,
            ComDaCreateItemRequest[] requests,
            ComDaGroupItem[] items,
            int start,
            int count)
        {
            // build list of the UA attributes that need to be read.
            ReadValueIdCollection attributesToRead = new ReadValueIdCollection();

            for (int ii = start; ii < start + count && ii < requests.Length; ii++)
            {
                // create the group item.
                ComDaCreateItemRequest request = requests[ii];
                ComDaGroupItem item = items[ii] = new ComDaGroupItem(group, request.ItemId);

                item.NodeId = m_mapper.GetRemoteNodeId(request.ItemId);
                item.Active = request.Active;
                item.ClientHandle = request.ClientHandle;
                item.RequestedDataType = request.RequestedDataType;
                item.SamplingRate = -1;
                item.Deadband = -1;

                // add attributes.
                ReadValueId attributeToRead;

                attributeToRead = new ReadValueId();
                attributeToRead.NodeId = item.NodeId;
                attributeToRead.AttributeId = Attributes.NodeClass;
                attributesToRead.Add(attributeToRead);

                attributeToRead = new ReadValueId();
                attributeToRead.NodeId = item.NodeId;
                attributeToRead.AttributeId = Attributes.DataType;
                attributesToRead.Add(attributeToRead);

                attributeToRead = new ReadValueId();
                attributeToRead.NodeId = item.NodeId;
                attributeToRead.AttributeId = Attributes.ValueRank;
                attributesToRead.Add(attributeToRead);

                attributeToRead = new ReadValueId();
                attributeToRead.NodeId = item.NodeId;
                attributeToRead.AttributeId = Attributes.UserAccessLevel;
                attributesToRead.Add(attributeToRead);
            }

            // read attribute values from the server.
            DataValueCollection results = null;
            DiagnosticInfoCollection diagnosticInfos = null;

            try
            {
                session.Read(
                    null,
                    0,
                    TimestampsToReturn.Neither,
                    attributesToRead,
                    out results,
                    out diagnosticInfos);

                ClientBase.ValidateResponse(results, attributesToRead);
                ClientBase.ValidateDiagnosticInfos(diagnosticInfos, attributesToRead);
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error reading attributes for items.");

                // set default values on error.
                for (int ii = start; ii < start + count && ii < requests.Length; ii++)
                {
                    requests[ii].Error = ResultIds.E_INVALIDITEMID;
                }

                return;
            }

            // process results.
            int first = 0;

            for (int ii = start; ii < start + count && ii < requests.Length; ii++, first += 4)
            {
                ComDaGroupItem item = items[ii];

                // verify node class.
                NodeClass nodeClass = (NodeClass)results[first].GetValue<int>((int)NodeClass.Unspecified);

                if (nodeClass != NodeClass.Variable)
                {
                    requests[ii].Error = ResultIds.E_INVALIDITEMID;
                    continue;
                }

                // verify data type.
                NodeId dataTypeId = results[first+1].GetValue<NodeId>(null);

                if (dataTypeId == null)
                {
                    requests[ii].Error = ResultIds.E_INVALIDITEMID;
                    continue;
                }

                // get value rank.
                int valueRank = results[first+2].GetValue<int>(ValueRanks.Scalar);

                // update datatypes.
                BuiltInType builtInType = DataTypes.GetBuiltInType(dataTypeId, session.TypeTree);
                item.RemoteDataType = new TypeInfo(builtInType, valueRank);
                item.CanonicalDataType = (short)ComUtils.GetVarType(item.RemoteDataType);

                // update access rights.
                byte userAccessLevel = results[first+3].GetValue<byte>(0);

                if ((userAccessLevel & AccessLevels.CurrentRead) != 0)
                {
                    item.AccessRights |= OpcRcw.Da.Constants.OPC_READABLE;
                }

                if ((userAccessLevel & AccessLevels.CurrentWrite) != 0)
                {
                    item.AccessRights |= OpcRcw.Da.Constants.OPC_WRITEABLE;
                }
            }
        }
Example #4
0
        /// <summary>
        /// Prompts the user to enter a value to write.
        /// </summary>
        /// <param name="session">The session to use.</param>
        /// <param name="nodeId">The identifier for the node to write to.</param>
        /// <param name="attributeId">The attribute being written.</param>
        /// <returns>True if successful. False if the operation was cancelled.</returns>
        public bool ShowDialog(Session session, NodeId nodeId, uint attributeId)
        {
            m_session = session;
            m_nodeId  = nodeId;
            m_attributeId = attributeId;

            ReadValueId nodeToRead = new ReadValueId();
            nodeToRead.NodeId = nodeId;
            nodeToRead.AttributeId = attributeId;

            ReadValueIdCollection nodesToRead = new ReadValueIdCollection();
            nodesToRead.Add(nodeToRead);
            
            // read current value.
            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);

            m_value = results[0];
            ValueTB.Text = Utils.Format("{0}", m_value.WrappedValue);
            
            // display the dialog.
            if (ShowDialog() != DialogResult.OK)
            {
                return false;
            }

            return true;
        }
        /// <summary>
        /// Gets the item handles.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="itemIds">The item ids.</param>
        /// <param name="clientHandles">The client handles.</param>
        /// <param name="validateOnly">if set to <c>true</c> handles are not created and item ids are only validated.</param>
        /// <returns>The handles containing any error information.</returns>
        public HdaItemHandle[] GetItemHandles(Session session, string[] itemIds, int[] clientHandles, bool validateOnly)
        {
            HdaItemHandle[] handles = new HdaItemHandle[itemIds.Length];
            ReadValueIdCollection nodesToRead = new ReadValueIdCollection();

            for (int ii = 0; ii < itemIds.Length; ii++)
            {
                InternalHandle handle = new InternalHandle();
                handles[ii] = handle;

                if (clientHandles != null)
                {
                    handle.ClientHandle = clientHandles[ii];
                }

                string itemId = itemIds[ii];

                if (String.IsNullOrEmpty(itemId))
                {
                    handle.Error = ResultIds.E_INVALIDITEMID;
                    continue;
                }

                // check if item has already been assigned.
                Item item = null;

                if (!validateOnly)
                {
                    lock (m_lock)
                    {
                        if (m_items.TryGetValue(itemId, out item))
                        {
                            handle.NodeId = item.NodeId;
                            handle.ServerHandle = ++m_lastServerHandle;
                            handle.Item = item;
                            item.Refs++;
                            m_handles[handle.ServerHandle] = handle;
                            Utils.Trace("Created Handle: {0} {1}", handle.ServerHandle, handle.NodeId);
                            continue;
                        }
                    }
                }

                // create a new item.
                handle.Item = item = new Item();
                item.ItemId = itemId;                
                handle.Error = ResultIds.S_OK; // assume valid for no - set to an error when detected.

                handle.NodeId = item.NodeId = m_mapper.GetRemoteNodeId(itemId);

                nodesToRead.Add(Construct(handle, Attributes.UserAccessLevel));
                nodesToRead.Add(Construct(handle, Attributes.DisplayName));
                nodesToRead.Add(Construct(handle, Attributes.Description));
                nodesToRead.Add(Construct(handle, Attributes.DataType));
                nodesToRead.Add(Construct(handle, Attributes.ValueRank));
                nodesToRead.Add(Construct(handle, Attributes.Historizing));
            }

            // check if nothing to do.
            if (nodesToRead.Count == 0)
            {
                return handles;
            }

            DataValueCollection values = null;
            DiagnosticInfoCollection diagnosticInfos = null;

            // read values from the UA server.
            session.Read(
                null,
                0,
                TimestampsToReturn.Neither,
                nodesToRead,
                out values,
                out diagnosticInfos);

            // validate response from the UA server.
            ClientBase.ValidateResponse(values, nodesToRead);
            ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead);

            // assign a local handle to all valid items.
            NodeIdCollection nodesToRegister = new NodeIdCollection();
            List<InternalHandle> items = new List<InternalHandle>();

            for (int ii = 0; ii < nodesToRead.Count; ii++)
            {
                InternalHandle handle = (InternalHandle)nodesToRead[ii].Handle;
                DataValue value = values[ii];
                Item item = handle.Item;
                
                // check status codes.
                if (StatusCode.IsBad(value.StatusCode))
                {
                    // description is an optional attribute.
                    if (nodesToRead[ii].AttributeId != Attributes.Description)
                    {
                        handle.Error = ResultIds.E_UNKNOWNITEMID;
                    }

                    continue;
                }

                // check access level.
                if (nodesToRead[ii].AttributeId == Attributes.UserAccessLevel)
                {
                    byte accessLevel = value.GetValue<byte>(AccessLevels.None);

                    if ((accessLevel & AccessLevels.HistoryRead) == 0)
                    {
                        handle.Error = ResultIds.E_UNKNOWNITEMID;
                        continue;
                    }
                }
                
                // save attribute.
                switch (nodesToRead[ii].AttributeId)
                {
                    case Attributes.DisplayName: { item.DisplayName = value.GetValue<LocalizedText>(null); break; }
                    case Attributes.Description: { item.Description = value.GetValue<LocalizedText>(null); break; }
                    case Attributes.DataType: { item.DataType = value.GetValue<NodeId>(null); break; }
                    case Attributes.ValueRank: { item.ValueRank = value.GetValue<int>(ValueRanks.Scalar); break; }
                    case Attributes.Historizing: { item.Historizing = value.GetValue<bool>(false); break; }
                }

                // should have all item metadata when processing the historizing attribute result.
                if (nodesToRead[ii].AttributeId == Attributes.Historizing)
                {
                    // check for a fatal error with one or more mandatory attributes.
                    if (handle.Error != ResultIds.S_OK)
                    {
                        continue;
                    }

                    BuiltInType builtInType = DataTypes.GetBuiltInType(item.DataType, session.TypeTree);
                    item.RemoteType = new TypeInfo(builtInType, item.ValueRank);

                    if (!validateOnly)
                    {
                        nodesToRegister.Add(item.NodeId);
                        items.Add(handle);

                        lock (m_lock)
                        {
                            m_items[handle.Item.ItemId] = handle.Item;
                            handle.ServerHandle = ++m_lastServerHandle;
                            handle.NodeId = handle.Item.NodeId;
                            handle.Item.Refs++;
                            m_handles[handle.ServerHandle] = handle;
                            Utils.Trace("Created Handle: {0} {1}", handle.ServerHandle, handle.NodeId);
                        }
                    }
                }
            }

            return handles;
        }
        /// <summary>
        /// Reads the current values for the specified attributes.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="itemHandle">The item handle.</param>
        /// <param name="attributeIds">The attribute ids.</param>
        /// <returns></returns>
        public DaValue[] ReadCurrentValues(Session session, HdaItemHandle itemHandle, uint[] attributeIds)
        {
            DaValue[] results = new DaValue[attributeIds.Length];

            // check handle.
            InternalHandle handle = itemHandle as InternalHandle;

            if (handle == null)
            {
                for (int ii = 0; ii < results.Length; ii++)
                {
                    results[ii] = new DaValue();
                    results[ii].Error = ResultIds.E_INVALIDHANDLE;
                }

                return results;
            }

            // look up the supported attributes for an item.
            ReadValueIdCollection supportedAttributes = handle.Item.SupportedAttributes;

            if (supportedAttributes == null)
            {
                handle.Item.SupportedAttributes = supportedAttributes = GetAvailableAttributes(session, handle.NodeId);
            }

            // build list of values to read.
            ReadValueIdCollection valuesToRead = new ReadValueIdCollection();
            List<int> indexes = new List<int>();

            for (int ii = 0; ii < attributeIds.Length; ii++)
            {
                ReadValueId valueToRead = GetReadValueId(supportedAttributes, attributeIds[ii]);

                if (valueToRead == null)
                {
                    results[ii] = new DaValue();
                    results[ii].Error = ResultIds.E_INVALIDATTRID;
                    continue;
                }

                valuesToRead.Add(valueToRead);
                indexes.Add(ii);

                // need to fetch the value rank as well.
                if (attributeIds[ii] == Constants.OPCHDA_DATA_TYPE)
                {
                    valuesToRead.Add(GetReadValueId(supportedAttributes, ComHdaProxy.INTERNAL_ATTRIBUTE_VALUE_RANK));
                    indexes.Add(-1);
                }
            }

            // nothing to do.
            if (valuesToRead.Count == 0)
            {
                return results;
            }

            // read values from the UA server.
            DataValueCollection values = null;
            DiagnosticInfoCollection diagnosticInfos = null;

            session.Read(
                null,
                0,
                TimestampsToReturn.Neither,
                valuesToRead,
                out values,
                out diagnosticInfos);

            // validate response from the UA server.
            ClientBase.ValidateResponse(values, valuesToRead);
            ClientBase.ValidateDiagnosticInfos(diagnosticInfos, valuesToRead);

            // assign a local handle to all valid items.
            for (int ii = 0; ii < valuesToRead.Count; ii++)
            {
                int index = indexes[ii];
                uint attributeId = (uint)valuesToRead[ii].Handle;

                // check for values which are combined with other values to create the value (e.g. ValueRank).
                if (index == -1)
                {
                    continue;
                }

                results[index] = GetAttributeValue(session, attributeId, values, ii);

                // only support current value for now.
                if (results[index].Error == ResultIds.S_OK)
                {
                    results[index].Error = ResultIds.S_CURRENTVALUE;
                }
            }

            return results;
        }
        /// <summary>
        /// Sends the read request to the server.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="nodesToRead">The nodes to read.</param>
        /// <returns></returns>
        private DataValueCollection Read(Session session, ReadValueIdCollection nodesToRead)
        {
            // read attribute values from the server.
            DataValueCollection results = null;
            DiagnosticInfoCollection diagnosticInfos = null;

            try
            {
                session.Read(
                    null,
                    0,
                    TimestampsToReturn.Source,
                    nodesToRead,
                    out results,
                    out diagnosticInfos);

                ClientBase.ValidateResponse(results, nodesToRead);
                ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead);
            }
            catch (Exception e)
            {
                // convert to item level errors.
                ServiceResult error = new ServiceResult(e, StatusCodes.BadUnexpectedError);

                results = new DataValueCollection();

                for (int ii = 0; ii < nodesToRead.Count; ii++)
                {
                    results.Add(new DataValue(error.StatusCode));
                }
            }

            return results;
        }
Example #8
0
        /// <summary>
        /// Returns the expected data type.
        /// </summary>
        private TypeInfo GetExpectedType(Session session, NodeId nodeId)
        {
            // build list of attributes to read.
            ReadValueIdCollection nodesToRead = new ReadValueIdCollection();

            foreach (uint attributeId in new uint[] { Attributes.DataType, Attributes.ValueRank })
            {
                ReadValueId nodeToRead = new ReadValueId();
                nodeToRead.NodeId = nodeId;
                nodeToRead.AttributeId = attributeId;
                nodesToRead.Add(nodeToRead);
            }

            // read the attributes.
            DataValueCollection results = null;
            DiagnosticInfoCollection diagnosticInfos = null;

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

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

            // this call checks for error and checks the data type of the value.
            // if an error or mismatch occurs the default value is returned.
            NodeId dataTypeId = results[0].GetValue<NodeId>(null);
            int valueRank = results[1].GetValue<int>(ValueRanks.Scalar);

            // use the local type cache to look up the base type for the data type.
            BuiltInType builtInType = DataTypes.GetBuiltInType(dataTypeId, session.NodeCache.TypeTree);

            // the type info object is used in cast and compare functions.
            return new TypeInfo(builtInType, valueRank);
        }
Example #9
0
        /// <summary>
        /// Updates the list of references.
        /// </summary>
        private void UpdateArguments(Session session, NodeId nodeId)
        {
            ArgumentsLV.Items.Clear();

            // need to fetch the node ids for the argument properties.
            BrowsePathCollection browsePaths = new BrowsePathCollection();

            foreach (string browseName in new string[] { BrowseNames.InputArguments, BrowseNames.OutputArguments })
            {
                BrowsePath browsePath = new BrowsePath();
                browsePath.StartingNode = nodeId;
                browsePath.Handle = browseName;

                RelativePathElement element = new RelativePathElement();
                element.ReferenceTypeId = ReferenceTypeIds.HasProperty;
                element.IsInverse = false;
                element.IncludeSubtypes = true;
                element.TargetName = browseName;

                browsePath.RelativePath.Elements.Add(element);
                browsePaths.Add(browsePath);
            }

            // translate property names.
            BrowsePathResultCollection results = null;
            DiagnosticInfoCollection diagnosticInfos = null;

            session.TranslateBrowsePathsToNodeIds(
                null,
                browsePaths,
                out results,
                out diagnosticInfos);

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

            // create a list of values to read.
            ReadValueIdCollection valuesToRead = new ReadValueIdCollection();

            for (int ii = 0; ii < results.Count; ii++)
            {
                if (StatusCode.IsBad(results[ii].StatusCode) || results[ii].Targets.Count <= 0)
                {
                    continue;
                }

                ReadValueId valueToRead = new ReadValueId();
                valueToRead.NodeId = (NodeId)results[ii].Targets[0].TargetId;
                valueToRead.AttributeId = Attributes.Value;
                valueToRead.Handle = browsePaths[ii].Handle;
                valuesToRead.Add(valueToRead);
            }

            // read the values.
            if (valuesToRead.Count > 0)
            {
                DataValueCollection values = null;

                session.Read(
                    null,
                    0,
                    TimestampsToReturn.Neither,
                    valuesToRead,
                    out values,
                    out diagnosticInfos);

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

                // update the list control.
                for (int ii = 0; ii < values.Count; ii++)
                {
                    // all structures are wrapped in extension objects.
                    ExtensionObject[] extensions = values[ii].GetValue<ExtensionObject[]>(null);

                    if (extensions != null)
                    {
                        // convert to an argument structure.
                        Argument[] arguments = (Argument[])ExtensionObject.ToArray(extensions, typeof(Argument));
                        UpdateList(session, arguments, (string)valuesToRead[ii].Handle);
                    }
                }
            }

            // auto size the columns.
            for (int ii = 0; ii < ArgumentsLV.Columns.Count; ii++)
            {
                ArgumentsLV.Columns[ii].Width = -2;
            }
        }