public EditHistoryViewModel(SensorDBHistory history) { _history = history; Sensor1Value = _history.Sensor1Value; Sensor2Value = _history.Sensor2Value; Temperature = _history.Temperature; TimeStamp = _history.TimeStamp; _view = new EditHistoryItem(); _view.DataContext = this; Ok = new Command((x) => OkAction(), (x) => true); Cancel = new Command((x) => CancelAction(), (x) => true); }
private void AddHistoryItemAction() { var h = new SensorDBHistory(); var vm = new EditHistoryViewModel(h); vm.Show(); if (vm.Result == EditNodeResult.Ok) { CurrentSensor.History.Add(h); } }
public SensorDBHistoryViewModel(SensorDBItem sensor, SensorDBHistory sensorHistory) { if (sensor == null) throw new ArgumentNullException("sensor", "sensor is null."); if (sensorHistory == null) throw new ArgumentNullException("sensorHistory", "sensorHistory is null."); _sensor = sensor; _sensorHistory = sensorHistory; }
public bool AskSensor(SensorDBItem sensor) { if (string.IsNullOrEmpty(SelectedPort)) { SynchronisationHelper.ShowMessage("Порт не выбран", "Ошибка", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Exclamation); return false; } SynchronisationHelper.ShowMessage("Запрос может занять несколько секунд.", "Внимание!", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Information); try { using (SerialPort port = new SerialPort(SelectedPort, 38400)) { port.Open(); PowerOn(port); for (int i = 1; i <= RETRY; i++) //Запрос адреса { SendCommand((byte)(ModIndexToBt(sensor.SensorIndex) + ADDRESS), port); int? address = ReadBytes(port); if (!address.HasValue) //Опрос завершился ошибкой - идем на перееопрос { if (i != RETRY) continue; else //Датчик не ответил { PowerOff(port); SynchronisationHelper.ShowMessage("Датчик не отвечает", "Ошибка", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return false; } } if (address.Value != sensor.SensorId) //Адрес не совпал поругались и прервали опрос { PowerOff(port); SynchronisationHelper.ShowMessage("Индекс датчика не соотвествует выбранному", "Ошибка", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return false; } else break; //Адрес совпал выходим из опроса } System.Threading.Thread.Sleep(200); //Задержка int? sens1 = 0; for (int i = 0; i <= RETRY; i++) //Опрос первого датчика { SendCommand((byte)(ModIndexToBt(sensor.SensorIndex) + SENSOR1), port); sens1 = ReadBytes(port); if (sens1.HasValue) break; if (i == RETRY) { PowerOff(port); SynchronisationHelper.ShowMessage("Датчик не ответил на запросы первого показания", "Ошибка", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return false; } } System.Threading.Thread.Sleep(200); //Задержка int? sens2 = 0; for (int i = 0; i <= RETRY; i++) //Опрос первого датчика { SendCommand((byte)(ModIndexToBt(sensor.SensorIndex) + SENSOR2), port); sens2 = ReadBytes(port); if (sens2.HasValue) break; if (i == RETRY) { PowerOff(port); SynchronisationHelper.ShowMessage("Датчик не ответил на запросы второго показания", "Ошибка", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return false; } } System.Threading.Thread.Sleep(2500); //Задержка int? temp = 0; for (int i = 0; i <= RETRY; i++) //Опрос первого температуры { SendCommand((byte)(ModIndexToBt(sensor.SensorIndex) + TEMP), port); temp = ReadBytes(port); if (temp.HasValue) { temp = GetTemperature(temp.Value) / 16; break; } if (i == RETRY) { PowerOff(port); SynchronisationHelper.ShowMessage("Датчик не ответил на запросы температуры", "Ошибка", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return false; } } var sdbh = new SensorDBHistory { Sensor1Value = sens1.Value, Sensor2Value = sens2.Value, Temperature = temp.Value, TimeStamp = DateTime.Now }; sensor.History.Add(sdbh); PowerOff(port); } } catch (Exception err) { SynchronisationHelper.ShowMessage(err.Message, "Ошибка", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } return false; }