Пример #1
0
        /// <summary>
        /// Creates an array of item value result objects from the callback data.
        /// </summary>
        internal static DaValue[] GetItemValues(
            int dwCount,
            object[] pvValues,
            short[] pwQualities,
            System.Runtime.InteropServices.ComTypes.FILETIME[] pftTimeStamps,
            int[] pErrors)
        {
            // contruct the item value results.
            DaValue[] values = new DaValue[dwCount];

            for (int ii = 0; ii < dwCount; ii++)
            {
                DaValue value = values[ii] = new DaValue();

                value.Error = pErrors[ii];

                if (pErrors[ii] >= 0)
                {
                    value.Value     = ComUtils.ProcessComValue(pvValues[ii]);
                    value.Quality   = pwQualities[ii];
                    value.Timestamp = ComUtils.GetDateTime(pftTimeStamps[ii]);
                }
            }

            // return results
            return(values);
        }
Пример #2
0
        /// <summary>
        /// Gets the item value.
        /// </summary>
        /// <param name="daValue">The da value.</param>
        /// <param name="uaValue">The ua value.</param>
        /// <param name="diagnosticsMasks">The diagnostics masks.</param>
        /// <returns></returns>
        public static ServiceResult GetItemValue(DaValue daValue, DataValue uaValue, DiagnosticsMasks diagnosticsMasks)
        {
            ServiceResult result = null;

            uaValue.Value = null;

            if (daValue == null)
            {
                result = uaValue.StatusCode = StatusCodes.BadOutOfService;
                return(result);
            }

            if (daValue.Timestamp.Kind == DateTimeKind.Unspecified)
            {
                uaValue.SourceTimestamp = DateTime.SpecifyKind(daValue.Timestamp, DateTimeKind.Utc);
            }
            else
            {
                throw new System.Exception(string.Format("The type of data from OPC server is not known: {0}", daValue.Timestamp.Kind.ToString()));
            }

            if (daValue.Error < 0)
            {
                result             = MapReadErrorToServiceResult(daValue.Error, diagnosticsMasks);
                uaValue.StatusCode = result.StatusCode;
                return(result);
            }

            if (daValue.Quality != OpcRcw.Da.Qualities.OPC_QUALITY_GOOD)
            {
                uaValue.StatusCode = ComUtils.GetQualityCode(daValue.Quality);
            }

            if (StatusCode.IsBad(uaValue.StatusCode))
            {
                result = uaValue.StatusCode;
                return(result);
            }

            uaValue.Value = daValue.Value;

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Gets the property value.
        /// </summary>
        /// <typeparam name="T">The expected type for the property value.</typeparam>
        /// <param name="propertyId">The property id.</param>
        /// <param name="value">The value to update.</param>
        /// <param name="isValue">if set to <c>true</c> if the property is required for the value attribute of a node.</param>
        /// <returns>The value cast to the type T.</returns>
        /// <remarks>
        /// This method sets the StatusCode in the DataValue if an error occurs and returns default(T).
        /// The DataValue.Value property is set to the value cast to type T.
        /// </remarks>
        public T GetPropertyValue <T>(int propertyId, DataValue value, bool isValue)
        {
            value.Value = null;

            // find the property value.
            DaValue result = null;

            if (PropertyIds != null && PropertyValues != null)
            {
                for (int ii = 0; ii < PropertyIds.Count; ii++)
                {
                    if (PropertyIds[ii] == propertyId)
                    {
                        if (PropertyValues != null && PropertyValues.Length > ii)
                        {
                            result = PropertyValues[ii];
                        }

                        break;
                    }
                }
            }

            // set the appropriate error code if no value found.
            if (result == null || result.Error < 0)
            {
                value.StatusCode = StatusCodes.BadNotFound;
                return(default(T));
            }

            // update the source timestamp if a value is being read.
            if (isValue)
            {
                value.SourceTimestamp = result.Timestamp;
            }

            // save the value and return the result.
            value.Value = (T)result.Value;
            return((T)result.Value);
        }
Пример #4
0
        /// <summary>
        /// Unmarshals and deallocates a OPCITEMSTATE structures.
        /// </summary>
        internal static DaValue[] GetItemValues(ref IntPtr pInput, int count, bool deallocate)
        {
            DaValue[] output = null;

            if (pInput != IntPtr.Zero && count > 0)
            {
                output = new DaValue[count];

                IntPtr pos = pInput;

                for (int ii = 0; ii < count; ii++)
                {
                    OpcRcw.Da.OPCITEMSTATE result = (OpcRcw.Da.OPCITEMSTATE)Marshal.PtrToStructure(pos, typeof(OpcRcw.Da.OPCITEMSTATE));

                    DaValue value = new DaValue();

                    value.Value     = ComUtils.ProcessComValue(result.vDataValue);
                    value.Quality   = result.wQuality;
                    value.Timestamp = ComUtils.GetDateTime(result.ftTimeStamp);

                    output[ii] = value;

                    if (deallocate)
                    {
                        Marshal.DestroyStructure(pos, typeof(OpcRcw.Da.OPCITEMSTATE));
                    }

                    pos = (IntPtr)(pos.ToInt32() + Marshal.SizeOf(typeof(OpcRcw.Da.OPCITEMSTATE)));
                }

                if (deallocate)
                {
                    Marshal.FreeCoTaskMem(pInput);
                    pInput = IntPtr.Zero;
                }
            }

            return(output);
        }
Пример #5
0
        /// <summary>
        /// Gets the item value.
        /// </summary>
        /// <param name="daValue">The da value.</param>
        /// <param name="uaValue">The ua value.</param>
        /// <param name="diagnosticsMasks">The diagnostics masks.</param>
        /// <returns></returns>
        public static ServiceResult GetItemValue(DaValue daValue, DataValue uaValue, DiagnosticsMasks diagnosticsMasks)
        {
            ServiceResult result = null;

            uaValue.Value = null;

            if (daValue == null)
            {
                result = uaValue.StatusCode = StatusCodes.BadOutOfService;
                return(result);
            }

            uaValue.SourceTimestamp = daValue.Timestamp;

            if (daValue.Error < 0)
            {
                result             = MapReadErrorToServiceResult(daValue.Error, diagnosticsMasks);
                uaValue.StatusCode = result.StatusCode;
                return(result);
            }

            if (daValue.Quality != OpcRcw.Da.Qualities.OPC_QUALITY_GOOD)
            {
                uaValue.StatusCode = ComUtils.GetQualityCode(daValue.Quality);
            }

            if (StatusCode.IsBad(uaValue.StatusCode))
            {
                result = uaValue.StatusCode;
                return(result);
            }

            uaValue.Value = daValue.Value;

            return(result);
        }