/*
* 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
{ ///
///
/// Representation of a SliceRaw-type serialization.
///
///
public partial class File
: Serialization.File, IReadable, IWritable
{ ///
///
/// Initialize an instance of the SliceRaw.File class.
///
///
public File()
: base("Slice Raw")
{
}
///
/// Get the file writer for this file type.
///
public IWriter 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 _Exporter = null;
///
/// Get the file reader for this file type.
///
public IReader 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 _Importer = null;
///
/// File extension for the disk entity that contains the serialized test information.
///
public static string TestFileExtension
{
get => _TestFileExtension.Value;
private set => _TestFileExtension.Value = value;
}
private static readonly Property _TestFileExtension = new Property("TestFileExtension", ".dts", true);
///
/// File extension for the disk entity that contains serialized channel information.
///
public static string ChannelFileExtension
{
get => _ChannelFileExtension.Value;
private set => _ChannelFileExtension.Value = value;
}
private static readonly Property _ChannelFileExtension = new Property("ChannelFileExtension", ".chn", true);
///
/// File extension for the disk entity that contains serialized channel information.
///
public static string CalculatedChannelFileExtension
{
get => _CalculatedChannelFileExtension.Value;
private set => _CalculatedChannelFileExtension.Value = value;
}
private static readonly Property _CalculatedChannelFileExtension = new Property("CalculatedChannelFileExtension", ".cchn", true);
///
/// File extension for the disk entity that contains binary information.
///
public static string BinaryFileExtension
{
get => _BinaryFileExtension.Value;
private set => _BinaryFileExtension.Value = value;
}
private static readonly Property _BinaryFileExtension = new Property("BinaryFileExtension", ".bin", true);
///
/// File extension for the disk entity that contains NMEA information.
///
public static string NMEAFileExtension
{
get => _NMEAFileExtension.Value;
private set => _NMEAFileExtension.Value = value;
}
private static readonly Property _NMEAFileExtension = new Property("NMEAFileExtension", ".nmea", true);
///
/// File extension for the disk entity that contains plaintext information.
///
public static string PlainTextFileExtension
{
get => _PlainTextFileExtension.Value;
private set => _PlainTextFileExtension.Value = value;
}
private static readonly Property _PlainTextFileExtension = new Property("PlainTextFileExtension", ".txt", true);
///
/// File extension for the disk entity that contains module timestamp information.
///
public static string DasTimestampFileExtension
{
get => _DasTimestampFileExtension.Value;
private set => _DasTimestampFileExtension.Value = value;
}
private static readonly Property _DasTimestampFileExtension = new Property("DasTimestampFileExtension", ".bin", true);
///
/// The string that separates the channel numbers from the riffraff.
///
public const string ChannelNumberDelimiter = ".";
public const string AbsoluteDisplayOrderDelimiter = "Ch";
///
/// Extract the channel file number from the specified channel name string.
///
///
///
/// The test name to be incorporated into the channel filename.
///
///
///
/// The channel number to be incorporated into the channel filename.
///
///
///
/// A channel filename that uniquely identifies test and channel
/// that this channel informtion belongs to.
///
///
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);
}
}
///
/// Extract the channel number from the specified channel filename.
///
///
///
/// The channel filename to be converted into a channel number.
///
///
///
/// The channel number represented by the specified file.
///
///
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 = "') + 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);
}
}
}
}