/// <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); }
/// <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); }
/// <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); }
/// <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) { for (int ii = 0; ii < PropertyIds.Count; ii++) { if (PropertyIds[ii] == propertyId) { 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); }
/// <summary> /// Reads the values of the properties from the server. /// </summary> /// <param name="itemId">The item id.</param> /// <param name="propertyIds">The list of property ids to read. All properties are read if null.</param> /// <returns>True if the element has properies.</returns> private DaValue[] ReadPropertyValues(string itemId, params int[] propertyIds) { string methodName = "IOPCItemProperties.GetItemProperties"; // check for trivial case. if (propertyIds == null) { return(null); } int count = propertyIds.Length; DaValue[] results = new DaValue[count]; // check for empty list. if (count == 0) { return(results); } // get the values from the server. IntPtr pValues = IntPtr.Zero; IntPtr pErrors = IntPtr.Zero; try { IOPCItemProperties server = BeginComCall <IOPCItemProperties>(methodName, true); server.GetItemProperties( itemId, count, propertyIds, out pValues, out pErrors); } catch (Exception e) { if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL, ResultIds.E_UNKNOWNITEMID, ResultIds.E_INVALIDITEMID)) { ComUtils.TraceComError(e, methodName); return(null); } for (int ii = 0; ii < count; ii++) { DaValue result = results[ii] = new DaValue(); result.Value = null; result.Quality = OpcRcw.Da.Qualities.OPC_QUALITY_GOOD; result.Timestamp = DateTime.UtcNow; result.Error = Marshal.GetHRForException(e); } return(results); } finally { EndComCall(methodName); } // unmarshal results. object[] values = ComUtils.GetVARIANTs(ref pValues, count, true); int[] errors = ComUtils.GetInt32s(ref pErrors, count, true); for (int ii = 0; ii < count; ii++) { DaValue result = results[ii] = new DaValue(); result.Value = ComUtils.ProcessComValue(values[ii]); result.Quality = OpcRcw.Da.Qualities.OPC_QUALITY_GOOD; result.Timestamp = DateTime.UtcNow; result.Error = errors[ii]; } return(results); }