using System; using System.Collections.Generic; using System.Linq; using System.Text; using DTS.SensorDB.TSF; namespace DTS.SensorDB { /// /// helper class to read/write TSF files /// mostly taken from TDC code /// public class TSFFile { private const double MINIMUM_BRIDGE_RESISTANCE_OHMS = 100D; private const double MAXIMUM_BRIDGE_RESISTANCE_OHMS = 16000.0D; /// /// cribbed from tdc, the string to indicate an empty rack /// public const string EMPTY_RACK_SN = "NONE"; /// /// cribbed from TDC, crash barrier types /// public enum ICRASH_BARRIER_TYPE { IWALL_BARRIER = 0, ITROLLEY_BARRIER = 1 }; /// /// this is a replacement function for TDC functionality /// we don't really have this functionality in datapro yet, so we have /// a filler function for now /// /// public ICRASH_BARRIER_TYPE GetICrashBarrierType() { return ICRASH_BARRIER_TYPE.IWALL_BARRIER; } /// /// set progress, value should be 0-1 (so multiply by 100 to get percent) /// /// public delegate void RegisterProgressDelgate(double value); private string _softwareVersion; public string SoftwareVersion { get { return _softwareVersion; } set { _softwareVersion = value; } } private double _versionNumber; public double VersionNumber { get { return _versionNumber; } set { _versionNumber = value; } } private string _tsfName; public string TSFName { get { return _tsfName; } set { _tsfName = value; } } private string _sysDate; public string SysDate { get { return _sysDate; } set { _sysDate = value; } } private string _sysTime; public string SysTime { get { return _sysTime; } set { _sysTime = value; } } private string _testDescription; public string TestDescription { get { return _testDescription; } set { _testDescription = value; } } public enum Tags { SoftwareVersion, VersionNumber, TSFName, SysDate, SysTime, TestDescription, SamplingInformationSection, RackInformationSection, ModuleInformationSection, SensorChannelSection, CalculatedChannelSection, TOMChannelSection, TCFSection } private TSFTCFSection _tcfSection = new TSFTCFSection(); public TSFTCFSection TCFSection { get { return _tcfSection; } set { _tcfSection = value; } } private TSFTOMChannelInformationSection _tomChannelSection = new TSFTOMChannelInformationSection(); public TSFTOMChannelInformationSection TOMChannelSection { get { return _tomChannelSection; } set { _tomChannelSection = value; } } private TSFCalculatedChannelInformationSection _calculatedChannelSection = new TSFCalculatedChannelInformationSection(); public TSFCalculatedChannelInformationSection CalculatedChannelSection { get { return _calculatedChannelSection; } set { _calculatedChannelSection = value; } } private TSFSensorChannelInformationSection _sensorChannelSection = new TSFSensorChannelInformationSection(); public TSFSensorChannelInformationSection SensorChannelSection { get { return _sensorChannelSection; } set { _sensorChannelSection = value; } } private TSFModuleInformationSection _moduleInformationSection = new TSFModuleInformationSection(); public TSFModuleInformationSection ModuleInformationSection { get { return _moduleInformationSection; } set { _moduleInformationSection = value; } } private TSFRackInformationSection _rackInformationSection = new TSFRackInformationSection(); public TSFRackInformationSection RackInformationSection { get { return _rackInformationSection; } set { _rackInformationSection = value; } } private TSFSamplingInformationSection _samplingSection; public TSFSamplingInformationSection SamplingInformationSection { get { return _samplingSection; } set { _samplingSection = value; } } private TSFDIMSection _dimSection = new TSFDIMSection(); public TSFDIMSection DIMSection { get { return _dimSection; } set { _dimSection = value; } } private TSFG5DigitalInputSection _g5DigitalInputSection = new TSFG5DigitalInputSection(); public TSFG5DigitalInputSection G5DigitalInputSection { get { return _g5DigitalInputSection; } set { _g5DigitalInputSection = value; } } /// /// read a TSF file, extract out all the different components /// /// /// /// /// /// /// public void ReadTSF(string filename, TDCINIFile ini, string tsfReadProtocol, string workingSIFDirectory, double defaultAveWindowStart, double defaultAveWindowStop, out List errors, RegisterProgressDelgate setProgress) { errors = new List(); var invariant = System.Globalization.CultureInfo.InvariantCulture; // CHECK IF FILE EXISTS if (!System.IO.File.Exists(filename)) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_FILE_NOT_FOUND)); return; } // TDC does this, it doesn't seem to be necessary for us // DETERMINE DIRECTORY FROM WHICH TSF OPENED //var fileInfo = new System.IO.FileInfo(filename); //string location = fileInfo.DirectoryName; // READ IN VALUES FROM TSF List lines = new List(); try { lines.AddRange(System.IO.File.ReadAllLines(filename)); } catch (System.Exception ex) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_FILE_IO_ERROR, 0, ex.Message)); return; } if (lines.Count < 5) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSFFILE_INCOMPLETE)); return; } SoftwareVersion = lines[0]; try { VersionNumber = double.Parse(SoftwareVersion.Substring(0, 3), invariant); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_INVALID_VERSION, 0, SoftwareVersion)); return; } //tdc sets some static variables for rack sizes based on the soft version, it doesn't seem to be necessary for our purposes? List temp = new List(); temp.AddRange(lines); lines.Clear(); foreach (string l in temp) { string s = l.TrimEnd(); s = s.Replace('\t', ','); lines.Add(s); } int currentLine = 1; TSFName = lines[currentLine++]; SysDate = lines[currentLine++]; SysTime = lines[currentLine++]; TestDescription = lines[currentLine++]; // CHECK VERSION, IF OLDER THEN 6.3 THEN BAIL if (VersionNumber < 6.3) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_OLD_FILE_VERSION, 0, VersionNumber.ToString())); return; } try { SamplingInformationSection = new TSFSamplingInformationSection(lines, ref currentLine); } catch (System.Exception) { errors.Add(new ReadTSFError(ReadTSFError.TSF_ERRORS.TSF_UNEXPECTED_EOF, currentLine)); return; } var systemDescription = ini.GetSystemDescription(); RackInformationSection.ReadFrom(lines, ref currentLine, ref errors, this, systemDescription); setProgress(currentLine / (double)lines.Count); ModuleInformationSection.ReadFrom(lines, ref currentLine, ref errors, systemDescription, RackInformationSection); setProgress(currentLine / (double)lines.Count); SensorChannelSection.ReadFrom(lines, ref currentLine, ref errors); CalculatedChannelSection.ReadFrom(lines, ref currentLine, ref errors); TOMChannelSection.ReadFrom(lines, ref currentLine, ref errors); TCFSection.ReadFrom(lines, ref currentLine, ref errors); DIMSection.ReadFrom(lines, ref currentLine, ref errors); G5DigitalInputSection.ReadFrom(lines, ref currentLine, ref errors); } } }