//******************************************************************************************************
// AnalogValueBase.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 an analog value.
///
[Serializable]
public abstract class AnalogValueBase : ChannelValueBase, IAnalogValue
{
#region [ Members ]
// Fields
private double m_value;
private bool m_valueAssigned;
#endregion
#region [ Constructors ]
///
/// Creates a new .
///
/// The parent of this .
/// The associated with this .
protected AnalogValueBase(IDataCell parent, IAnalogDefinition analogDefinition)
: this(parent, analogDefinition, double.NaN)
{
}
///
/// Creates a new from specified parameters.
///
/// The parent of this .
/// The associated with this .
/// The floating point value that represents this .
protected AnalogValueBase(IDataCell parent, IAnalogDefinition analogDefinition, double value)
: base(parent, analogDefinition)
{
m_value = value;
m_valueAssigned = !double.IsNaN(value);
}
///
/// Creates a new from serialization parameters.
///
/// The with populated with data.
/// The source for this deserialization.
protected AnalogValueBase(SerializationInfo info, StreamingContext context)
: base(info, context)
{
// Deserialize analog value
m_value = info.GetDouble("value");
m_valueAssigned = true;
}
#endregion
#region [ Properties ]
///
/// Gets or sets the floating point value that represents this .
///
public virtual double Value
{
get => m_value;
set
{
m_value = value;
m_valueAssigned = true;
}
}
///
/// Gets or sets the integer representation of this value.
///
public virtual int IntegerValue
{
get
{
unchecked
{
return (int)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 .
///
///
/// 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 2;
return 4;
}
}
///
/// 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)m_value, buffer, 0);
else
BigEndian.CopyBytes((float)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;
baseAttributes.Add("Analog Value", Value.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 composite value as .
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.
///
/// 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)
{
m_value = BigEndian.ToInt16(buffer, startIndex);
m_valueAssigned = true;
return 2;
}
m_value = BigEndian.ToSingle(buffer, startIndex);
m_valueAssigned = true;
return 4;
}
///
/// 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 analog value
info.AddValue("value", m_value);
}
#endregion
}
}