黄学彪
2020-08-04 fd3c74df4d30a88d490d0c5b469df821f1bb2d78
ZigbeeApp/Shared/Phone/UserCenter/Residence/AddNewRoomForm.cs
@@ -106,8 +106,6 @@
            this.newRoom = new Common.Room();
            newRoom.FloorId = this.floorKeys;
            newRoom.BackgroundImage = btnPic.ImagePath;
            newRoom.Humidity = -1000;
            newRoom.Temperatrue = -1000;
            //添加全部菜单
            this.AddAllMenuRow(tableContr, btnPic);
@@ -205,14 +203,6 @@
                }
                newRoom.FloorId = this.floorKeys;
                if (newRoom.Humidity == -1000)
                {
                    newRoom.Humidity = 0;
                }
                if (newRoom.Temperatrue == -1000)
                {
                    newRoom.Temperatrue = 0;
                }
                //添加房间,主页需要重新刷新
                UserView.UserPage.Instance.RefreshAllForm = true;
@@ -313,63 +303,113 @@
            {
                return;
            }
            if (newRoom.Temperatrue == -1000)
            {
                //没有获取得到过温度
            //获取中
                btnTemper.Text = Language.StringByID(R.MyInternationalizationString.Getting);
            }
            else
            {
                btnTemper.Text = this.newRoom.Temperatrue == 0 ? "0.0℃" : this.newRoom.Temperatrue.ToString() + "℃";
                btnTemper.Text += "  " + Language.StringByID(R.MyInternationalizationString.Getting);
            }
            bool receiveData = false;
            HdlGatewayReceiveLogic.Current.RemoveEvent("TemperatrueDevice");
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("TemperatrueDevice", ReceiveComandDiv.A设备属性上报, ((report) =>
            {
                string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(report);
                if (newRoom.TemperatrueDevice != mainKeys || btnTemper == null)
                if (newRoom.TemperatrueDevice != mainKeys || btnTemper == null
                || report.DeviceStatusReport.CluterID != 1026)
                {
                    return;
                }
                receiveData = true;
                foreach (var data in report.DeviceStatusReport.AttriBute)
                {
                    if (data.AttributeId == (int)ZigBee.Device.AttriButeId.MeasuredValue)
                    {
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            if (data.AttriButeData == 0)
                    decimal temperatrue = (decimal)device.GetType().InvokeMember("currentTemperature", System.Reflection.BindingFlags.GetField, null, device, null);
                    if (temperatrue == 0)
                    {
                        //0
                        btnTemper.Text = "0.0℃";
                    }
                    else
                    {
                        btnTemper.Text = temperatrue + "℃";
                    }
                }, ShowErrorMode.NO);
            }));
            //发送获取温度的命令
            device.ReadAttri(ZigBee.Device.Cluster_ID.TemperatureMeasurement, ZigBee.Device.AttriButeId.MeasuredValue);
            HdlThreadLogic.Current.RunThread(() =>
            {
                //去获取设备列表的信息
                System.Threading.Thread.Sleep(1500);
                var result = Common.LocalDevice.Current.ReadDeviceEpointDeviceInfo(device);
                if (result != null)
                {
                    device.AttributeStatus.Clear();
                    device.AttributeStatus.AddRange(result.AttributeStatus);
                    device.ReSave();
                }
                if (receiveData == true)
                {
                    //已经读取到数据
                    return;
                }
                foreach (var data in device.AttributeStatus)
                {
                    if (data.ClusterId == 1026
                       && data.AttributeId == (int)ZigBee.Device.AttriButeId.MeasuredValue)
                    {
                        //处理网关返回的温度值
                        decimal temperatrue = 0;
                        this.AdjustTemperatrueValue(data.AttributeData, ref temperatrue);
                        device.GetType().InvokeMember("currentTemperature", System.Reflection.BindingFlags.SetField, null, device, new object[] { temperatrue });
                        device.ReSave();
                        //制作虚假的属性上报
                        var report = new ZigBee.Device.CommonDevice { DeviceAddr = device.DeviceAddr, DeviceEpoint = device.DeviceEpoint };
                        report.DeviceStatusReport.CluterID = 1026;
                        HdlGatewayReceiveLogic.Current.DeviceReportPush(report, ReceiveComandDiv.A设备属性上报);
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            btnTemper.Text = temperatrue == 0 ? "0.0℃" : temperatrue.ToString() + "℃";
                        });
                        break;
                    }
                }
            });
        }
        /// <summary>
        /// 处理网关返回的温度值
        /// </summary>
        /// <param name="value">网关返回的属性值</param>
        /// <param name="temperatrue">转换成的温度值</param>
        private void AdjustTemperatrueValue(int value, ref decimal temperatrue)
        {
            if (value == 0)
                            {
                                //0℃
                                btnTemper.Text = "0.0℃";
                                this.newRoom.Temperatrue = 0;
                temperatrue = 0;
                            }
                            else if (data.AttriButeData > 32767)
            else if (value > 32767)
                            {
                                //负数(特殊处理)
                                string strValue = (data.AttriButeData - 65536).ToString();
                string strValue = (value - 65536).ToString();
                                //小数点需要一位
                                strValue = strValue.Substring(0, strValue.Length - 1);
                                //记录温度
                                this.newRoom.Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                                btnTemper.Text = this.newRoom.Temperatrue.ToString() + "℃";
                temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                            }
                            else
                            {
                                //小数点需要一位
                                string strValue = data.AttriButeData.ToString();
                string strValue = value.ToString();
                                strValue = strValue.Substring(0, strValue.Length - 1);
                                //记录温度
                                this.newRoom.Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                                btnTemper.Text = this.newRoom.Temperatrue.ToString() + "℃";
                temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                            }
                        });
                    }
                }
            }));
            //发送获取温度的命令
            ((ZigBee.Device.TemperatureSensor)device).ReadTemperatureOrHumidity();
        }
        #endregion
