PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 218 lines | 111 code | 16 blank | 91 comment | 5 complexity | 7c7394b510406204f9ce71ec92750bba MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, EPL-1.0
  1. //******************************************************************************************************
  2. // Common.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. // 09/15/2009 - Stephen C. Wills
  22. // Added new header and license agreement.
  23. // 04/21/2010 - J.Ritchie Carroll
  24. // Added GetFormattedSignalTypeName signal type enumeration extension.
  25. // 10/5/2012 - Gavin E. Holden
  26. // Added new header and license agreement.
  27. //
  28. //******************************************************************************************************
  29. using System;
  30. using System.IO;
  31. using System.Runtime.Serialization.Formatters;
  32. using System.Runtime.Serialization.Formatters.Soap;
  33. using TimeSeriesFramework;
  34. using TVA.Parsing;
  35. namespace TVA.PhasorProtocols
  36. {
  37. /// <summary>
  38. /// Common constants, functions and extensions for phasor classes.
  39. /// </summary>
  40. public static class Common
  41. {
  42. /// <summary>
  43. /// Typical data stream synchrnonization byte.
  44. /// </summary>
  45. public const byte SyncByte = 0xAA;
  46. /// <summary>
  47. /// Undefined measurement key.
  48. /// </summary>
  49. internal static MeasurementKey UndefinedKey = new MeasurementKey(Guid.Empty, uint.MaxValue, "__");
  50. /// <summary>
  51. /// This is a common optimized block copy function for binary data.
  52. /// </summary>
  53. /// <param name="source">Source buffer to copy data from.</param>
  54. /// <param name="destination">Destination buffer to hold copied buffer data.</param>
  55. /// <param name="index">Index into <paramref name="destination"/> buffer to begin copy. Index is automatically incremented by <paramref name="length"/>.</param>
  56. /// <param name="length">Number of bytes to copy from source.</param>
  57. /// <remarks>
  58. /// Source index is always zero so hence not requested. This function automatically advances index for convenience.
  59. /// </remarks>
  60. public static void CopyImage(this byte[] source, byte[] destination, ref int index, int length)
  61. {
  62. if (length > 0)
  63. {
  64. Buffer.BlockCopy(source, 0, destination, index, length);
  65. index += length;
  66. }
  67. }
  68. /// <summary>
  69. /// This is a common optimized block copy function for any kind of data.
  70. /// </summary>
  71. /// <param name="channel">Source channel with BinaryImage data to copy.</param>
  72. /// <param name="destination">Destination buffer to hold copied buffer data.</param>
  73. /// <param name="index">
  74. /// Index into <paramref name="destination"/> buffer to begin copy. Index is automatically incremented by <see cref="ISupportBinaryImage.BinaryLength"/>.
  75. /// </param>
  76. /// <remarks>
  77. /// This function automatically advances index for convenience.
  78. /// </remarks>
  79. public static void CopyImage(this ISupportBinaryImage channel, byte[] destination, ref int index)
  80. {
  81. index += channel.GenerateBinaryImage(destination, index);
  82. }
  83. /// <summary>
  84. /// Deserializes a configuration frame from an XML file.
  85. /// </summary>
  86. /// <param name="configFileName">Path and file name of XML configuration file.</param>
  87. /// <returns>Deserialized <see cref="IConfigurationFrame"/>.</returns>
  88. public static IConfigurationFrame DeserializeConfigurationFrame(string configFileName)
  89. {
  90. IConfigurationFrame configFrame = null;
  91. FileStream configFile = null;
  92. try
  93. {
  94. configFile = File.Open(configFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  95. configFrame = DeserializeConfigurationFrame(configFile);
  96. }
  97. finally
  98. {
  99. if (configFile != null)
  100. configFile.Close();
  101. }
  102. return configFrame;
  103. }
  104. /// <summary>
  105. /// Deserializes a configuration frame from an XML stream.
  106. /// </summary>
  107. /// <param name="configStream"><see cref="Stream"/> that contains an XML serialized configuration frame.</param>
  108. /// <returns>Deserialized <see cref="IConfigurationFrame"/>.</returns>
  109. public static IConfigurationFrame DeserializeConfigurationFrame(Stream configStream)
  110. {
  111. IConfigurationFrame configFrame = null;
  112. SoapFormatter xmlSerializer = new SoapFormatter();
  113. // TODO: When project is renamed to "PhasorProtocols.dll", uncomment the following:
  114. //string xmlFile = Encoding.Default.GetString(configStream.ReadStream());
  115. //xmlFile = xmlFile.Replace("TVA.Phasors", "PhasorProtocols");
  116. //xmlFile = xmlFile.Replace("TVA.PhasorProtocols", "PhasorProtocols");
  117. xmlSerializer.AssemblyFormat = FormatterAssemblyStyle.Simple;
  118. xmlSerializer.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
  119. //configFrame = xmlSerializer.Deserialize(new MemoryStream(Encoding.Default.GetBytes(xmlFile))) as IConfigurationFrame;
  120. configFrame = xmlSerializer.Deserialize(configStream) as IConfigurationFrame;
  121. return configFrame;
  122. }
  123. /// <summary>
  124. /// Removes control characters and null from a string.
  125. /// </summary>
  126. /// <param name="value">Source <see cref="String"/> to validate.</param>
  127. /// <remarks>
  128. /// Strings reported from field devices can be full of inconsistencies, this function helps clean-up the strings.
  129. /// </remarks>
  130. /// <returns><paramref name="value"/> with control characters and nulls removed.</returns>
  131. public static string GetValidLabel(this string value)
  132. {
  133. return value.RemoveNull().ReplaceControlCharacters().Trim();
  134. }
  135. /// <summary>
  136. /// Returns display friendly protocol name.
  137. /// </summary>
  138. /// <param name="protocol"><see cref="PhasorProtocol"/> to return display name for.</param>
  139. /// <returns>Friendly protocol display name for specified phasor <paramref name="protocol"/>.</returns>
  140. public static string GetFormattedProtocolName(this PhasorProtocol protocol)
  141. {
  142. switch (protocol)
  143. {
  144. case PhasorProtocol.IeeeC37_118V2:
  145. return "IEEE C37.118.2-2011";
  146. case PhasorProtocol.IeeeC37_118V1:
  147. return "IEEE C37.118-2005";
  148. case PhasorProtocol.IeeeC37_118D6:
  149. return "IEEE C37.118 Draft 6";
  150. case PhasorProtocol.Ieee1344:
  151. return "IEEE 1344-1995";
  152. case PhasorProtocol.BpaPdcStream:
  153. return "BPA PDCstream";
  154. case PhasorProtocol.FNet:
  155. return "UTK F-NET";
  156. case PhasorProtocol.SelFastMessage:
  157. return "SEL Fast Message";
  158. case PhasorProtocol.Macrodyne:
  159. return "Macrodyne";
  160. case PhasorProtocol.Iec61850_90_5:
  161. return "IEC 61850-90-5";
  162. default:
  163. return protocol.ToString().Replace('_', '.').ToUpper();
  164. }
  165. }
  166. /// <summary>
  167. /// Returns display friendly signal type name.
  168. /// </summary>
  169. /// <param name="signalType"><see cref="SignalType"/> to return display name for.</param>
  170. /// <returns>Friendly protocol display name for specified <paramref name="signalType"/>.</returns>
  171. public static string GetFormattedSignalTypeName(this SignalType signalType)
  172. {
  173. switch (signalType)
  174. {
  175. case SignalType.IPHM:
  176. return "Current phase magnitude";
  177. case SignalType.IPHA:
  178. return "Current phase angle";
  179. case SignalType.VPHM:
  180. return "Voltage phase magnitude";
  181. case SignalType.VPHA:
  182. return "Voltage phase angle";
  183. case SignalType.FREQ:
  184. return "Frequency";
  185. case SignalType.DFDT:
  186. return "Frequency delta (dF/dt)";
  187. case SignalType.ALOG:
  188. return "Analog";
  189. case SignalType.FLAG:
  190. return "Status flags";
  191. case SignalType.DIGI:
  192. return "Digital";
  193. case SignalType.CALC:
  194. return "Calculated";
  195. case SignalType.NONE:
  196. return "Undefined";
  197. default:
  198. return signalType.ToString().ToTitleCase();
  199. }
  200. }
  201. }
  202. }