#region Copyright (c) 2011-2023 Technosoftware GmbH. All rights reserved //----------------------------------------------------------------------------- // Copyright (c) 2011-2023 Technosoftware GmbH. All rights reserved // Web: https://www.technosoftware.com // // The source code in this file is covered under a dual-license scenario: // - Owner of a purchased license: SCLA 1.0 // - GPL V3: everybody else // // SCLA license terms accompanied with this source code. // See SCLA 1.0: https://technosoftware.com/license/Source_Code_License_Agreement.pdf // // GNU General Public License as published by the Free Software Foundation; // version 3 of the License are accompanied with this source code. // See https://technosoftware.com/license/GPLv3License.txt // // This source code is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. //----------------------------------------------------------------------------- #endregion Copyright (c) 2011-2023 Technosoftware GmbH. All rights reserved #region Using Directives using System; #endregion namespace Technosoftware.DaAeHdaClient.Da { /// /// Contains the quality field for an item value. /// [Serializable] public struct TsCDaQuality { #region Fields private TsDaQualityBits qualityBits_; private TsDaLimitBits limitBits_; private byte vendorBits_; #endregion #region Constructors, Destructor, Initialization /// /// Initializes the object with the specified quality. /// public TsCDaQuality(TsDaQualityBits quality) { qualityBits_ = quality; limitBits_ = TsDaLimitBits.None; vendorBits_ = 0; } /// /// Initializes the object from the contents of a 16 bit integer. /// public TsCDaQuality(short code) { qualityBits_ = (TsDaQualityBits)(code & (short)TsDaQualityMasks.QualityMask); limitBits_ = (TsDaLimitBits)(code & (short)TsDaQualityMasks.LimitMask); vendorBits_ = (byte)((code & (short)TsDaQualityMasks.VendorMask) >> 8); } #endregion #region Properties /// /// The value in the quality bits field. /// public TsDaQualityBits QualityBits { get => qualityBits_; set => qualityBits_ = value; } /// /// The value in the limit bits field. /// public TsDaLimitBits LimitBits { get => limitBits_; set => limitBits_ = value; } /// /// The value in the quality bits field. /// public byte VendorBits { get => vendorBits_; set => vendorBits_ = value; } /// /// A 'good' quality value. /// public static readonly TsCDaQuality Good = new TsCDaQuality(TsDaQualityBits.Good); /// /// An 'bad' quality value. /// public static readonly TsCDaQuality Bad = new TsCDaQuality(TsDaQualityBits.Bad); #endregion #region Public Methods /// /// Returns the quality as a 16 bit integer. /// public short GetCode() { ushort code = 0; code |= (ushort)QualityBits; code |= (ushort)LimitBits; code |= (ushort)(VendorBits << 8); return (code <= short.MaxValue) ? (short)code : (short)-((ushort.MaxValue + 1 - code)); } /// /// Initializes the quality from a 16 bit integer. /// public void SetCode(short code) { qualityBits_ = (TsDaQualityBits)(code & (short)TsDaQualityMasks.QualityMask); limitBits_ = (TsDaLimitBits)(code & (short)TsDaQualityMasks.LimitMask); vendorBits_ = (byte)((code & (short)TsDaQualityMasks.VendorMask) >> 8); } /// /// Returns true if the objects are equal. /// public static bool operator ==(TsCDaQuality a, TsCDaQuality b) { return a.Equals(b); } /// /// Returns true if the objects are not equal. /// public static bool operator !=(TsCDaQuality a, TsCDaQuality b) { return !a.Equals(b); } #endregion #region Object Member Overrides /// /// Converts a quality to a string with the format: 'quality[limit]:vendor'. /// public override string ToString() { string text = null; switch (QualityBits) { case TsDaQualityBits.Good: text += "(Good"; break; case TsDaQualityBits.GoodLocalOverride: text += "(Good:Local Override"; break; case TsDaQualityBits.Bad: text += "(Bad"; break; case TsDaQualityBits.BadConfigurationError: text += "(Bad:Configuration Error"; break; case TsDaQualityBits.BadNotConnected: text += "(Bad:Not Connected"; break; case TsDaQualityBits.BadDeviceFailure: text += "(Bad:Device Failure"; break; case TsDaQualityBits.BadSensorFailure: text += "(Bad:Sensor Failure"; break; case TsDaQualityBits.BadLastKnownValue: text += "(Bad:Last Known Value"; break; case TsDaQualityBits.BadCommFailure: text += "(Bad:Communication Failure"; break; case TsDaQualityBits.BadOutOfService: text += "(Bad:Out of Service"; break; case TsDaQualityBits.BadWaitingForInitialData: text += "(Bad:Waiting for Initial Data"; break; case TsDaQualityBits.Uncertain: text += "(Uncertain"; break; case TsDaQualityBits.UncertainLastUsableValue: text += "(Uncertain:Last Usable Value"; break; case TsDaQualityBits.UncertainSensorNotAccurate: text += "(Uncertain:Sensor not Accurate"; break; case TsDaQualityBits.UncertainEUExceeded: text += "(Uncertain:Engineering Unit exceeded"; break; case TsDaQualityBits.UncertainSubNormal: text += "(Uncertain:Sub Normal"; break; } if (LimitBits != TsDaLimitBits.None) { text += $":[{LimitBits.ToString()}]"; } else { text += ":Not Limited"; } if (VendorBits != 0) { text += $":{VendorBits,0:X})"; } else { text += ")"; } return text; } /// /// Determines whether the specified Object is equal to the current Quality /// public override bool Equals(object target) { if (target == null || target.GetType() != typeof(TsCDaQuality)) return false; var quality = (TsCDaQuality)target; if (QualityBits != quality.QualityBits) return false; if (LimitBits != quality.LimitBits) return false; if (VendorBits != quality.VendorBits) return false; return true; } /// /// Returns hash code for the current Quality. /// public override int GetHashCode() { return GetCode(); } #endregion } }