/* * 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 { /// /// /// Utility object for serializing s to disk /// in the Diadem /// /// public class Writer : Writer, IWriter { /// /// Initialize an instance of the Diadem.File.Writer class. /// /// /// /// The associated object. /// /// internal Writer(File fileType, int encoding) : base(fileType, encoding) { } /// /// Write the specified test to the specified pathname. /// /// /// /// The pathname to which the specified test should be serialized. /// /// /// /// The to be written out. /// /// 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"; /// /// Write the representation file/files of the specified DTS.Serialization.Test /// at the given pathname. /// /// /// /// The pathname of the specified object's resulting file /// representation. /// /// 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) { } } } }