/// <summary> /// 新增传感器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnInsertSensor_OnClick(object sender, RoutedEventArgs e) { if (lstCoor.SelectedItem != null) { _coordinator = GetListboxSelected(); Sensor sensor = new Sensor(); if (lstCoor != null) { sensor.CoordinatorID = _coordinator.ID; WindowSensorInfo windowSensorInfo = new WindowSensorInfo(sensor, true); if (windowSensorInfo.ShowDialog() == true) { _coordinator = GetListboxSelected(); lvSensorList.ItemsSource = ShowSensorData.ShowSensorDatasList(_coordinator); lblSensorCount.Content = "数量:" + lvSensorList.Items.Count; } } } else { MessageBox.Show("请先选择协调器!"); } }
/// <summary> /// listbox变化事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void lstCoor_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (lstCoor.SelectedItem != null) { try { _coordinator = GetListboxSelected(); if (_coordinator != null) { if (lblPort.Content.ToString() == "打开串口" && lblPing.Content.ToString() == "Ping") { _sensor.CoordinatorID = _coordinator.ID; data = ShowSensorData.ShowSensorDatasList(_coordinator); lvSensorList.ItemsSource = data; lblSensorCount.Content = "传感器数量:" + lvSensorList.Items.Count; } else { kvCoordinator.CloseSerialPort(); threadPing = false; lblPort.Content = "打开串口"; lblPing.Content = "Ping"; _sensor.CoordinatorID = _coordinator.ID; data = ShowSensorData.ShowSensorDatasList(_coordinator); lvSensorList.ItemsSource = data; lblSensorCount.Content = "传感器数量:" + lvSensorList.Items.Count; } } } catch (Exception) { } } }
/// <summary> /// 删除传感器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnDeleteSebsor_OnClick(object sender, RoutedEventArgs e) { if (lstCoor.SelectedItem != null) { OperationResult result = null; ShowSensorData showSensorData = GetListviewSelected(); if (showSensorData != null) { if (MessageBox.Show("是否删除该项", "询问", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { Sensor sensor; sensor = SensorApp.GetList().Where(p => p.Name == showSensorData.Name).First(); result = SensorApp.Delete(sensor); Message message = new Message(); message.Time = DateTime.Now; message.Messages = "删除" + sensor.Coordinator.Name + "协调器下" + "传感器:" + sensor.Name + " 类型:" + sensor.Type + " 地址:" + sensor.Address; if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); } else { MessageApp.Insert(message); _coordinator = GetListboxSelected(); lvSensorList.ItemsSource = ShowSensorData.ShowSensorDatasList(_coordinator); lblSensorCount.Content = "数量:" + lvSensorList.Items.Count; } } } else { MessageBox.Show("请选择要删除的传感器!"); } } }
/// <summary> /// 编辑传感器信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnUpdateSensor_OnClick(object sender, RoutedEventArgs e) { if (lstCoor.SelectedItem != null) { ShowSensorData showSensorData = GetListviewSelected(); if (showSensorData != null) { Sensor sensor; sensor = SensorApp.GetList().Where(p => p.Name == showSensorData.Name).First(); WindowSensorInfo windowSensorInfo = new WindowSensorInfo(sensor, false); if (windowSensorInfo.ShowDialog() == true) { _coordinator = GetListboxSelected(); lvSensorList.ItemsSource = ShowSensorData.ShowSensorDatasList(_coordinator); lblSensorCount.Content = "数量:" + lvSensorList.Items.Count; } } else { MessageBox.Show("请先选择编辑的传感器"); } } }