init
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
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<T>(this T[] sourceArray, T with)
|
||||
{
|
||||
for (var i = 0; i < sourceArray.Length; i++)
|
||||
{
|
||||
sourceArray[i] = with;
|
||||
}
|
||||
}
|
||||
|
||||
public static void SubFill<T>(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<T>(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<T>(this T[] source, int startIndex, int length)
|
||||
{
|
||||
var ret = new T[length];
|
||||
Array.Copy(source, startIndex, ret, 0, length);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// handles writing, reading from an ISF file
|
||||
/// </summary>
|
||||
public class ISFFile
|
||||
{
|
||||
private char[] _HeaderLine1 = new char[ConstantsAndEnums.RECORD_LENGTH];
|
||||
|
||||
/// <summary>
|
||||
/// RECORD_LENGTH bytes (80)
|
||||
/// </summary>
|
||||
public char[] HeaderLine1
|
||||
{
|
||||
get => _HeaderLine1;
|
||||
set
|
||||
{
|
||||
_HeaderLine1.Fill(' ');
|
||||
Array.Copy(value, _HeaderLine1, Math.Min(value.Length, _HeaderLine1.Length));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 8 characters, starting at character 7
|
||||
/// </summary>
|
||||
public char[] TestSetupName
|
||||
{
|
||||
get => _HeaderLine1.GetValues(7, 8);
|
||||
set => _HeaderLine1.SetValues(value, 7, 8, ' ');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// starts at character 15, 5 characters long
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 22 characters, starting at character 20
|
||||
/// </summary>
|
||||
public char[] TestType
|
||||
{
|
||||
get => _HeaderLine1.GetValues(20, 22);
|
||||
set => _HeaderLine1.SetValues(value, 20, 22, ' ');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 30 characters, starting at character 42
|
||||
/// </summary>
|
||||
public char[] TestDivision
|
||||
{
|
||||
get => _HeaderLine1.GetValues(42, 30);
|
||||
set => _HeaderLine1.SetValues(value, 42, 30, ' ');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 8 characters start at character 72, without .TCF extension
|
||||
/// </summary>
|
||||
public char[] TCFile
|
||||
{
|
||||
get => _HeaderLine1.GetValues(72, 8);
|
||||
set => _HeaderLine1.SetValues(value, 72, 8, ' ');
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// RECORD_LENGTH, we don't use anything from it currently...
|
||||
/// </summary>
|
||||
private char[] HeaderLine2 { get; set; } = new char[ConstantsAndEnums.RECORD_LENGTH];
|
||||
|
||||
/// <summary>
|
||||
/// RECORD_LENGTH, we don't use anything from it currently
|
||||
/// </summary>
|
||||
private char[] HeaderLine3 { get; set; } = new char[ConstantsAndEnums.RECORD_LENGTH];
|
||||
|
||||
private List<IISFSensorRecord> _records = new List<IISFSensorRecord>();
|
||||
public IISFSensorRecord[] Records => _records.ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// adds a record, updates record count
|
||||
/// </summary>
|
||||
/// <param name="record"></param>
|
||||
public void AddRecord(IISFSensorRecord record)
|
||||
{
|
||||
record.SetDataChannelNumber(Convert.ToInt16(1 + _records.Count));
|
||||
_records.Add(record);
|
||||
SetNumberOfRecords(Records.Length * 4); //4 "records" per record
|
||||
}
|
||||
/// <summary>
|
||||
/// writes file to path
|
||||
/// </summary>
|
||||
/// <param name="pathToFile"></param>
|
||||
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(' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user