void fetchData(IProgress <string> message_progress, IProgress <List <Interfaces.IParameter> > data) { Task.Factory.StartNew(() => { int trycount = 0; IsFetching = true; if (imitator == null) { imitator = Environment.Imitator.Build(); } if (device == null) { device = SelectedDevice.Build(); } trycount = 0; bool SucessStep = false; if (!imitator.IsConnected) { while (!SucessStep && !CancelTesting.IsCancellationRequested && trycount <= maxtrycount) { message_progress?.Report("Попытка подключения к имитатору"); try { imitator.Connect(); message_progress?.Report("Имитатор подключен"); SucessStep = true; } catch (Exception) { message_progress?.Report("Ошибка подключения к имитатору"); System.Threading.Thread.Sleep(500); } trycount++; } } if (trycount <= maxtrycount) { trycount = 0; } SucessStep = false; while (!SucessStep && !CancelTesting.IsCancellationRequested && trycount <= maxtrycount) { message_progress?.Report("Установка параметров имитации"); try { imitator.Speed = 60; imitator.Direction = Direction.Income; imitator.Distance = 30; SucessStep = true; } catch (Exception) { message_progress?.Report("Ошибка установки параметров имитации"); System.Threading.Thread.Sleep(500); } trycount++; } if (trycount <= maxtrycount) { trycount = 0; } SucessStep = false; if (!device.IsConnected) { while (!SucessStep && !CancelTesting.IsCancellationRequested && trycount <= maxtrycount) { message_progress?.Report("Попытка подключения к устройству"); try { device.Connect(); message_progress?.Report("Устройство подключено"); SucessStep = true; } catch (Exception) { message_progress?.Report("Подключение к устройству не удалось"); System.Threading.Thread.Sleep(500); } trycount++; } } if (trycount <= maxtrycount) { trycount = 0; } SucessStep = false; while (!SucessStep && !CancelTesting.IsCancellationRequested && trycount <= maxtrycount) { message_progress?.Report("Включение имитации"); imitator.Enable(); System.Threading.Thread.Sleep(500); OnPropertyChanged(nameof(DeviceSerial)); OnPropertyChanged(nameof(ImitatorSerial)); message_progress?.Report("Получение данных с устройства"); try { var datat = device.GetData(message_progress); message_progress?.Report("Запись данных"); data.Report(datat.ToList()); SucessStep = true; } catch (Exception) { System.Threading.Thread.Sleep(500); } trycount++; } message_progress?.Report("Ожидание"); try { imitator.Disconnect(); } catch (Exception) { } try { device.Disconnect(); } catch (Exception) { } IsFetching = false; }, CancelTesting); }
public ExperimentModel(ViewModel.ExpirementAddMasterModel ModelFrom) { start_time = DateTime.Now; State = "Ожидание"; SelectedDevice = ModelFrom.selectedDeviceFactory; Environment = ModelFrom.selecteEnvironmentModel; switch (ModelFrom.TerminateCause) { case TimeExpWatch.ByTime: TerminateSpan = ModelFrom.TerminateDateTime - DateTime.Now; break; case TimeExpWatch.ByInterval: TerminateSpan = ModelFrom.TerminateSpan; break; } FetchSpan = ModelFrom.FetchSpan; Environment.IsBusy = true; imitator = Environment.Imitator.Build(); device = SelectedDevice.Build(); //ExpirementData.Clear(); FetchProgress.ProgressChanged += FetchProgress_ProgressChanged; DataProgress.ProgressChanged += DataProgress_ProgressChanged; FetchData = new ViewModel.RelayCommand(o => { CancelTestingSource = new CancellationTokenSource(); CancelTesting = CancelTestingSource.Token; fetchData(FetchProgress, DataProgress); }, o => true); updateTimer.Tick += UpdateTimer_Tick; updateTimer.Interval = TimeSpan.FromMilliseconds(400); lastMeasurment = DateTime.Now; updateTimer.Start(); ExpirementChartModel.Axes.Add(new OxyPlot.Axes.DateTimeAxis() { Font = "Consolas", IsAxisVisible = true, IsPanEnabled = true, FontSize = 10, Key = "Время", IsZoomEnabled = true, MajorGridlineThickness = 1, MajorGridlineStyle = LineStyle.Dot, MajorGridlineColor = OxyColors.LightGray, }); ExpirementChartModel.Axes.Add(new OxyPlot.Axes.LinearAxis() { Font = "Consolas", IsPanEnabled = false, MaximumPadding = 1, MinimumPadding = 1, Angle = -45, FontSize = 10, Key = "Значение", AbsoluteMaximum = 200, AbsoluteMinimum = -100, MajorStep = 50, IsZoomEnabled = false, MajorGridlineThickness = 1, MajorGridlineStyle = LineStyle.Dot, MajorGridlineColor = OxyColors.LightGray, }); }