Beispiel #1
0
 /// <summary>
 /// Gets the latest value in the cache.
 /// </summary>
 /// <param name="value">The value.</param>
 public void GetLatest(DaValue value)
 {
     value.Value     = this.Value;
     value.Quality   = this.Quality;
     value.Timestamp = this.Timestamp;
     value.Error     = this.Error;
 }
        /// <summary>
        /// Gets the local data value.
        /// </summary>
        /// <param name="remoteValue">The remote value.</param>
        /// <returns>The local data value.</returns>
        public DaValue GetLocalDataValue(DataValue remoteValue)
        {
            DaValue localValue = new DaValue();

            localValue.Error = ComDaProxy.MapReadStatusToErrorCode(remoteValue.StatusCode);

            if (localValue.Error >= 0)
            {
                localValue.HdaQuality = ComUtils.GetHdaQualityCode(remoteValue.StatusCode);
                localValue.Timestamp  = remoteValue.SourceTimestamp;
                localValue.Error      = ResultIds.S_OK;

                if (localValue.Timestamp == DateTime.MinValue)
                {
                    localValue.Timestamp = remoteValue.ServerTimestamp;
                }

                try
                {
                    localValue.Value = GetLocalValue(remoteValue.WrappedValue);
                }
                catch
                {
                    localValue.Error = ResultIds.E_BADTYPE;
                }
            }

            return(localValue);
        }
Beispiel #3
0
        /// <summary>
        /// Reads the attribute values from the server.
        /// </summary>
        /// <param name="valuesToRead">The values to read.</param>
        /// <returns>
        /// The values read.
        /// </returns>
        public DaValue[] Read(ReadValueIdCollection valuesToRead)
        {
            TraceState("Read", valuesToRead.Count);

            // check for valid session.
            Session session = m_session;

            if (session == null)
            {
                throw ComUtils.CreateComException(ResultIds.E_FAIL);
            }

            int masterError = ResultIds.E_FAIL;

            DaValue[] results = new DaValue[valuesToRead.Count];

            if (session != null)
            {
                try
                {
                    // read the values.
                    DataValueCollection      values          = null;
                    DiagnosticInfoCollection diagnosticInfos = null;

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

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

                    // convert the response.
                    for (int ii = 0; ii < values.Count; ii++)
                    {
                        results[ii] = m_mapper.GetLocalDataValue(values[ii]);
                    }

                    // return the results.
                    return(results);
                }
                catch (Exception e)
                {
                    masterError = ComUtils.GetErrorCode(e, ResultIds.E_FAIL);
                }
            }

            // report any unexpected errors.
            for (int ii = 0; ii < results.Length; ii++)
            {
                DaValue result = results[ii] = new DaValue();
                result.Error = masterError;
            }

            return(results);
        }
Beispiel #4
0
        /// <summary>
        /// Converts a UA value to an HDA attribute value.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="attributeId">The attribute id.</param>
        /// <param name="values">The values.</param>
        /// <param name="index">The index.</param>
        /// <returns></returns>
        private DaValue GetAttributeValue(Session session, uint attributeId, DataValueCollection values, int index)
        {
            switch (attributeId)
            {
            case Constants.OPCHDA_DATA_TYPE:
            {
                DaValue   result = new DaValue();
                DataValue value  = values[index];

                // check for valid node.
                if (StatusCode.IsBad(value.StatusCode))
                {
                    result.Error = ResultIds.E_UNKNOWNITEMID;
                    return(result);
                }

                // covert to var type.
                NodeId dataTypeId = value.GetValue <NodeId>(DataTypeIds.BaseDataType);
                int    valueRank  = values[index + 1].GetValue <int>(ValueRanks.Scalar);

                BuiltInType builtInType = DataTypes.GetBuiltInType(dataTypeId, session.TypeTree);
                TypeInfo    typeInfo    = new TypeInfo(builtInType, valueRank);
                short       varType     = (short)ComUtils.GetVarType(typeInfo);

                result.Value     = varType;
                result.Quality   = ComUtils.GetQualityCode(value.StatusCode);
                result.Timestamp = value.ServerTimestamp;
                result.Error     = ResultIds.S_OK;

                return(result);
            }

            case Constants.OPCHDA_DESCRIPTION:
            {
                DataValue value = values[index];

                if (value.StatusCode == StatusCodes.BadAttributeIdInvalid)
                {
                    DaValue result = new DaValue();
                    result.Error = ResultIds.E_INVALIDATTRID;
                    return(result);
                }

                return(m_mapper.GetLocalDataValue(value));
            }

            default:
            {
                return(ComHdaProxy.GetAttributeValue(session, m_mapper, attributeId, values[index]));
            }
            }
        }
        /// <summary>
        /// Gets the remote data value.
        /// </summary>
        /// <param name="localValue">The local value.</param>
        /// <param name="remoteType">The remote data type.</param>
        /// <returns>The remote data value.</returns>
        /// <exception cref="COMException">Thrown if a conversion error occurs.</exception>
        public DataValue GetRemoteDataValue(DaValue localValue, TypeInfo remoteType)
        {
            DataValue remoteValue = new DataValue();

            remoteValue.SourceTimestamp = localValue.Timestamp;

            if (localValue.Error < 0)
            {
                throw ComUtils.CreateComException(localValue.Error);
            }

            remoteValue.StatusCode = ComUtils.GetHdaQualityCode(localValue.HdaQuality);

            try
            {
                remoteValue.WrappedValue = GetRemoteValue(new Variant(localValue.Value), remoteType);
            }
            catch (Exception e)
            {
                throw ComUtils.CreateComException(e, ResultIds.E_BADTYPE);
            }

            return(remoteValue);
        }
Beispiel #6
0
        /// <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);
        }