Files
DP44/Common/DTS.Common.Serialization/SliceRaw/SliceRaw.File.cs
2026-04-17 14:55:32 -04:00

345 lines
15 KiB
C#

/*
* SliceRaw.File.cs
*
* Copyright © 2009
* Diversified Technical Systems, Inc.
* All Rights Reserved
*/
using System;
using System.IO;
using DTS.Common.Utilities.DotNetProgrammingConstructs;
using DTS.Common.Utilities.Logging;
using System.Linq;
namespace DTS.Serialization.SliceRaw
{ ///
/// <summary>
/// Representation of a SliceRaw-type serialization.
/// </summary>
///
public partial class File
: Serialization.File, IReadable<Test>, IWritable<Test>
{ ///
/// <summary>
/// Initialize an instance of the SliceRaw.File class.
/// </summary>
///
public File()
: base("Slice Raw")
{
}
/// <summary>
/// Get the file writer for this file type.
/// </summary>
public IWriter<Test> Exporter
{
get
{
try
{
if (_Exporter == null)
_Exporter = new Writer(this, DefaultEncoding);
return _Exporter;
}
catch (System.Exception ex)
{
throw new Exception("encountered problem getting exporter", ex);
}
}
}
private IWriter<Test> _Exporter = null;
/// <summary>
/// Get the file reader for this file type.
/// </summary>
public IReader<Test> Importer
{
get
{
try
{
if (_Importer == null)
_Importer = new Reader(this);
return _Importer;
}
catch (System.Exception ex)
{
throw new Exception("encountered problem getting importer", ex);
}
}
}
private IReader<Test> _Importer = null;
/// <summary>
/// File extension for the disk entity that contains the serialized test information.
/// </summary>
public static string TestFileExtension
{
get => _TestFileExtension.Value;
private set => _TestFileExtension.Value = value;
}
private static readonly Property<string> _TestFileExtension = new Property<string>("TestFileExtension", ".dts", true);
/// <summary>
/// File extension for the disk entity that contains serialized channel information.
/// </summary>
public static string ChannelFileExtension
{
get => _ChannelFileExtension.Value;
private set => _ChannelFileExtension.Value = value;
}
private static readonly Property<string> _ChannelFileExtension = new Property<string>("ChannelFileExtension", ".chn", true);
/// <summary>
/// File extension for the disk entity that contains serialized channel information.
/// </summary>
public static string CalculatedChannelFileExtension
{
get => _CalculatedChannelFileExtension.Value;
private set => _CalculatedChannelFileExtension.Value = value;
}
private static readonly Property<string> _CalculatedChannelFileExtension = new Property<string>("CalculatedChannelFileExtension", ".cchn", true);
/// <summary>
/// File extension for the disk entity that contains binary information.
/// </summary>
public static string BinaryFileExtension
{
get => _BinaryFileExtension.Value;
private set => _BinaryFileExtension.Value = value;
}
private static readonly Property<string> _BinaryFileExtension = new Property<string>("BinaryFileExtension", ".bin", true);
/// <summary>
/// File extension for the disk entity that contains NMEA information.
/// </summary>
public static string NMEAFileExtension
{
get => _NMEAFileExtension.Value;
private set => _NMEAFileExtension.Value = value;
}
private static readonly Property<string> _NMEAFileExtension = new Property<string>("NMEAFileExtension", ".nmea", true);
/// <summary>
/// File extension for the disk entity that contains plaintext information.
/// </summary>
public static string PlainTextFileExtension
{
get => _PlainTextFileExtension.Value;
private set => _PlainTextFileExtension.Value = value;
}
private static readonly Property<string> _PlainTextFileExtension = new Property<string>("PlainTextFileExtension", ".txt", true);
/// <summary>
/// File extension for the disk entity that contains module timestamp information.
/// </summary>
public static string DasTimestampFileExtension
{
get => _DasTimestampFileExtension.Value;
private set => _DasTimestampFileExtension.Value = value;
}
private static readonly Property<string> _DasTimestampFileExtension = new Property<string>("DasTimestampFileExtension", ".bin", true);
/// <summary>
/// The string that separates the channel numbers from the riffraff.
/// </summary>
public const string ChannelNumberDelimiter = ".";
public const string AbsoluteDisplayOrderDelimiter = "Ch";
/// <summary>
/// Extract the channel file number from the specified channel name string.
/// </summary>
///
/// <param name="testName">
/// The test name <see cref="string"/> to be incorporated into the channel filename.
/// </param>
///
/// <param name="channelNumber">
/// The <see cref="int"/> channel number to be incorporated into the channel filename.
/// </param>
///
/// <returns>
/// A channel filename <see cref="string"/> that uniquely identifies test and channel
/// that this channel informtion belongs to.
/// </returns>
///
public string GetChannelFileNameFromTestNameAndChannelNumber(string testName, int channelNumber, int absoluteDisplayOrder)
{
try
{
if (string.IsNullOrEmpty(testName))
throw new ArgumentException("cannot construct channel file name from null/empty test name string");
else
return string.Format("{0}{5}{1:000}{2}{3:000}{4}",
testName, 1 + absoluteDisplayOrder, ChannelNumberDelimiter, channelNumber, ChannelFileExtension, AbsoluteDisplayOrderDelimiter);
}
catch (System.Exception ex)
{
throw new Exception("encountered problem getting channel file name from channel number \"" + channelNumber.ToString() + "\"", ex);
}
}
///
public string GetCalculatedChannelFileNameFromTestNameAndChannelNumber(string testName, int channelNumber)
{
try
{
if (string.IsNullOrEmpty(testName))
throw new ArgumentException("cannot construct channel file name from null/empty test name string");
return testName + ChannelNumberDelimiter + channelNumber.ToString() + CalculatedChannelFileExtension;
}
catch (System.Exception ex)
{
throw new Exception("encountered problem getting channel file name from channel number \"" + channelNumber.ToString() + "\"", ex);
}
}
/// <summary>
/// Extract the channel number from the specified channel filename.
/// </summary>
///
/// <param name="channelFileName">
/// The channel filename <see cref="string"/> to be converted into a channel number.
/// </param>
///
/// <returns>
/// The <see cref="int"/> channel number represented by the specified file.
/// </returns>
///
public override int GetChannelNumberFromChannelFileName(string channelFileName)
{
try
{
var indexOfExtension = channelFileName.LastIndexOf(ChannelFileExtension);
if (!char.IsDigit(channelFileName[indexOfExtension - 1]))
{
//extension prefix detected, back up some more
indexOfExtension = channelFileName.LastIndexOf(ChannelNumberDelimiter, indexOfExtension - 1);
}
var indexOfFirstChannelNumberCharacter = channelFileName.LastIndexOf(ChannelNumberDelimiter, indexOfExtension - 1) + 1;
return int.Parse(channelFileName.Substring(indexOfFirstChannelNumberCharacter, indexOfExtension - indexOfFirstChannelNumberCharacter));
}
catch (System.Exception ex)
{
throw new Exception("encountered problem getting channel number from channel file name", ex);
}
}
///
public string GetTimestampFileNameFromTestNameAndDASSerialNumber(string testName, string dasSerialNumber)
{
try
{
if (string.IsNullOrEmpty(testName))
throw new ArgumentException("cannot construct das file name from null/empty test name string");
else
return string.Format("TS-{0}{2}{1}{3}",
testName, dasSerialNumber, ChannelNumberDelimiter, DasTimestampFileExtension);
}
catch (System.Exception ex)
{
throw new Exception("encountered problem getting das file name from das serial number \"" + dasSerialNumber.ToString() + "\"", ex);
}
}
public static void ReadTestSetup(string testFileName, ref Test _test, ref TestSetup _testSetup)
{
ReadTestSetup(testFileName, out _test, out _testSetup, false);
}
public static void ReadTestSetup(string testFileName, out Test test, out TestSetup testSetup,
bool justDTSFile)
{
var fi = new FileInfo(testFileName);
var directory = fi.Directory.FullName;
test = null;
testSetup = null;
var f = new Serialization.SliceRaw.File();
try
{
using (var reader = new StreamReader(testFileName))
{
var allText = reader.ReadToEnd();
const string delimiter = "<?xml version=";
var delimiterArray = new[] { delimiter };
var allTextArray = allText.Split(delimiterArray, StringSplitOptions.RemoveEmptyEntries);
var xmlCounter = 0;
foreach (var fullstring in allTextArray)
{
var fullString = allTextArray[xmlCounter];
xmlCounter++;
if (!fullString.Contains("Test")) continue;
fullString = delimiter + fullString;
var sr = new StringReader(fullString);
var tr = new System.Xml.XmlTextReader(sr);
var xmlType = fullString.Substring(fullString.IndexOf('>') + 4, 5).TrimEnd();
switch (xmlType)
{
case "Test":
try
{
if (justDTSFile)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(fullString);
var id = (doc.ChildNodes[1] as System.Xml.XmlElement).GetAttribute("Id");
var desc =
(doc.ChildNodes[1] as System.Xml.XmlElement).GetAttribute("Description");
//FB 18312 Get the event number from xml if it's not in xaml calculate it from file name
var eventNum = (doc.ChildNodes[1] as System.Xml.XmlElement).GetAttribute("EventNumber");
if (!string.IsNullOrWhiteSpace(eventNum))
{
var eventNumber = Convert.ToInt32(eventNum);
test = new Test(id, desc, eventNumber);
}
else
{
var fileName = fi.Name;
var segments = fileName.Split('_');
if (Array.Exists(segments, p => p.Contains(DTS.Common.Constants.EventNumber)))
{
var foundEvent = Array.Find(segments, p => p.Contains(DTS.Common.Constants.EventNumber));
var eventNumber = Convert.ToInt32(foundEvent.Replace(DTS.Common.Constants.EventNumber, ""));
test = new Test(id, desc, eventNumber);
}
else
{
test = new Test(id, desc, 0);
}
}
}
else
{
f.Importer.Read(directory, out test);
}
}
catch (System.Exception ex)
{
APILogger.Log(ex);
}
break;
case "TestS":
testSetup = new TestSetup();
var xs = new System.Xml.Serialization.XmlSerializer(testSetup.GetType());
testSetup = (TestSetup)xs.Deserialize(tr);
tr.Close();
sr.Close();
break;
}
}
}
}
catch (System.Exception ex)
{
APILogger.Log("exception in read test setup", ex);
}
}
}
}