417 lines
19 KiB
Plaintext
417 lines
19 KiB
Plaintext
|
|
using DTS.Common.Classes.Sensors;
|
||
|
|
using DTS.Common.Enums;
|
||
|
|
using DTS.Common.Enums.Sensors;
|
||
|
|
using DTS.Common.Import.Interfaces;
|
||
|
|
using DTS.Common.SharedResource.Strings;
|
||
|
|
using DTS.SensorDB;
|
||
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Globalization;
|
||
|
|
using System.Linq;
|
||
|
|
using static DTS.Common.Enums.Sensors.SensorConstants;
|
||
|
|
|
||
|
|
namespace DTS.Common.Import.Parsers.CSV
|
||
|
|
{
|
||
|
|
public class Version2CSVSensorParser : AbstractCSVParser
|
||
|
|
{
|
||
|
|
public override int Version => 2;
|
||
|
|
public override void ParseVersion(CSVImportTags.Tags field, string sVal, ParseParameters pp)
|
||
|
|
{
|
||
|
|
var sd = (SensorData)pp.SensorData;
|
||
|
|
var sc = (SensorCalibration)pp.SensorCal;
|
||
|
|
#region and another very long switch statement
|
||
|
|
switch (field)
|
||
|
|
{
|
||
|
|
case CSVImportTags.Tags.AdditionalInitialOffsets:
|
||
|
|
{
|
||
|
|
if (!string.IsNullOrWhiteSpace(sVal))
|
||
|
|
{
|
||
|
|
var additionalInitialOffsets = new InitialOffsets(sVal); //merge with original (initial) initial offset
|
||
|
|
var numAdditionalInitialOffsets = additionalInitialOffsets.Offsets.Length;
|
||
|
|
var mergedInitialOffsets = new InitialOffset[numAdditionalInitialOffsets + 1];
|
||
|
|
mergedInitialOffsets[0] = sc.InitialOffsets.Offsets[0];
|
||
|
|
var counter = 0;
|
||
|
|
foreach (var additionalInitialOffset in additionalInitialOffsets.Offsets)
|
||
|
|
{
|
||
|
|
counter++;
|
||
|
|
mergedInitialOffsets[counter] = additionalInitialOffset;
|
||
|
|
}
|
||
|
|
sc.InitialOffsets.Offsets = mergedInitialOffsets;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.AdditionalLinearSensitivity:
|
||
|
|
if (!string.IsNullOrWhiteSpace(sVal))
|
||
|
|
{
|
||
|
|
//This is a non-linear sensor with additional linear attributes
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var d))
|
||
|
|
{
|
||
|
|
//13810 Sensor ID and sensitivity are degraded to scientific notation when editing
|
||
|
|
if (sVal.ToLower().Contains("e"))
|
||
|
|
{
|
||
|
|
pp.Errors.Add(string.Format(StringResources.InvalidSensitivity,
|
||
|
|
sd.SerialNumber, sVal));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
pp.Sensitivity = d;
|
||
|
|
}
|
||
|
|
else { pp.Errors.Add(string.Format(StringResources.InvalidSensitivity, sd.SerialNumber, sVal)); }
|
||
|
|
|
||
|
|
//Add a linear calibration record if it doesn't already exist
|
||
|
|
sc = _calibrationImport.AddLinearCalRecordIfNeeded(sc, pp.SavedIsProportional, pp.SavedRemoveOffset);
|
||
|
|
|
||
|
|
sc.Records.Records[1].Sensitivity = pp.Sensitivity;
|
||
|
|
if (sc.IsProportional) { sc.Records.Records[1].Excitation = sd.SupportedExcitation.First(); }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.AdditionalLinearZeroMethod:
|
||
|
|
if (!string.IsNullOrWhiteSpace(sVal))
|
||
|
|
{
|
||
|
|
{
|
||
|
|
//Add a linear ZeroMethods record if it doesn't already exist
|
||
|
|
sc = _calibrationImport.AddLinearZeroMethodIfNeeded(sc, _zeroMethodOptions.ZeroMethodType, _zeroMethodOptions.ZeroMethodStart, _zeroMethodOptions.ZeroMethodEnd);
|
||
|
|
|
||
|
|
if (Enum.TryParse(sVal, out ZeroMethodType zmt))
|
||
|
|
{
|
||
|
|
sc.ZeroMethods.Methods.Last().Method = zmt;
|
||
|
|
pp.ZeroType = zmt;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.AdditionalLinearZeroMethodEnd:
|
||
|
|
if (!string.IsNullOrWhiteSpace(sVal))
|
||
|
|
{
|
||
|
|
{
|
||
|
|
//Add a linear ZeroMethods record if it doesn't already exist
|
||
|
|
sc = _calibrationImport.AddLinearZeroMethodIfNeeded(sc, _zeroMethodOptions.ZeroMethodType, _zeroMethodOptions.ZeroMethodStart, _zeroMethodOptions.ZeroMethodEnd);
|
||
|
|
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var zeroMethodEnd))
|
||
|
|
{
|
||
|
|
sc.ZeroMethods.Methods.Last().End = zeroMethodEnd;
|
||
|
|
pp.ZeroEnd = zeroMethodEnd;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.AdditionalLinearZeroMethodStart:
|
||
|
|
if (!string.IsNullOrWhiteSpace(sVal))
|
||
|
|
{
|
||
|
|
{
|
||
|
|
//Add a linear ZeroMethods record if it doesn't already exist
|
||
|
|
sc = _calibrationImport.AddLinearZeroMethodIfNeeded(sc, _zeroMethodOptions.ZeroMethodType, _zeroMethodOptions.ZeroMethodStart, _zeroMethodOptions.ZeroMethodEnd);
|
||
|
|
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var zeroMethodStart))
|
||
|
|
{
|
||
|
|
sc.ZeroMethods.Methods.Last().Start = zeroMethodStart;
|
||
|
|
pp.ZeroStart = zeroMethodStart;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.AxisNumber:
|
||
|
|
if (short.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var axisNumber))
|
||
|
|
{
|
||
|
|
sd.AxisNumber = axisNumber;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.BridgeLegMode:
|
||
|
|
BridgeLeg bl;
|
||
|
|
if (Enum.TryParse(sVal, out bl)) { sd.BridgeLegMode = bl; }
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.BridgeType:
|
||
|
|
if (Enum.TryParse(sVal, out BridgeType bt))
|
||
|
|
{
|
||
|
|
sd.Bridge = bt;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
//FB 41817 if not parsable but contains these values use them
|
||
|
|
if (sVal.ToLower() == "squib setting")
|
||
|
|
{
|
||
|
|
sd.Bridge = BridgeType.SQUIB;
|
||
|
|
}
|
||
|
|
else if (sVal.ToLower() == "digital input setting")
|
||
|
|
{
|
||
|
|
sd.Bridge = BridgeType.DigitalInput;
|
||
|
|
}
|
||
|
|
else if (sVal.ToLower() == "digital output setting")
|
||
|
|
{
|
||
|
|
sd.Bridge = BridgeType.TOMDigital;
|
||
|
|
}
|
||
|
|
else if (sVal.ToLower() == "full bridge")
|
||
|
|
{
|
||
|
|
sd.Bridge = BridgeType.FullBridge;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.CalInterval:
|
||
|
|
{
|
||
|
|
if (int.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var temp)) { sd.CalInterval = temp; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.CouplingMode:
|
||
|
|
{
|
||
|
|
if (Enum.TryParse(sVal, out CouplingModes cm)) { sd.CouplingMode = cm; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.Created:
|
||
|
|
{
|
||
|
|
if (DateTime.TryParse(sVal, pp.ImportCulture, DateTimeStyles.AssumeLocal, out var dt)) { sd.Created = dt; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.DelayMS:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, out var d)) { sd.DelayMS = d; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.DigitalOutputDelayMS:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, out var d)) { sd.DigitalOutputDelayMS = d; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.DigitalInputMode:
|
||
|
|
{
|
||
|
|
if (Enum.TryParse(sVal, out DigitalInputModes im)) { sd.InputMode = im; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.DigitalOutputMode:
|
||
|
|
{
|
||
|
|
if (Enum.TryParse(sVal, out DigitalOutputModes om)) { sd.DigitalOutputMode = om; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.DigitalScaleMultiplier:
|
||
|
|
if (!string.IsNullOrEmpty(sVal))
|
||
|
|
{
|
||
|
|
sd.ScaleMultiplier.FromDbSerializeString(sVal);
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.Direction:
|
||
|
|
{
|
||
|
|
if (sd.Direction == "?" && sd.Direction != sVal)
|
||
|
|
{
|
||
|
|
sd.Direction = sVal;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.DisplayUnits:
|
||
|
|
{
|
||
|
|
//Determine if we know about this display unit
|
||
|
|
var calEU = MeasurementUnitList.GetMeasurementUnit(sc.Records.Records.First().EngineeringUnits);
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var unitConversion = calEU.GetScalerConversion(sVal);
|
||
|
|
|
||
|
|
// Convert all the EU values to be represented in DisplayUnits
|
||
|
|
sd.RangeHigh *= unitConversion;
|
||
|
|
sd.RangeMedium *= unitConversion;
|
||
|
|
sd.RangeLow *= unitConversion;
|
||
|
|
sd.Capacity *= unitConversion;
|
||
|
|
sc.InitialOffsets.Offsets.First().EU *= unitConversion;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
if (ex is InvalidCastException)
|
||
|
|
{
|
||
|
|
sd.DisplayUnit = sc.Records.Records.First().EngineeringUnits;
|
||
|
|
// The units don't match what we are aware of. Lets move on.
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
_importNotification.ReportErrors(new List<string>() { ex.Message });
|
||
|
|
}
|
||
|
|
|
||
|
|
sd.DisplayUnit = sVal; break;
|
||
|
|
}
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.DurationMS:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var d)) { sd.DurationMS = d; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.DigitalOutputDurationMS:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var d)) { sd.DigitalOutputDurationMS = d; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.FiveVoltExcSensitivity:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var d))
|
||
|
|
{
|
||
|
|
pp.SensorCal = _calibrationImport.CheckForExcitationCalibration(sc, d, ExcitationVoltageOptions.ExcitationVoltageOption.Volt5, sd.DisplayUnit);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.InitialOffset: sc.InitialOffsets = new InitialOffsets(sVal); break;
|
||
|
|
case CSVImportTags.Tags.LimitDuration: sd.LimitDuration = sVal.ToLower() == "yes"; break;
|
||
|
|
case CSVImportTags.Tags.NonLinear: sc.NonLinear = sVal.ToLower() == "yes"; break;
|
||
|
|
case CSVImportTags.Tags.NonLinearCalibration:
|
||
|
|
{
|
||
|
|
sc.Records.Records[0].Poly.FromSerializeString(sVal);
|
||
|
|
if (sc.Records.Records[0].Poly.IsValid()) { sc.NonLinear = true; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.NumberOfAxes:
|
||
|
|
{
|
||
|
|
if (short.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var temp)) { sd.NumberOfAxes = temp; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.PhysicalDimension:
|
||
|
|
{
|
||
|
|
//if the physical dimension was already set by the ISOCode, don't reset it
|
||
|
|
if (sd.PhysicalDimension == "??" && sd.PhysicalDimension != sVal)
|
||
|
|
{
|
||
|
|
sd.PhysicalDimension = sVal;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.Polarity: sd.Polarity = sVal; break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.RangeHigh:
|
||
|
|
{
|
||
|
|
pp.ImportContainedSensorRanges = true;
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var d)) { sd.RangeHigh = d; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.RangeLow:
|
||
|
|
{
|
||
|
|
pp.ImportContainedSensorRanges = true;
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var d)) { sd.RangeLow = d; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.RangeMedium:
|
||
|
|
{
|
||
|
|
pp.ImportContainedSensorRanges = true;
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var d)) { sd.RangeMedium = d; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.SquibFireMode:
|
||
|
|
{
|
||
|
|
if (Enum.TryParse(sVal, out SquibFireMode sfm)) { sd.SquibFireMode = sfm; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.SquibMeasurementType:
|
||
|
|
{
|
||
|
|
if (Enum.TryParse(sVal, out SquibMeasurementType smt)) { sd.SquibMeasurementType = smt; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.SquibOutputCurrent:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var d)) { sd.SquibOutputCurrent = d; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.SupportedExcitation: sd.SetSupportedExcitationFromString(sVal); break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.TenVoltExcSensitivity:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var d))
|
||
|
|
{
|
||
|
|
pp.SensorCal = _calibrationImport.CheckForExcitationCalibration(sc, d, ExcitationVoltageOptions.ExcitationVoltageOption.Volt10, sd.DisplayUnit);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.TimesUsed:
|
||
|
|
{
|
||
|
|
if (int.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var temp)) { sd.TimesUsed = temp; }
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.TwoVoltExcSensitivity:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var d))
|
||
|
|
{
|
||
|
|
pp.SensorCal = _calibrationImport.CheckForExcitationCalibration(sc, d, ExcitationVoltageOptions.ExcitationVoltageOption.Volt2, sd.DisplayUnit);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.Unipolar: sd.UniPolar = sVal.ToLower() == "yes"; break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.Unknown: break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.ZeroMethod:
|
||
|
|
{
|
||
|
|
if (Enum.TryParse(sVal, out ZeroMethodType zmt))
|
||
|
|
{
|
||
|
|
sc.ZeroMethods.Methods.First().Method = zmt;
|
||
|
|
pp.ZeroType = zmt;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.ZeroMethodEnd:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var zeroMethodEnd))
|
||
|
|
{
|
||
|
|
sc.ZeroMethods.Methods.First().End = zeroMethodEnd;
|
||
|
|
pp.ZeroEnd = zeroMethodEnd;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.ZeroMethodStart:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var zeroMethodStart))
|
||
|
|
{
|
||
|
|
sc.ZeroMethods.Methods.First().Start = zeroMethodStart;
|
||
|
|
pp.ZeroStart = zeroMethodStart;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.AtCapacity:
|
||
|
|
{
|
||
|
|
sc.Records.Records.First().AtCapacity = sVal.ToLower() == "yes";
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.CapacityOutputIsBasedOn:
|
||
|
|
{
|
||
|
|
if (double.TryParse(sVal, NumberStyles.Any, pp.ImportCulture, out var temp))
|
||
|
|
{
|
||
|
|
sc.Records.Records.First().CapacityOutputIsBasedOn = temp;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case CSVImportTags.Tags.SensitivityUnits:
|
||
|
|
{
|
||
|
|
if (!string.IsNullOrEmpty(sVal))
|
||
|
|
{
|
||
|
|
sc.Records.Records.First().SensitivityUnits = SensUnitStringConverter.ConvertFromString(sVal);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
|
||
|
|
case CSVImportTags.Tags.CheckOffset: { sd.CheckOffset = sVal.ToLower() == "yes"; } break;
|
||
|
|
case CSVImportTags.Tags.Broken: { sd.Broken = sVal.ToLower() == "yes"; } break;
|
||
|
|
case CSVImportTags.Tags.DoNotUse: { sd.DoNotUse = sVal.ToLower() == "yes"; } break;
|
||
|
|
case CSVImportTags.Tags.ISOChannelName: { sd.ISOChannelName = sVal; } break;
|
||
|
|
case CSVImportTags.Tags.UserCode: { sd.UserCode = sVal; } break;
|
||
|
|
case CSVImportTags.Tags.UserChannelName: { sd.UserChannelName = sVal; } break;
|
||
|
|
default: throw new NotSupportedException("Unknown field: " + field);
|
||
|
|
}
|
||
|
|
#endregion and another very long switch statement
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|