Files

174 lines
7.5 KiB
C#
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
/*
* TDAS.File.Writer.cs
*
* Copyright © 2009
* Diversified Technical Systems, Inc.
* All Rights Reserved
*/
using System;
using System.IO;
using System.Linq;
using System.Text;
using DTS.Common.Utilities.Logging;
namespace DTS.Serialization.TDAS
{
// *** see TDAS.File.cs ***
public partial class File
{ ///
/// <summary>
/// Utility object for serializing <see cref="DTS.Serialization.Test"/>s to disk
/// in the Diadem
/// </summary>
///
public class Writer : Writer<File>, IWriter<Test>
{
/// <summary>
/// Initialize an instance of the Diadem.File.Writer class.
/// </summary>
///
/// <param name="fileType">
/// The associated <see cref="DTS.SErialization.Diadem.File"/> object.
/// </param>
///
internal Writer(File fileType, int encoding)
: base(fileType, encoding)
{
}
/// <summary>
/// Write the specified test to the specified pathname.
/// </summary>
///
/// <param name="pathname">
/// The <see cref="string"/> pathname to which the specified test should be serialized.
/// </param>
///
/// <param name="test">
/// The <see cref="DTS.Serialization.Test"/> to be written out.
/// </param>
///
public void Write(string pathname, string id, Test test, bool bFiltering, bool includeGroupNameInISOExport, double minStartTime, int dataCollectionLength)
{
throw new NotSupportedException("TDAS::File::Writer Write(pathname, id, test, bFiltering) not supported");
}
private const string TLF_FILE_BACKUP_EXTENSION = ".TLF PreTest Backup";
/// <summary>
/// Write the representation file/files of the specified DTS.Serialization.Test
/// at the given pathname.
/// </summary>
///
/// <param name="targetPathname">
/// The <see cref="string"/> pathname of the specified object's resulting file
/// representation.
/// </param>
///
public void Write(string pathname,
string id,
string dataFolder,
Test test,
bool bFiltering,
bool includeGroupNameInISOExport,
FilteredData fd,
Test.Module.Channel tmChannel,
int channelNumber,
BeginEventHandler beginEventHandler,
CancelEventHandler cancelEventHandler,
EndEventHandler endEventHandler,
TickEventHandler tickEventHandler,
ErrorEventHandler errorEventHandler,
CancelRequested cancelRequested,
double minStartTime,
int dataCollectionLength)
{
System.Exception exception = null;
try
{
beginEventHandler?.Invoke(this, Convert.ToUInt32(test.Channels.Count));
var tlf = new TLF(pathname, test, id);
using (var sw = new StreamWriter(pathname, false, Encoding.Default))
{
tlf.Serialize(sw);
sw.Flush();
sw.Close();
}
var tlfFileInfo = new FileInfo(pathname);
var backupTLFFileName = tlfFileInfo.Name.Replace(tlfFileInfo.Extension, TLF_FILE_BACKUP_EXTENSION);
var backupFilePath = Path.Combine(tlfFileInfo.DirectoryName, backupTLFFileName);
try
{
System.IO.File.Copy(tlfFileInfo.FullName, backupFilePath);
}
catch (Exception ex)
{
APILogger.Log($"Failed to backup file {tlfFileInfo.FullName} to {backupFilePath}", ex);
}
var startingChannel = tlf.LastChannelNumber + 1;
var startingSquibChannel = 901;
var maxSampleRate = test.Channels.Select(ch => ch.ParentModule.SampleRateHz).Max();
for (var i = 0; i < test.Channels.Count; i++)
{
var fi = new FileInfo(pathname);
var newFile = string.Empty;
if ((test.Channels[i] as Test.Module.AnalogInputChannel).IsSquibChannel)
{
newFile = fi.FullName.Replace(fi.Extension, string.Format("{0:000}.BIN", startingSquibChannel));
startingSquibChannel++;
}
else
{
newFile = fi.FullName.Replace(fi.Extension, string.Format("{0:000}.BIN", startingChannel));
startingChannel++;
}
using (var bw = new BinaryWriter(new FileStream(newFile, FileMode.OpenOrCreate)))
{
var bin = new TLFBin(test.Channels[i], maxSampleRate);
bin.Serialize(bw, test.Channels[i]);
bw.Flush();
bw.Close();
}
tickEventHandler?.Invoke(this, Convert.ToDouble(i + 1));
}
}
catch (System.Exception ex)
{
exception = ex;
APILogger.Log("encountered problem writing TDAS test files", ex);
}
if (null != errorEventHandler && null != exception)
{
endEventHandler(this);
errorEventHandler(this, exception);
}
else if (null != exception) { throw exception; }
else
{
tickEventHandler?.Invoke(this, 100.0);
endEventHandler?.Invoke(this);
}
}
public void Initialize(string pathname,
string id,
string dataFolder,
Test test,
bool bFiltering,
bool includeGroupNameInISOExport,
FilteredData fd,
Test.Module.Channel tmChannel,
int channelNumber,
BeginEventHandler beginEventHandler,
CancelEventHandler cancelEventHandler,
EndEventHandler endEventHandler,
TickEventHandler tickEventHandler,
ErrorEventHandler errorEventHandler,
CancelRequested cancelRequested)
{
}
}
}
}