using DataPROWin7.DataModel; using DataPROWin7.DataModel.Classes.Hardware; using DTS.Common.Import.Enums; using DTS.SensorDB; using DTS.Slice.Users; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Net.Mime.MediaTypeNames; namespace DTS.Common.Import.Persist { public class SaveTestSetup : SaveVariantBase { private readonly SaveCustomChannels _saveCustomChannels; private readonly SaveHardware _saveHardware; private readonly SaveGroups _saveGroups; public User CurrentUser { get; set; } //FB 38039 Get/ Set setup name used in run test public string TestSetupName { get; set; } public SaveTestSetup(ImportObject importObject, IPersistCalculator persistCalculator, IImportNotification importNotification, SaveCustomChannels saveCustomChannels, SaveHardware saveHardware, SaveGroups saveGroups, Func isCancelled = null) : base(importObject, persistCalculator, importNotification, isCancelled) { _saveCustomChannels = saveCustomChannels; _saveHardware = saveHardware; _saveGroups = saveGroups; } //FB 38039 Default used for run test private void ConfigureDefaultRunTestTestSetup(TestTemplate TestSetup) { TestSetup.DoROIDownload = true; TestSetup.ViewROIDownload = true; TestSetup.DownloadAll = true; TestSetup.ViewDownloadAll = true; TestSetup.RequireUserConfirmationOnErrors = true; //FB 43757 disable upload since eqx for GM does not use upload any more TestSetup.UploadData = false; TestSetup.ViewDiagnostics = true; TestSetup.VerifyChannels = true; TestSetup.ViewExport = true; TestSetup.DefaultNumberRealtimeGraphs = 1; TestSetup.ExportFormats = DTS.Common.Enums.SupportedExportFormatBitFlags.rdfadc; TestSetup.AllowMissingSensors = false; TestSetup.StrictDiagnostics = true; TestSetup.AutomaticProgression = true; TestSetup.DoAutoArm = false; TestSetup.DoEnableRepeat = false; TestSetup.DoStreaming = false; TestSetup.AllowSensorIdToBlankChannel = false; TestSetup.AllowMissingSensors = false; TestSetup.SuppressMissingSensorsWarning = false; TestSetup.CheckListRequirePass = true; } public override void Save() { var testSetups = _importObject.TestSetups(); if (_importObject.TestSetupImportFileFormat == ImportFileFormat.SingleTestSetup && !string.IsNullOrEmpty(TestSetupName)) { //FB 38039 this test is used for run test var testSetup = testSetups.FirstOrDefault(); if (testSetup == null) { return; } testSetup.Name = TestSetupName; testSetup.TestId = TestSetupName; ConfigureDefaultRunTestTestSetup(testSetup); } Dictionary oldIdToNewIdLookup = new Dictionary(); SaveTestSetupHelper.UpdateLevelTriggers(ref oldIdToNewIdLookup, _saveCustomChannels, testSetups); SaveTestSetupHelper.DeleteExistingTestSetups(testSetups, CurrentUser.UserName); Dictionary hardwareLookup = SaveTestSetupHelper.PopulateHarwareLookup(); _importNotification.SetStatus.Invoke(new ImportStatus { ExtraStatus = ImportExtraStatus.ReadingTestSetups, PossibleStatus = PossibleStatus.Importing }); foreach (var t in testSetups) { SaveTestSetupHelper.FixDasAff(hardwareLookup, t); //keep track of test id prior to commit, so that we can correct das.TestId //if we need to var idPriorToCommit = t.Id; t.Id = 0; var allSensorsInDb = SensorsCollection.SensorsList.GetAllSensors(true); foreach (var channels in t.ChannelsForGroup) { foreach (var ch in channels.Value) { if (_importObject.OldSensorDatabaseIdsToNew().ContainsKey(ch.SensorId)) { ch.SensorId = _importObject.OldSensorDatabaseIdToNew(ch.SensorId); var match = allSensorsInDb?.FirstOrDefault(s => s.DatabaseId == ch.SensorId); if (null != match) { ch.SetSensorData(match, null, false); } } if (_saveHardware.OldDASIdToNewDASId.ContainsKey(ch.DASId)) { ch.DASId = _saveHardware.OldDASIdToNewDASId[ch.DASId]; } //15262 Absolute zero sensors in test setup change to ave over time after import. //zero method may be undefined on older imports //make sure we set the zero method before embedded group is committed ... SaveSensor.FixMissingZeroMethodParameter(ch, _saveGroups.DefaultZeroMethod, _saveGroups.DefaultZeroStart, _saveGroups.DefaultZeroEnd); SaveSensor.FixMissingInitialOffset(ch, _saveGroups.DefaultInitialOffset); } } var hardwareIncluded = t.AddedHardware.ToList(); var hardwareRemoved = t.RemovedHardware.ToList(); var listToProcess = new List>(); foreach (var e in _saveHardware.OldDASIdToNewDASId) { listToProcess.Add(new Tuple(e.Key, e.Value)); } //arrange the to process by the largest new id listToProcess.Sort((a, b) => { if (a == b) { return 0; } return b.Item2.CompareTo(a.Item2); }); foreach (var tuple in listToProcess) { if (tuple.Item1 == tuple.Item2) { continue; } if (hardwareIncluded.Contains(tuple.Item2)) { throw new Exception("Panic, new id already in list"); } if (hardwareRemoved.Contains(tuple.Item2)) { throw new Exception("Panic, new id already in list"); } if (hardwareIncluded.Contains(tuple.Item1)) { hardwareIncluded.Remove(tuple.Item1); hardwareIncluded.Add(tuple.Item2); } if (hardwareRemoved.Contains(tuple.Item1)) { hardwareRemoved.Remove(tuple.Item1); hardwareRemoved.Add(tuple.Item2); } } SaveTestSetupHelper.AddHardwareFromEmbeddedGroups(t, _saveGroups, hardwareRemoved, hardwareIncluded); t.ReplaceCalculatedChannel(oldIdToNewIdLookup); if (IsCancelled()) { return; } if (t.TestSetupUniqueId == null) { t.TestSetupUniqueId = Guid.NewGuid().ToString(); } TestTemplateList.TestTemplatesList.Commit(t, false); if (idPriorToCommit > 0 && _saveHardware.TestIdToHardware.ContainsKey(idPriorToCommit)) { foreach (var enumH in _saveHardware.TestIdToHardware[idPriorToCommit]) { enumH.TestId = t.Id; enumH.Update(); } } _persistCalculator.AddDone(); _importNotification.SetProgress(_persistCalculator.ProgressValue); } oldIdToNewIdLookup.Clear(); if (_importObject.Errors().Any()) { //report errors } //FB 36879 Still parsing it should report as Working status _importNotification.SetStatus.Invoke(new ImportStatus { ExtraStatus = ImportExtraStatus.None, PossibleStatus = PossibleStatus.Working }); } } }