PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/Synchrophasor/Current Version/Source/Libraries/TVA.PhasorProtocols/DigitalValueBase.cs

#
C# | 243 lines | 114 code | 32 blank | 97 comment | 5 complexity | c7b099a14190333a0ab70d5bafba0d53 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, EPL-1.0
  1. //******************************************************************************************************
  2. // DigitalValueBase.cs - Gbtc
  3. //
  4. // Copyright Š 2010, Grid Protection Alliance. All Rights Reserved.
  5. //
  6. // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
  7. // the NOTICE file distributed with this work for additional information regarding copyright ownership.
  8. // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
  9. // not use this file except in compliance with the License. You may obtain a copy of the License at:
  10. //
  11. // http://www.opensource.org/licenses/eclipse-1.0.php
  12. //
  13. // Unless agreed to in writing, the subject software distributed under the License is distributed on an
  14. // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
  15. // License for the specific language governing permissions and limitations.
  16. //
  17. // Code Modification History:
  18. // ----------------------------------------------------------------------------------------------------
  19. // 02/18/2005 - J. Ritchie Carroll
  20. // Generated original version of source code.
  21. // 08/07/2009 - Josh L. Patterson
  22. // Edited Comments.
  23. // 09/15/2009 - Stephen C. Wills
  24. // Added new header and license agreement.
  25. // 10/5/2012 - Gavin E. Holden
  26. // Added new header and license agreement.
  27. //
  28. //******************************************************************************************************
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Runtime.Serialization;
  32. using TimeSeriesFramework;
  33. namespace TVA.PhasorProtocols
  34. {
  35. /// <summary>
  36. /// Represents the common implementation of the protocol independent representation of a digital value.
  37. /// </summary>
  38. [Serializable()]
  39. public abstract class DigitalValueBase : ChannelValueBase<IDigitalDefinition>, IDigitalValue
  40. {
  41. #region [ Members ]
  42. // Fields
  43. private ushort m_value;
  44. private bool m_valueAssigned;
  45. #endregion
  46. #region [ Constructors ]
  47. /// <summary>
  48. /// Creates a new <see cref="DigitalValueBase"/>.
  49. /// </summary>
  50. /// <param name="parent">The <see cref="IDataCell"/> parent of this <see cref="DigitalValueBase"/>.</param>
  51. /// <param name="digitalDefinition">The <see cref="IDigitalDefinition"/> associated with this <see cref="DigitalValueBase"/>.</param>
  52. protected DigitalValueBase(IDataCell parent, IDigitalDefinition digitalDefinition)
  53. : base(parent, digitalDefinition)
  54. {
  55. }
  56. /// <summary>
  57. /// Creates a new <see cref="DigitalValueBase"/> from specified parameters.
  58. /// </summary>
  59. /// <param name="parent">The <see cref="IDataCell"/> parent of this <see cref="DigitalValueBase"/>.</param>
  60. /// <param name="digitalDefinition">The <see cref="IDigitalDefinition"/> associated with this <see cref="DigitalValueBase"/>.</param>
  61. /// <param name="value">The unsigned 16-bit integer value (composed of digital bits) that represents this <see cref="DigitalValueBase"/>.</param>
  62. protected DigitalValueBase(IDataCell parent, IDigitalDefinition digitalDefinition, ushort value)
  63. : base(parent, digitalDefinition)
  64. {
  65. m_value = value;
  66. m_valueAssigned = (value != ushort.MaxValue);
  67. }
  68. /// <summary>
  69. /// Creates a new <see cref="DigitalValueBase"/> from serialization parameters.
  70. /// </summary>
  71. /// <param name="info">The <see cref="SerializationInfo"/> with populated with data.</param>
  72. /// <param name="context">The source <see cref="StreamingContext"/> for this deserialization.</param>
  73. protected DigitalValueBase(SerializationInfo info, StreamingContext context)
  74. : base(info, context)
  75. {
  76. // Deserialize digital value
  77. m_value = info.GetUInt16("value");
  78. m_valueAssigned = true;
  79. }
  80. #endregion
  81. #region [ Properties ]
  82. /// <summary>
  83. /// Gets or sets the unsigned 16-bit integer value (composed of digital bits) that represents this <see cref="DigitalValueBase"/>.
  84. /// </summary>
  85. public virtual ushort Value
  86. {
  87. get
  88. {
  89. return m_value;
  90. }
  91. set
  92. {
  93. m_value = value;
  94. m_valueAssigned = true;
  95. }
  96. }
  97. /// <summary>
  98. /// Gets boolean value that determines if none of the composite values of <see cref="DigitalValueBase"/> have been assigned a value.
  99. /// </summary>
  100. /// <returns>True, if no composite values have been assigned a value; otherwise, false.</returns>
  101. public override bool IsEmpty
  102. {
  103. get
  104. {
  105. return !m_valueAssigned;
  106. }
  107. }
  108. /// <summary>
  109. /// Gets total number of composite values that this <see cref="DigitalValueBase"/> provides.
  110. /// </summary>
  111. public override int CompositeValueCount
  112. {
  113. get
  114. {
  115. return 1;
  116. }
  117. }
  118. /// <summary>
  119. /// Gets the length of the <see cref="BodyImage"/>.
  120. /// </summary>
  121. protected override int BodyLength
  122. {
  123. get
  124. {
  125. return 2;
  126. }
  127. }
  128. /// <summary>
  129. /// Gets the binary body image of the <see cref="DigitalValueBase"/> object.
  130. /// </summary>
  131. protected override byte[] BodyImage
  132. {
  133. get
  134. {
  135. byte[] buffer = new byte[BodyLength];
  136. EndianOrder.BigEndian.CopyBytes(m_value, buffer, 0);
  137. return buffer;
  138. }
  139. }
  140. /// <summary>
  141. /// <see cref="Dictionary{TKey,TValue}"/> of string based property names and values for the <see cref="DigitalValueBase"/> object.
  142. /// </summary>
  143. public override Dictionary<string, string> Attributes
  144. {
  145. get
  146. {
  147. Dictionary<string, string> baseAttributes = base.Attributes;
  148. byte[] valueBytes = BitConverter.GetBytes(Value);
  149. baseAttributes.Add("Digital Value", Value.ToString());
  150. baseAttributes.Add("Digital Value (Big Endian Bits)", ByteEncoding.BigEndianBinary.GetString(valueBytes));
  151. baseAttributes.Add("Digital Value (Hexadecimal)", "0x" + ByteEncoding.Hexadecimal.GetString(valueBytes));
  152. return baseAttributes;
  153. }
  154. }
  155. #endregion
  156. #region [ Methods ]
  157. /// <summary>
  158. /// Gets the specified composite value of this <see cref="DigitalValueBase"/>.
  159. /// </summary>
  160. /// <param name="index">Index of composite value to retrieve.</param>
  161. /// <remarks>
  162. /// Some <see cref="ChannelValueBase{T}"/> implementations can contain more than one value, this method is used to abstractly expose each value.
  163. /// </remarks>
  164. /// <returns>A <see cref="Double"/> representing the composite value.</returns>
  165. public override double GetCompositeValue(int index)
  166. {
  167. if (index == 0)
  168. return m_value;
  169. throw new ArgumentOutOfRangeException("index", "Invalid composite index requested");
  170. }
  171. /// <summary>
  172. /// Gets function used to apply a downsampling filter over a sequence of <see cref="IMeasurement"/> values.
  173. /// </summary>
  174. /// <param name="index">Index of composite value for which to retrieve its filter function.</param>
  175. /// <returns>Majority value filter function since all values are digital in nature.</returns>
  176. public override MeasurementValueFilterFunction GetMeasurementValueFilterFunction(int index)
  177. {
  178. if (index != 0)
  179. throw new ArgumentOutOfRangeException("index", "Invalid composite index requested");
  180. // Digital values shouldn't be averaged, so a majority value filter is applied when downsampling
  181. return Measurement.MajorityValueFilter;
  182. }
  183. /// <summary>
  184. /// Parses the binary body image.
  185. /// </summary>
  186. /// <param name="buffer">Binary image to parse.</param>
  187. /// <param name="startIndex">Start index into <paramref name="buffer"/> to begin parsing.</param>
  188. /// <param name="length">Length of valid data within <paramref name="buffer"/>.</param>
  189. /// <returns>The length of the data that was parsed.</returns>
  190. protected override int ParseBodyImage(byte[] buffer, int startIndex, int length)
  191. {
  192. // Length is validated at a frame level well in advance so that low level parsing routines do not have
  193. // to re-validate that enough length is available to parse needed information as an optimization...
  194. m_value = EndianOrder.BigEndian.ToUInt16(buffer, startIndex);
  195. m_valueAssigned = true;
  196. return 2;
  197. }
  198. /// <summary>
  199. /// Populates a <see cref="SerializationInfo"/> with the data needed to serialize the target object.
  200. /// </summary>
  201. /// <param name="info">The <see cref="SerializationInfo"/> to populate with data.</param>
  202. /// <param name="context">The destination <see cref="StreamingContext"/> for this serialization.</param>
  203. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  204. {
  205. base.GetObjectData(info, context);
  206. // Serialize digital value
  207. info.AddValue("value", m_value);
  208. }
  209. #endregion
  210. }
  211. }