static void Main(string[] args) { IImputOutput viewer = new ConsoleOperations(); ControlApp app = new ControlApp(viewer); app.Start(args); }
/// <summary> /// 设置继电器的智能控制 /// </summary> private void SetRelay(SensorBase sensor, bool isRelayNum, ControlApp control) { _controlApp = control; byte bIlluminance = 0x00; byte bAirTemp = 0x00; byte bAirHumi = 0x00; byte bCO2 = 0x00; byte bPM25 = 0x00; byte bSmoke = 0x00; byte bRedinfrared = 0x00;//红外 if (Convert.ToDouble(_controlApp.AirTemp) < controlApp.MinAirTemp) { bAirTemp = 0x01; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测温度小于阈值,已自动打开温控系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } else if (Convert.ToDouble(_controlApp.AirTemp) > controlApp.MaxAirTemp) { bAirTemp = 0x00; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测温度大于阈值,已自动打开温控系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } if (Convert.ToDouble(_controlApp.AirHumi) < controlApp.MinAirHumi) { bAirHumi = 0x01; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测湿度小于阈值,已自动打开湿控系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } else if (Convert.ToDouble(_controlApp.AirHumi) > controlApp.MaxAirHumi) { bAirHumi = 0x00; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测湿度大于阈值,已自动打开湿控系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } if (Convert.ToDouble(_controlApp.PM25) < controlApp.MinPM25) { bPM25 = 0x01; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测PM25小于阈值,已自动打开净化系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } else if (Convert.ToDouble(_controlApp.PM25) > controlApp.MaxPM25) { bPM25 = 0x00; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测PM25大于阈值,已自动打开净化系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } if (Convert.ToDouble(_controlApp.C02Thickness) < controlApp.MinC02Thickness) { bCO2 = 0x01; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测CO2小于阈值,已自动打开排风系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } else if (Convert.ToDouble(_controlApp.C02Thickness) > controlApp.MaxC02Thickness) { bCO2 = 0x00; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测CO2大于阈值,已自动打开湿控系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } if (Convert.ToDouble(_controlApp.Illuminance) < controlApp.MinIlluminance) { bIlluminance = 0x01; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测光照强度小于阈值,已自动打开遮阳系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } else if (Convert.ToDouble(_controlApp.Illuminance) > controlApp.MaxIlluminance) { bIlluminance = 0x00; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测光照强度大于阈值,已自动打开遮阳系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } if (Convert.ToDouble(_controlApp.Smoke) != 0) { bSmoke = 0x01; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测有烟,已自动打开排风系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } if (Convert.ToDouble(_controlApp.radInfrared) != 0) { bRedinfrared = 0x01; OperationResult result = null; Message message = new Message(); message.Time = DateTime.Now; message.Messages = "智能控制:检测有人,已自动打开监测系统!"; result = MessageApp.Insert(message); if (result.ResultType != OperationResultType.Success) { MessageBox.Show(result.Message); return; } } if (isRelayNum == true) { Thread.Sleep(10); byte[] Senddata = sensor.Data; Senddata = new byte[] { bAirTemp, bAirHumi, bCO2, bIlluminance, Senddata[4] }; kvCoordinator.SendData(sensor.Type, sensor.Addr, Senddata); } else { Thread.Sleep(10); byte[] Senddata = sensor.Data; Senddata = new byte[] { bPM25, bSmoke, bRedinfrared, Senddata[2], Senddata[3], Senddata[4] }; kvCoordinator.SendData(sensor.Type, sensor.Addr, Senddata); } }
/// <summary> /// 环境参数 /// </summary> private void GetSensorData() { this.Dispatcher.BeginInvoke ( DispatcherPriority.Normal, (ThreadStart) delegate() { if (_controlApp != null) { _controlApp = controlApp; } sensorList = KV_WSN.Coordinator.sensorList.OrderByDescending(p => p.Type).ToList(); //传感器数据集合 foreach (SensorBase sensor in sensorList) { Thread.Sleep(100); //判断连接传感器的类型 switch (sensor.Type) { case 0xC0: //光照 lblIlluminance.Content = sensor.GetData(); _controlApp.Illuminance = (sensor.Data[0] * 256 + sensor.Data[1]).ToString(); break; case 0x30: //温湿度 lblTemperature.Content = sensor.GetData().Split(' ')[0]; lblHumidity.Content = sensor.GetData().Split(' ')[2]; _controlApp.AirTemp = sensor.Data[1].ToString(); _controlApp.AirHumi = sensor.Data[3].ToString(); break; case 0x33: //co2 lblCO2.Content = sensor.GetData(); _controlApp.C02Thickness = (sensor.Data[0] * 256 + sensor.Data[1]).ToString(); break; case 0x44: //pm2.5 lblPM25.Content = sensor.GetData(); _controlApp.PM25 = (sensor.Data[0] * 256 + sensor.Data[1]).ToString(); break; case 0x40: //烟雾 lblSmoke.Content = sensor.GetData(); if (sensor.Data[0] == 0x01) { _controlApp.Smoke = "1"; } else { _controlApp.Smoke = "0"; } break; case 0x50: //红外 lblradInfrared.Content = sensor.GetData(); if (sensor.Data[0] == 0x01) { _controlApp.radInfrared = "1"; } else { _controlApp.radInfrared = "255"; } break; case 0x34: //??? lblIlluminance.Content = sensor.GetData(); break; case 0x20: //??? //继电器控制 switch (sensor.Addr) { case 0x3F: SetRelay(sensor, true, _controlApp); break; case 0x41: SetRelay(sensor, false, _controlApp); break; } break; default: break; } } } ); }