using DTS.Common.Interface.Sensors; using DTS.Common.Interface.TestSetups; using System; using System.Collections.Generic; namespace DTS.Common.Classes.TestSetups { public static class ArrayExtensions { public static void Fill(this T[] sourceArray, T with) { for (var i = 0; i < sourceArray.Length; i++) { sourceArray[i] = with; } } public static void SubFill(this T[] source, T with, int startIndex, int finalIndex) { for (var i = startIndex; i < source.Length && i < finalIndex; i++) { source[i] = with; } } public static void SetValues(this T[] source, T[] with, int startIndex, int length, T pad) { source.SubFill(pad, startIndex, startIndex + length); Array.Copy(with, 0, source, startIndex, Math.Min(length, with.Length)); } public static T[] GetValues(this T[] source, int startIndex, int length) { var ret = new T[length]; Array.Copy(source, startIndex, ret, 0, length); return ret; } } /// /// handles writing, reading from an ISF file /// public class ISFFile { private char[] _HeaderLine1 = new char[ConstantsAndEnums.RECORD_LENGTH]; /// /// RECORD_LENGTH bytes (80) /// public char[] HeaderLine1 { get => _HeaderLine1; set { _HeaderLine1.Fill(' '); Array.Copy(value, _HeaderLine1, Math.Min(value.Length, _HeaderLine1.Length)); } } /// /// 8 characters, starting at character 7 /// public char[] TestSetupName { get => _HeaderLine1.GetValues(7, 8); set => _HeaderLine1.SetValues(value, 7, 8, ' '); } /// /// starts at character 15, 5 characters long /// public char[] NumberOfRecords { get => _HeaderLine1.GetValues(15, 5); private set => _HeaderLine1.SetValues(value, 15, 5, ' '); } private void SetNumberOfRecords(int i) { var s = i.ToString(); NumberOfRecords = s.ToCharArray(); } /// /// 22 characters, starting at character 20 /// public char[] TestType { get => _HeaderLine1.GetValues(20, 22); set => _HeaderLine1.SetValues(value, 20, 22, ' '); } /// /// 30 characters, starting at character 42 /// public char[] TestDivision { get => _HeaderLine1.GetValues(42, 30); set => _HeaderLine1.SetValues(value, 42, 30, ' '); } /// /// 8 characters start at character 72, without .TCF extension /// public char[] TCFile { get => _HeaderLine1.GetValues(72, 8); set => _HeaderLine1.SetValues(value, 72, 8, ' '); } /// /// RECORD_LENGTH, we don't use anything from it currently... /// private char[] HeaderLine2 { get; set; } = new char[ConstantsAndEnums.RECORD_LENGTH]; /// /// RECORD_LENGTH, we don't use anything from it currently /// private char[] HeaderLine3 { get; set; } = new char[ConstantsAndEnums.RECORD_LENGTH]; private List _records = new List(); public IISFSensorRecord[] Records => _records.ToArray(); /// /// adds a record, updates record count /// /// public void AddRecord(IISFSensorRecord record) { record.SetDataChannelNumber(Convert.ToInt16(1 + _records.Count)); _records.Add(record); SetNumberOfRecords(Records.Length * 4); //4 "records" per record } /// /// writes file to path /// /// public void WriteToFile(string pathToFile) { if (System.IO.File.Exists(pathToFile)) { Utils.FileUtils.DeleteFileOrMove(pathToFile, LogFunction); } using (var fs = new System.IO.FileStream(pathToFile, System.IO.FileMode.CreateNew)) { using (var bw = new System.IO.BinaryWriter(fs)) { bw.Write(HeaderLine1); bw.Write(HeaderLine2); bw.Write(HeaderLine3); foreach (var record in Records) { record.Write(bw); bw.Flush(); } } } } public void AddSensors(ISensorData[] sensors) { foreach (var sensor in sensors) { var record = new ISFSensorRecord(); record.SetSensor(sensor); AddRecord(record); } } private void LogFunction(params object[] paramlist) { } public ISFFile() { _HeaderLine1.Fill(' '); } } }