//****************************************************************************************************** // FrequencyValueBase.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 11/12/2004 - J. Ritchie Carroll // Generated original version of source code. // 08/07/2009 - Josh L. Patterson // Edited Comments. // 09/15/2009 - Stephen C. Wills // Added new header and license agreement. // 12/17/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Runtime.Serialization; namespace GSF.PhasorProtocols { #region [ Enumerations ] /// /// Composite frequency value indices enumeration. /// public enum CompositeFrequencyValue { /// /// Composite frequency value index. /// Frequency, /// /// Composite dF/dt value index. /// DfDt } #endregion /// /// Represents the common implementation of the protocol independent representation of a frequency and dF/dt value. /// [Serializable] public abstract class FrequencyValueBase : ChannelValueBase, IFrequencyValue { #region [ Members ] // Fields private double m_frequency; private double m_dfdt; private bool m_frequencyAssigned; private bool m_dfdtAssigned; #endregion #region [ Constructors ] /// /// Creates a new . /// /// The parent of this . /// The associated with this . protected FrequencyValueBase(IDataCell parent, IFrequencyDefinition frequencyDefinition) : base(parent, frequencyDefinition) { } /// /// Creates a new from specified parameters. /// /// The parent of this . /// The associated with this . /// The floating point value that represents this . /// The floating point value that represents the change in this over time. protected FrequencyValueBase(IDataCell parent, IFrequencyDefinition frequencyDefinition, double frequency, double dfdt) : base(parent, frequencyDefinition) { m_frequency = frequency; m_dfdt = dfdt; m_frequencyAssigned = !double.IsNaN(frequency); m_dfdtAssigned = !double.IsNaN(dfdt); } /// /// Creates a new from serialization parameters. /// /// The with populated with data. /// The source for this deserialization. protected FrequencyValueBase(SerializationInfo info, StreamingContext context) : base(info, context) { // Deserialize frequency value m_frequency = info.GetDouble("frequency"); m_dfdt = info.GetDouble("dfdt"); m_frequencyAssigned = true; m_dfdtAssigned = true; } #endregion #region [ Properties ] /// /// Gets or sets the floating point value that represents this . /// public virtual double Frequency { get => m_frequency; set { m_frequency = value; m_frequencyAssigned = true; } } /// /// Gets or sets the floating point value that represents the change in this over time. /// public virtual double DfDt { get => m_dfdt; set { m_dfdt = value; m_dfdtAssigned = true; } } /// /// Gets or sets the unscaled integer representation of this . /// public virtual int UnscaledFrequency { get { unchecked { return (int)((m_frequency - Definition.Offset) * Definition.ScalingValue); } } set { m_frequency = value / (double)Definition.ScalingValue + Definition.Offset; m_frequencyAssigned = true; } } /// /// Gets or sets the unscaled integer representation of the change in this over time. /// public virtual int UnscaledDfDt { get { unchecked { return (int)((m_dfdt - Definition.DfDtOffset) * Definition.DfDtScalingValue); } } set { m_dfdt = value / (double)Definition.DfDtScalingValue + Definition.DfDtOffset; m_dfdtAssigned = true; } } /// /// Gets boolean value that determines if none of the composite values of have been assigned a value. /// /// True, if no composite values have been assigned a value; otherwise, false. public override bool IsEmpty => !m_frequencyAssigned || !m_dfdtAssigned; /// /// Gets total number of composite values that this provides. /// public override int CompositeValueCount => 2; /// /// Gets the length of the . /// /// /// The base implementation assumes fixed integer values are represented as 16-bit signed /// integers and floating point values are represented as 32-bit single-precision floating-point /// values (i.e., short and float data types respectively). /// protected override int BodyLength { get { if (DataFormat == DataFormat.FixedInteger) return 4; return 8; } } /// /// Gets the binary body image of the object. /// /// /// The base implementation assumes fixed integer values are represented as 16-bit signed /// integers and floating point values are represented as 32-bit single-precision floating-point /// values (i.e., short and float data types respectively). /// protected override byte[] BodyImage { get { byte[] buffer = new byte[BodyLength]; // Had to make a decision on usage versus typical protocol implementation when // exposing values as double / int when protocols typically use float / short for // transmission. Exposing values as double / int makes class more versatile by // allowing future protocol implementations to support higher resolution values // simply by overriding BodyLength, BodyImage and ParseBodyImage. However, exposing // the double / int values runs the risk of providing values that are outside the // data type limitations, hence the unchecked section below. Risk should generally // be low in typical usage scenarios since values being transmitted via a generated // image were likely parsed previously from a binary image with the same constraints. unchecked { if (DataFormat == DataFormat.FixedInteger) { BigEndian.CopyBytes((short)UnscaledFrequency, buffer, 0); BigEndian.CopyBytes((short)UnscaledDfDt, buffer, 2); } else { BigEndian.CopyBytes((float)m_frequency, buffer, 0); BigEndian.CopyBytes((float)m_dfdt, buffer, 4); } } return buffer; } } /// /// of string based property names and values for the object. /// public override Dictionary Attributes { get { Dictionary baseAttributes = base.Attributes; baseAttributes.Add("Frequency Value", Frequency.ToString()); baseAttributes.Add("dF/dt Value", DfDt.ToString()); baseAttributes.Add("Unscaled Frequency Value", UnscaledFrequency.ToString()); baseAttributes.Add("Unscaled dF/dt Value", UnscaledDfDt.ToString()); return baseAttributes; } } #endregion #region [ Methods ] /// /// Gets the specified composite value of this . /// /// Index of composite value to retrieve. /// /// Some implementations can contain more than one value, this method is used to abstractly expose each value. /// /// A representing the composite value. public override double GetCompositeValue(int index) { return index switch { (int)CompositeFrequencyValue.Frequency => m_frequency, (int)CompositeFrequencyValue.DfDt => m_dfdt, _ => throw new ArgumentOutOfRangeException(nameof(index), "Invalid composite index requested"), }; } /// /// Parses the binary body image. /// /// Binary image to parse. /// Start index into to begin parsing. /// Length of valid data within . /// The length of the data that was parsed. /// /// The base implementation assumes fixed integer values are represented as 16-bit signed /// integers and floating point values are represented as 32-bit single-precision floating-point /// values (i.e., short and float data types respectively). /// protected override int ParseBodyImage(byte[] buffer, int startIndex, int length) { // Length is validated at a frame level well in advance so that low level parsing routines do not have // to re-validate that enough length is available to parse needed information as an optimization... if (DataFormat == DataFormat.FixedInteger) { UnscaledFrequency = BigEndian.ToInt16(buffer, startIndex); UnscaledDfDt = BigEndian.ToInt16(buffer, startIndex + 2); return 4; } m_frequency = BigEndian.ToSingle(buffer, startIndex); m_dfdt = BigEndian.ToSingle(buffer, startIndex + 4); m_frequencyAssigned = true; m_dfdtAssigned = true; return 8; } /// /// Populates a with the data needed to serialize the target object. /// /// The to populate with data. /// The destination for this serialization. public override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); // Serialize frequency value info.AddValue("frequency", m_frequency); info.AddValue("dfdt", m_dfdt); } #endregion } }