void CloseMasterRequest() { Task.Factory.StartNew(() => { CancelTestingSource.Cancel(); CancelSamplingSource.Cancel(); try { imitator.Disable(); } catch (Exception) { } try { if (device is Interfaces.IFlowable) { (device as Interfaces.IFlowable).DisableFlow(); } } catch (Exception) { } try { imitator.Disconnect(); } catch (Exception) { } try { device.Disconnect(); } catch (Exception) { } device = null; imitator = null; }); }
void TryFetchTestData(IProgress <string> progress_reporter) { Task.Factory.StartNew(() => { IsTestFinish = false; int failcounter = 0; if (imitator == null) { imitator = selecteEnvironmentModel.Imitator.Build(); } if (device == null) { device = selectedDeviceFactory.Build(); } bool SucessStep = false; if (!imitator.IsConnected) { while (!SucessStep && !CancelTesting.IsCancellationRequested && failcounter < FailAvalibleCount + 5) { failcounter++; progress_reporter?.Report(String.Format("Попытка подключения к имитатору {0}", selecteEnvironmentModel.Imitator.DeviceInfo)); try { imitator.Connect(); progress_reporter?.Report(String.Format("Имитатор {0} {1} подключен", selecteEnvironmentModel.Imitator.DeviceInfo, imitator.Serial)); SucessStep = true; } catch (Exception) { progress_reporter?.Report("Подключение к имитатору не удалось"); } System.Threading.Thread.Sleep(500); } } SucessStep = false; while (!SucessStep && !CancelTesting.IsCancellationRequested && failcounter < FailAvalibleCount + 5) { failcounter++; progress_reporter?.Report("Установка параметров имитации"); try { imitator.Speed = random.Next(10, 100); imitator.Direction = Direction.Income; imitator.Distance = 10; progress_reporter?.Report(String.Format("Параметры:\n \tСкорость: {0} км/ч \n \tРасстояние: {1} м\n \tНаправление: {2}", imitator.Speed, imitator.Distance, imitator.Direction == Direction.Income ? "Встречное" : "Попутное")); SucessStep = true; } catch (Exception) { progress_reporter?.Report("Ошибка установки параметров имитации"); } System.Threading.Thread.Sleep(500); } SucessStep = false; if (!device.IsConnected) { while (!SucessStep && !CancelTesting.IsCancellationRequested && failcounter < FailAvalibleCount + 5) { failcounter++; progress_reporter?.Report(String.Format("Попытка подключения к устройству {0}", selectedDeviceFactory.DeviceInfo)); try { device.Connect(); progress_reporter?.Report(String.Format("Устройство {0} {1} подключено", selectedDeviceFactory.DeviceInfo, device.SerialNumber)); SucessStep = true; } catch (Exception) { progress_reporter?.Report("Подключение к устройству не удалось"); } System.Threading.Thread.Sleep(500); } } if (device is Interfaces.IFlowable) { SucessStep = false; if (!device.IsConnected) { while (!SucessStep && !CancelTesting.IsCancellationRequested && failcounter < FailAvalibleCount + 5) { try { (device as Interfaces.IFlowable).DisableFlow(); SucessStep = true; } catch (Exception) { } } } } SucessStep = false; while (!SucessStep && !CancelTesting.IsCancellationRequested && failcounter < FailAvalibleCount + 5) { failcounter++; progress_reporter?.Report("Включение имитации"); imitator.Enable(); System.Threading.Thread.Sleep(500); progress_reporter?.Report("Получение данных с устройства при включенной имитации"); try { var data = device.GetData(progress); progress_reporter?.Report(String.Format("Измеренная скорость: {0} км/ч", device.Speed)); if (IsWithin((int)device.Speed, (int)(imitator.Speed - 1), (int)(imitator.Speed + 1))) { progress_reporter?.Report("Значение скорости в рамках погрешности(±1 км/ч) при включенной имитации"); SucessStep = true; } else { progress_reporter?.Report("Значение скорости не в рамках погрешности(±1 км/ч) при включенной имитации"); } } catch (Exception) { } System.Threading.Thread.Sleep(500); } SucessStep = false; while (!SucessStep && !CancelTesting.IsCancellationRequested && failcounter < FailAvalibleCount + 5) { failcounter++; progress_reporter?.Report("Выключение имитации"); imitator.Disable(); System.Threading.Thread.Sleep(500); progress_reporter?.Report("Получение данных с устройства при выключенной имитации"); try { var data = device.GetData(progress); progress_reporter?.Report(String.Format("Измеренная скорость: {0} км/ч", device.Speed)); if (IsWithin((int)device.Speed, (int)(0 - 1), (int)(0 + 1))) ///TODO Хардкод - исправить на заполнение из конфигов { progress_reporter?.Report("Значение скорости в рамках погрешности(±1 км/ч) при выключенной имитации"); SucessStep = true; } else { progress_reporter?.Report("Значение скорости не в рамках погрешности(±1 км/ч) при выключенной имитации"); } } catch (Exception) { } System.Threading.Thread.Sleep(500); } if (CancelTesting.IsCancellationRequested) { progress_reporter?.Report("Операция прервана"); } if (!CancelTesting.IsCancellationRequested && failcounter < FailAvalibleCount + 5) { progress_reporter?.Report("Автоматический тест пройден"); TestComplete = true; } if (!CancelTesting.IsCancellationRequested && failcounter >= FailAvalibleCount + 5) { progress_reporter?.Report("Автоматический тест не пройден"); TestComplete = false; } IsTestFinish = true; try { if (device is Interfaces.IFlowable) { (device as Interfaces.IFlowable).DisableFlow(); } } catch (Exception) { } }, CancelTesting ); }