@@ -430,50 +470,105 @@
            {
                return;
            }
            if (newRoom.Humidity == -1000)
            {
                //没有获取得到过湿度
            //获取中
                btnHumi.Text = Language.StringByID(R.MyInternationalizationString.Getting);
            }
            else
            {
                btnHumi.Text = this.newRoom.Humidity == 0 ? "0.0%" : this.newRoom.Humidity.ToString() + "%";
                btnHumi.Text += "  " + Language.StringByID(R.MyInternationalizationString.Getting);
            }
            bool receiveData = false;
            HdlGatewayReceiveLogic.Current.RemoveEvent("HumidityDevice");
            HdlGatewayReceiveLogic.Current.AddAttributeEvent("HumidityDevice", ReceiveComandDiv.A设备属性上报, (report) =>
            {
                string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(report);
                if (newRoom.HumidityDevice != mainKeys || btnHumi == null)
                if (newRoom.HumidityDevice != mainKeys || btnHumi == null
                || report.DeviceStatusReport.CluterID != 1029)
                {
                    return;
                }
                foreach (var data in report.DeviceStatusReport.AttriBute)
                {
                    if (data.AttributeId == (int)ZigBee.Device.AttriButeId.MeasuredValue)
                    {
                receiveData = true;
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            decimal humidity = (decimal)device.GetType().InvokeMember("Humidity", System.Reflection.BindingFlags.GetField, null, device, null);
                    decimal humidity = (decimal)device.GetType().InvokeMember("currentHumidity", System.Reflection.BindingFlags.GetField, null, device, null);
                            if (humidity == 0)
                            {
                                //0
                                btnHumi.Text = "0.0%";
                                this.newRoom.Humidity = 0;
                            }
                            else
                            {
                                //记录湿度
                                this.newRoom.Humidity = humidity;
                                btnHumi.Text = this.newRoom.Humidity.ToString() + "%";
                        btnHumi.Text = humidity.ToString() + "%";
                            }
                        });
                    }
                }
                }, ShowErrorMode.NO);
            });
            //发送获取湿度的命令
            device.GetType().InvokeMember("ReadTemperatureOrHumidity", System.Reflection.BindingFlags.InvokeMethod, null, device, null);
            device.ReadAttri(ZigBee.Device.Cluster_ID.RelativeHumidityMeasurement, ZigBee.Device.AttriButeId.MeasuredValue);
            HdlThreadLogic.Current.RunThread(() =>
            {
                //去获取设备列表的信息
                System.Threading.Thread.Sleep(1500);
                var result = Common.LocalDevice.Current.ReadDeviceEpointDeviceInfo(device);
                if (result != null)
                {
                    device.AttributeStatus.Clear();
                    device.AttributeStatus.AddRange(result.AttributeStatus);
                    device.ReSave();
                }
                if (receiveData == true)
                {
                    //已经读取到数据
                    return;
                }
                //初始值,取缓存数据
                foreach (var data in device.AttributeStatus)
                {
                    if (data.ClusterId == 1029
                       && data.AttributeId == (int)ZigBee.Device.AttriButeId.MeasuredValue)
                    {
                        //处理网关返回的湿度值
                        decimal humidity = 0;
                        this.AdjustHumidityValue(data.AttributeData, ref humidity);
                        device.GetType().InvokeMember("currentHumidity", System.Reflection.BindingFlags.SetField, null, device, new object[] { humidity });
                        device.ReSave();
                        //制作虚假的属性上报
                        var report = new ZigBee.Device.CommonDevice { DeviceAddr = device.DeviceAddr, DeviceEpoint = device.DeviceEpoint };
                        report.DeviceStatusReport.CluterID = 1029;
                        HdlGatewayReceiveLogic.Current.DeviceReportPush(report, ReceiveComandDiv.A设备属性上报);
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            btnHumi.Text = humidity == 0 ? "0.0%" : humidity.ToString() + "%";
                        });
                        break;
                    }
                }
            });
        }
        /// <summary>
        /// 处理网关返回的湿度值
        /// </summary>
        /// <param name="value">网关返回的属性值</param>
        /// <param name="temperatrue">转换成的湿度值</param>
        /// <param name="valueText">转换成的湿度值的翻译文本</param>
        private void AdjustHumidityValue(int value, ref decimal humidity)
        {
            if (value == 0)
            {
                //0%
                humidity = 0;
            }
            //不会出现负数
            else
            {
                //小数点需要一位
                string strValue = value.ToString();
                strValue = strValue.Substring(0, strValue.Length - 1);
                //记录温度
                humidity = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
            }
        }
        #endregion