init
This commit is contained in:
@@ -0,0 +1,281 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using DataPROWin7.DataModel;
|
||||
using DTS.Common.Classes;
|
||||
using DTS.Common.Enums.DASFactory;
|
||||
using DTS.Common.Import.Enums;
|
||||
using DTS.Common.Import.Factories;
|
||||
using DTS.Common.Import.ImportOptions;
|
||||
using DTS.Common.Import.Parsers;
|
||||
using DTS.Common.Interface.Groups.GroupList;
|
||||
using DTS.Common.SharedResource.Strings;
|
||||
using DTS.Common.Storage;
|
||||
using DTS.Common.Utils;
|
||||
using DTS.SensorDB;
|
||||
using Microsoft.VisualBasic.FileIO;
|
||||
|
||||
namespace DTS.Common.Import
|
||||
{
|
||||
public class DTSCSVTestSetupParser : ParseVariantBase
|
||||
{
|
||||
private readonly IImportNotification _importNotification;
|
||||
private readonly CsvImportOptions _csvImportOptions;
|
||||
private readonly TestSetupImportData _defaultsTestSetupImportData;
|
||||
private readonly IGroupImport _groupImport;
|
||||
//FB 36905
|
||||
private readonly bool _createDynamicGroups;
|
||||
public DTSCSVTestSetupParser(IImportNotification importNotification, CsvImportOptions csvImportOptions, TestSetupImportData testSetupImportData, IGroupImport groupImport, bool createDynamicGroups)
|
||||
{
|
||||
_csvImportOptions = csvImportOptions;
|
||||
_defaultsTestSetupImportData = testSetupImportData;
|
||||
_importNotification = importNotification;
|
||||
_groupImport = groupImport;
|
||||
_createDynamicGroups = createDynamicGroups;
|
||||
}
|
||||
|
||||
public static TestTemplate CreateTestSetup(TestSetupImportData tsid, DASHardware[] allDAS)
|
||||
{
|
||||
var t = new TestTemplate
|
||||
{
|
||||
Name = tsid.Name,
|
||||
Description = tsid.Description,
|
||||
SamplesPerSecondAggregate = tsid.SamplesPerSecond,
|
||||
PostTriggerSeconds = tsid.PosttriggerSeconds,
|
||||
PreTriggerSeconds = tsid.PretriggerSeconds,
|
||||
RecordingMode = tsid.RecordingMode,
|
||||
CalibrationBehavior = tsid.CalibrationBehavior
|
||||
};
|
||||
|
||||
AddHardwareToTestSetup(t, tsid, allDAS);
|
||||
|
||||
SetClockSyncs(t, tsid);
|
||||
if (RecordingModeExtensions.IsAStreamMode(tsid.RecordingMode))
|
||||
{
|
||||
t.DoStreaming = true;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
private static void SetClockSyncs(TestTemplate t, TestSetupImportData tsid)
|
||||
{
|
||||
if (tsid.ManageClocksOutsideOfDataPROMaster)
|
||||
{
|
||||
t.ClockSyncProfileMaster = ClockSyncProfile.Manual;
|
||||
}
|
||||
if (tsid.ManageClocksOutsideOfDataPROSlave)
|
||||
{
|
||||
t.ClockSyncProfileSlave = ClockSyncProfile.Manual;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void AddHardwareToTestSetup(TestTemplate t, TestSetupImportData tsid, DASHardware[] allDAS)
|
||||
{
|
||||
if (null != tsid.SampleRateForDAS && 0 != tsid.SampleRateForDAS.Count)
|
||||
{
|
||||
using (var e = tsid.SampleRateForDAS.GetEnumerator())
|
||||
{
|
||||
while (e.MoveNext())
|
||||
{
|
||||
var das = Array.Find(allDAS, d => d.SerialNumber.Equals(e.Current.Key));
|
||||
if (null == das) { continue; }
|
||||
t.AddHardware(das.DASId);
|
||||
if (tsid.IsClockMaster.ContainsKey(das.SerialNumber))
|
||||
{
|
||||
t.DASClockMasterList[das.SerialNumber] = tsid.IsClockMaster[das.SerialNumber];
|
||||
}
|
||||
if (tsid.DomainIdForDAS.ContainsKey(das.SerialNumber))
|
||||
{
|
||||
t.DASPTPDomainIDList[das.SerialNumber] = Convert.ToByte(t.DASPTPDomainIDList[das.SerialNumber]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void UpdateDASSampleRate(TestTemplate t, TestSetupImportData tsid, DASHardware[] allDAS)
|
||||
{
|
||||
if (null != tsid.SampleRateForDAS && 0 != tsid.SampleRateForDAS.Count)
|
||||
{
|
||||
using (var e = tsid.SampleRateForDAS.GetEnumerator())
|
||||
{
|
||||
while (e.MoveNext())
|
||||
{
|
||||
var das = Array.Find(allDAS, d => d.SerialNumber.Equals(e.Current.Key));
|
||||
if (null == das) { continue; }
|
||||
//why are there two places for this information?
|
||||
t.SetSampleRateForHardware(das, Convert.ToDouble(e.Current.Value));
|
||||
t.DASSampleRateList[das.SerialNumber] = Convert.ToDouble(e.Current.Value);
|
||||
}
|
||||
}
|
||||
if (tsid.SampleRateForDAS.Distinct().Count() > 1)
|
||||
{
|
||||
t.CommonStatusLine = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Parse(ref ImportObject importObject)
|
||||
{
|
||||
if (string.IsNullOrEmpty(FileName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (importObject == null)
|
||||
{
|
||||
throw new ArgumentNullException("importObject", "importObject can't be null or empty");
|
||||
}
|
||||
//FB 40598 Check if the file is supported for test setup import
|
||||
|
||||
if (!CSVFile.IsCSVFileForTestSetupImport(FileName, _csvImportOptions))
|
||||
{
|
||||
importObject.AddError(new ImportError { Message = StringResources.Import_CSVFileNotForTestSetup, ContinueImportOnError = false, Severity = ImportSeverityError.Critical });
|
||||
return;
|
||||
}
|
||||
|
||||
var tsid = ParseTestSetup(FileName);
|
||||
|
||||
if (tsid == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (importObject.TestSetups().Any(p => p.Name == tsid.Name))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var allDAS = DataPROWin7.DataModel.Classes.Hardware.DASHardwareList.GetAllHardware();
|
||||
var t = CreateTestSetup(tsid, allDAS);
|
||||
//FB 36879 Add the hardware list for the test setup
|
||||
if (t != null)
|
||||
{
|
||||
importObject.AddHardwareList(t.GetHardware());
|
||||
}
|
||||
|
||||
if (tsid.Tags != null)
|
||||
{
|
||||
t?.SetTags(tsid.Tags.ToArray(), DbOperations.GetSQLCommand, DbOperations.TagsGet,
|
||||
DbOperations.TagsGetId, DbOperations.TagsInsert);
|
||||
}
|
||||
|
||||
//FB Assign the parse parameters
|
||||
_groupImport.ParseParameters = importObject.ParseParameters();
|
||||
|
||||
AssignCalibrations(ref importObject);
|
||||
|
||||
var res = AssignGroupsToTestSetup(t, importObject.SensorGroupNamesLookup(), null, importObject.StaticGroups().ToList(),
|
||||
importObject.Sensors().ToList(), _groupImport, _importNotification, tsid.Version);
|
||||
|
||||
var staticGroups = res?.Item2;
|
||||
var sensors = res?.Item3;
|
||||
var testSetup = res?.Item1;
|
||||
|
||||
if (staticGroups != null)
|
||||
{
|
||||
importObject.AddStaticGroups(staticGroups);
|
||||
}
|
||||
|
||||
var cleanedSensors = GroupHelper.CleanUneededSensorDataPlaceHolder(importObject.CalibrationsLookup(), sensors);
|
||||
if (cleanedSensors != null)
|
||||
{
|
||||
importObject.ClearSensors();
|
||||
|
||||
importObject.AddSensors(cleanedSensors);
|
||||
}
|
||||
|
||||
UpdateDASSampleRate(t, tsid, allDAS);
|
||||
|
||||
if (testSetup != null)
|
||||
{
|
||||
importObject.AddTestSetup(testSetup);
|
||||
//FB 38039, 40758 Identify type of import by number of test setups
|
||||
importObject.TestSetupImportFileFormat = importObject.GetImportFileFormat();
|
||||
}
|
||||
|
||||
importObject.SourceFormat = ImportFormats.DTS_CSV;
|
||||
}
|
||||
/// <summary>
|
||||
/// set the calibration for the sensor based on the most recent in the import
|
||||
/// this is necessary as it was coming in with the latest calibration from the database (which
|
||||
/// will be empty in a brand new import)
|
||||
/// http://manuscript.dts.local/f/cases/43722/When-Importing-Csv-test-setup-software-zero-method-in-test-setup-parameters-does-not-match-import
|
||||
/// </summary>
|
||||
private void AssignCalibrations(ref ImportObject importObject)
|
||||
{
|
||||
if (null == importObject) { return; }
|
||||
if (null == importObject.Sensors() || !importObject.Sensors().Any()) { return; }
|
||||
if (null == importObject.Calibrations() || !importObject.Calibrations().Any()) { return; }
|
||||
|
||||
foreach( var sensor in importObject.Sensors())
|
||||
{
|
||||
var cals = importObject.CalibrationLookup(sensor.SerialNumber);
|
||||
if (null == cals || 0 == cals.Count) { return; }
|
||||
cals.Sort();
|
||||
sensor.Calibration = cals[cals.Count - 1];
|
||||
}
|
||||
|
||||
}
|
||||
//FB 36879 Single Responsibility principle , moved the method to this class
|
||||
private Tuple<TestTemplate, List<IGroup>, List<SensorData>> AssignGroupsToTestSetup(TestTemplate testTemplate, Dictionary<string, string> sensorGroupNameLookup, Dictionary<string, List<string>> groupNameSensorListLookup,
|
||||
List<IGroup> existingStaticGroups, List<SensorData> sensors, IGroupImport groupImport, IImportNotification importNotification, int csvVersion)
|
||||
{
|
||||
try
|
||||
{
|
||||
_ = GroupHelper.ReverseChannelOrder(testTemplate, sensorGroupNameLookup, sensors);
|
||||
|
||||
var groupSensorLookup = groupImport.GetGroupSensorLookup(sensors, sensorGroupNameLookup, groupNameSensorListLookup);
|
||||
|
||||
sensors = GroupHelper.NormalizeSensorIds(sensors);
|
||||
//FB 36905
|
||||
var testAndGroups = groupImport.CreateGroups(sensors, groupSensorLookup, testTemplate, _createDynamicGroups, existingStaticGroups, importNotification.SetProgress);
|
||||
|
||||
if (testAndGroups == null)
|
||||
{
|
||||
TestTemplate noTestSetup = null;
|
||||
List<IGroup> noGroup = null;
|
||||
return Tuple.Create(noTestSetup, noGroup, sensors);
|
||||
}
|
||||
return Tuple.Create(testAndGroups.Item1, testAndGroups.Item2, sensors);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
importNotification.ReportErrors(new List<string> { ex.Message });
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private TestSetupImportData ParseTestSetup(string filename)
|
||||
{
|
||||
var tsid = new TestSetupImportData()
|
||||
{
|
||||
SamplesPerSecond = _defaultsTestSetupImportData.SamplesPerSecond,
|
||||
Tags = new List<string>(),
|
||||
Version = 0,
|
||||
RecordingMode = _defaultsTestSetupImportData.RecordingMode,
|
||||
PretriggerSeconds = _defaultsTestSetupImportData.PretriggerSeconds,
|
||||
PosttriggerSeconds = _defaultsTestSetupImportData.PosttriggerSeconds,
|
||||
CalibrationBehavior = _defaultsTestSetupImportData.CalibrationBehavior
|
||||
};
|
||||
using (var parser = new TextFieldParser(filename, Encoding.GetEncoding(_csvImportOptions.Encoding), true))
|
||||
{
|
||||
parser.TextFieldType = FieldType.Delimited;
|
||||
parser.SetDelimiters(_csvImportOptions.FieldSeparator);
|
||||
var parsers = CSVTestParserFactory.CreateCSVParsers();
|
||||
|
||||
parsers[0].ParseVersion(parser, tsid);
|
||||
|
||||
if (tsid.Version == 6)
|
||||
{
|
||||
for (var i = 1; i < parsers.Length; i++)
|
||||
{
|
||||
parsers[i].ParseVersion(parser, tsid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tsid;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user