//****************************************************************************************************** // DigitalValueBase.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: // ---------------------------------------------------------------------------------------------------- // 02/18/2005 - 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. // 10/5/2012 - Gavin E. Holden // 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 { /// /// Represents the common implementation of the protocol independent representation of a digital value. /// [Serializable] public abstract class DigitalValueBase : ChannelValueBase, IDigitalValue { #region [ Members ] // Fields private ushort m_value; private bool m_valueAssigned; #endregion #region [ Constructors ] /// /// Creates a new . /// /// The parent of this . /// The associated with this . protected DigitalValueBase(IDataCell parent, IDigitalDefinition digitalDefinition) : this(parent, digitalDefinition, ushort.MaxValue) { } /// /// Creates a new from specified parameters. /// /// The parent of this . /// The associated with this . /// The unsigned 16-bit integer value (composed of digital bits) that represents this . protected DigitalValueBase(IDataCell parent, IDigitalDefinition digitalDefinition, ushort value) : base(parent, digitalDefinition) { m_value = value; m_valueAssigned = value != ushort.MaxValue; } /// /// Creates a new from serialization parameters. /// /// The with populated with data. /// The source for this deserialization. protected DigitalValueBase(SerializationInfo info, StreamingContext context) : base(info, context) { // Deserialize digital value m_value = info.GetUInt16("value"); m_valueAssigned = true; } #endregion #region [ Properties ] /// /// Gets or sets the unsigned 16-bit integer value (composed of digital bits) that represents this . /// public virtual ushort Value { get => m_value; set { m_value = value; m_valueAssigned = 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_valueAssigned; /// /// Gets total number of composite values that this provides. /// public override int CompositeValueCount => 1; /// /// Gets the length of the . /// protected override int BodyLength => 2; /// /// Gets the binary body image of the object. /// protected override byte[] BodyImage { get { byte[] buffer = new byte[BodyLength]; BigEndian.CopyBytes(m_value, buffer, 0); return buffer; } } /// /// of string based property names and values for the object. /// public override Dictionary Attributes { get { Dictionary baseAttributes = base.Attributes; byte[] valueBytes = BitConverter.GetBytes(Value); baseAttributes.Add("Digital Value", Value.ToString()); baseAttributes.Add("Digital Value (Big Endian Bits)", ByteEncoding.BigEndianBinary.GetString(valueBytes)); baseAttributes.Add("Digital Value (Hexadecimal)", $"0x{ByteEncoding.Hexadecimal.GetString(valueBytes)}"); 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) { if (index == 0) return m_value; 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. 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... m_value = BigEndian.ToUInt16(buffer, startIndex); m_valueAssigned = true; return 2; } /// /// 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 digital value info.AddValue("value", m_value); } #endregion } }