//****************************************************************************************************** // Common.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. // 09/15/2009 - Stephen C. Wills // Added new header and license agreement. // 04/21/2010 - J.Ritchie Carroll // Added GetFormattedSignalTypeName signal type enumeration extension. // 10/5/2012 - Gavin E. Holden // Added new header and license agreement. // 12/17/2012 - Starlynn Danyelle Gilliam // Modified Header. // //****************************************************************************************************** using System; using System.IO; using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization.Formatters.Soap; using GSF.Parsing; namespace GSF.PhasorProtocols { /// /// Common constants, functions and extensions for phasor classes. /// public static class Common { /// /// Typical data stream synchronization byte. /// public const byte SyncByte = 0xAA; /// /// This is a common optimized block copy function for binary data. /// /// Source buffer to copy data from. /// Destination buffer to hold copied buffer data. /// Index into buffer to begin copy. Index is automatically incremented by . /// Number of bytes to copy from source. /// /// Source index is always zero so hence not requested. This function automatically advances index for convenience. /// public static void CopyImage(this byte[] source, byte[] destination, ref int index, int length) { if (length > 0) { Buffer.BlockCopy(source, 0, destination, index, length); index += length; } } /// /// This is a common optimized block copy function for any kind of data. /// /// Source channel with BinaryImage data to copy. /// Destination buffer to hold copied buffer data. /// /// Index into buffer to begin copy. Index is automatically incremented by . /// /// /// This function automatically advances index for convenience. /// public static void CopyImage(this ISupportBinaryImage channel, byte[] destination, ref int index) { index += channel.GenerateBinaryImage(destination, index); } /// /// Deserializes a configuration frame from an XML file. /// /// Path and file name of XML configuration file. /// Deserialized . public static IConfigurationFrame DeserializeConfigurationFrame(string configFileName) { IConfigurationFrame configFrame; FileStream configFile = null; try { configFile = File.Open(configFileName, FileMode.Open, FileAccess.Read, FileShare.Read); configFrame = DeserializeConfigurationFrame(configFile); } finally { configFile?.Close(); } return configFrame; } /// /// Deserializes a configuration frame from an XML stream. /// /// that contains an XML serialized configuration frame. /// Deserialized . public static IConfigurationFrame DeserializeConfigurationFrame(Stream configStream) { IConfigurationFrame configFrame; SoapFormatter xmlSerializer = new(); xmlSerializer.AssemblyFormat = FormatterAssemblyStyle.Simple; xmlSerializer.TypeFormat = FormatterTypeStyle.TypesWhenNeeded; xmlSerializer.Binder = Serialization.LegacyBinder; //configFrame = xmlSerializer.Deserialize(new MemoryStream(Encoding.Default.GetBytes(xmlFile))) as IConfigurationFrame; configFrame = xmlSerializer.Deserialize(configStream) as IConfigurationFrame; return configFrame; } /// /// Removes control characters and null from a string. /// /// Source to validate. /// /// Strings reported from field devices can be full of inconsistencies, this function helps clean-up the strings. /// /// with control characters and nulls removed. public static string GetValidLabel(this string value) { return value.RemoveNull().ReplaceControlCharacters().Trim(); } /// /// Returns display friendly protocol name. /// /// to return display name for. /// Friendly protocol display name for specified phasor . public static string GetFormattedProtocolName(this PhasorProtocol protocol) { return protocol switch { PhasorProtocol.IEEEC37_118V2 => "IEEE C37.118.2-2011", PhasorProtocol.IEEEC37_118V1 => "IEEE C37.118-2005", PhasorProtocol.IEEEC37_118D6 => "IEEE C37.118 Draft 6", PhasorProtocol.IEEE1344 => "IEEE 1344-1995", PhasorProtocol.BPAPDCstream => "BPA PDCstream", PhasorProtocol.FNET => "UTK F-NET", PhasorProtocol.SelFastMessage => "SEL Fast Message", PhasorProtocol.Macrodyne => "Macrodyne", PhasorProtocol.IEC61850_90_5 => "IEC 61850-90-5", _ => protocol.ToString().Replace('_', '.').ToUpper(), }; } } }