/// <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> /// 删除协调器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnDeleteCoor_OnClick(object sender, RoutedEventArgs e) { if (lstCoor.SelectedItem != null) { if (MessageBox.Show("是否删除该项", "询问", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { OperationResult result = null; if (GetListboxSelected() != null) { _coordinator = GetListboxSelected(); result = CoordinatorApp.Delete(_coordinator); Message message = new Message(); message.Time = DateTime.Now; message.Messages = "删除协调器:" + _coordinator.Name + " 串口号:" + _coordinator.COM + " PanID:" + _coordinator.PanID; if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); } else { MessageApp.Insert(message); SetListCoor(); } } else { MessageBox.Show("请选择要删除的协调器!"); } } } }
/// <summary> /// 编辑协调器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnUpdateCoor_OnClick(object sender, RoutedEventArgs e) { _coordinator = GetListboxSelected(); WindowCoorInfo windowCoorInfo = new WindowCoorInfo(_coordinator, false); if (windowCoorInfo.ShowDialog() == true) { SetListCoor(); } }
/// <summary> /// 新增协调器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnInsertCoor_OnClick(object sender, RoutedEventArgs e) { ES.Domain.Coordinator coor = new ES.Domain.Coordinator(); WindowCoorInfo windowCoorInfo = new WindowCoorInfo(coor, true); if (windowCoorInfo.ShowDialog() == true) { SetListCoor(); } }
/// <summary> /// 打开串口 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnOpenPortSensor_OnClick(object sender, RoutedEventArgs e) { if (lstCoor.SelectedItem != null) { if (lblPort.Content.ToString() == "打开串口") { if (lstCoor != null) { _coordinator = GetListboxSelected(); kvCoordinator.OpenSerialPort(_coordinator.COM); lblPort.Content = "关闭串口"; OperationResult result; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "打开串口:" + _coordinator.COM; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } } else { if (lblPing.Content.ToString() != "取消") { kvCoordinator.CloseSerialPort(); lblPort.Content = "打开串口"; OperationResult result; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "关闭串口:" + _coordinator.COM; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } else { MessageBox.Show("请先取消Ping"); } } } else { MessageBox.Show("请先选择协调器!"); } }
/// <summary> /// 显示传感器集合 /// </summary> /// <returns></returns> public static ObservableCollection <ShowSensorData> ShowSensorDatasList(Coordinator coordinator) { ObservableCollection <ShowSensorData> observableCollection = new ObservableCollection <ShowSensorData>(); foreach (Sensor sensor in GetCoorList(coordinator)) { ShowSensorData showSensor = new ShowSensorData(); showSensor.Name = sensor.Name; showSensor.Address = Converts.ByteToString((byte)sensor.Address); showSensor.State = "未连接"; observableCollection.Add(showSensor); } return(observableCollection); }
/// <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("请先选择编辑的传感器"); } } }
/// <summary> /// 对应协调器的传感器集合 /// </summary> /// <param name="coor"></param> /// <returns></returns> public static List <Sensor> GetCoorList(Coordinator coor) { return(GetList().Where(p => p.CoordinatorID == coor.ID).OrderBy(p => p.Type).ToList()); }