黄学彪
2020-09-18 c7df85937f73fb347ee0b19e9c052d2d00a6df6c
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayReceiveLogic.cs
@@ -251,6 +251,51 @@
                }
                #endregion
                #region ■ 电量推送
                //电量推送
                if (report.DeviceStatusReport.CluterID == 1)
                {
                    foreach (var attData in report.DeviceStatusReport.AttriBute)
                    {
                        //电量
                        if (attData.AttributeId == 33)
                        {
                            string receiptData = string.Empty;
                            //两个两个位置替换
                            for (int i = attData.AttriButeDataHex.Length - 1; i >= 0; i = i - 2)
                            {
                                receiptData += attData.AttriButeDataHex[i - 1].ToString() + attData.AttriButeDataHex[i].ToString();
                            }
                            int batteryValue = Convert.ToInt32(receiptData, 16);
                            //低于20%,则代表电量低
                            locadevice.IsBatteryDown = batteryValue < 20;
                        }
                        //已经接收到状态
                        locadevice.HadReadDeviceStatu = true;
                    }
                }
                #endregion
                #region ■ 蜂鸣器推送
                //蜂鸣器数据
                else if (report.DeviceStatusReport.CluterID == 1282)
                {
                    //mini夜灯
                    if (Common.LocalDevice.Current.IsMiniLight(locadevice) == true)
                    {
                        foreach (var attData in report.DeviceStatusReport.AttriBute)
                        {
                            if (attData.AttributeId == 0)
                            {
                                //这个是报警持续时间(大于2秒时,标记为还在响着)
                                ((ColorTemperatureLight)locadevice).IsBuzzerRing = attData.AttriButeData > 2 ? true : false;
                            }
                        }
                    }
                }
                #endregion
                #region ■ 窗帘数据
                //窗帘数据
                else if (report.DeviceStatusReport.CluterID == 258)
@@ -406,10 +451,43 @@
                else if (report.DeviceStatusReport.CluterID == 8)
                {
                    locadevice.DeviceStatusReport = report.DeviceStatusReport;
                    if (locadevice.Type == DeviceType.DimmableLight && report.DeviceStatusReport.AttriBute[0].AttributeId == 0)
                    if (report.DeviceStatusReport.AttriBute[0].AttributeId == 0)
                    {
                        //此属性表明当前亮度程度
                        ((DimmableLight)locadevice).Level = report.DeviceStatusReport.AttriBute[0].AttriButeData;
                        if (locadevice.Type == DeviceType.DimmableLight)
                        {
                            //此属性表明当前亮度程度
                            ((DimmableLight)locadevice).Level = report.DeviceStatusReport.AttriBute[0].AttriButeData;
                        }
                        else if (locadevice.Type == DeviceType.ColorTemperatureLight)
                        {
                            //此属性表明当前亮度程度
                            ((ColorTemperatureLight)locadevice).Level = report.DeviceStatusReport.AttriBute[0].AttriButeData;
                        }
                        //已经接收到状态
                        locadevice.HadReadDeviceStatu = true;
                        //if (locadevice is LightBase)
                        //{
                        //    //当接收到亮度值时,默认打开
                        //    ((LightBase)locadevice).OnOffStatus = 1;
                        //}
                    }
                }
                #endregion
                #region ■ 色温数据
                //色温数据
                else if (report.DeviceStatusReport.CluterID == 768)
                {
                    locadevice.DeviceStatusReport = report.DeviceStatusReport;
                    if (report.DeviceStatusReport.AttriBute[0].AttributeId == 0)
                    {
                        if (locadevice.Type == DeviceType.ColorTemperatureLight)
                        {
                            //此属性表明当前色温
                            int value = report.DeviceStatusReport.AttriBute[0].AttriButeData != 0 ? 1000000 / report.DeviceStatusReport.AttriBute[0].AttriButeData : 0;
                            ((ColorTemperatureLight)locadevice).ColorTemperature = value;
                        }
                        //已经接收到状态
                        locadevice.HadReadDeviceStatu = true;
                    }
@@ -425,27 +503,56 @@
                        //温度
                        if (attData.AttributeId == (int)AttriButeId.MeasuredValue)
                        {
                            if (attData.AttriButeData == 0)
                            decimal temperatrue = 0;
                            string receiptData = string.Empty;
                            //两个两个位置替换
                            for (int i = attData.AttriButeDataHex.Length - 1; i >= 0; i = i - 2)
                            {
                                ((TemperatureSensor)locadevice).Temperatrue = 0;
                                receiptData += attData.AttriButeDataHex[i - 1].ToString() + attData.AttriButeDataHex[i].ToString();
                            }
                            else if (attData.AttriButeData > 32767)
                            {
                                //负数(特殊处理)
                                string strValue = (attData.AttriButeData - 65536).ToString();
                                //小数点需要一位
                                strValue = strValue.Substring(0, strValue.Length - 1);
                                ((TemperatureSensor)locadevice).Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                            }
                            else
                            //有符号(会出现负数)
                            if (attData.AttriButeDataType == 40 || attData.AttriButeDataType == 41)
                            {
                                //小数点需要一位
                                string strValue = attData.AttriButeData.ToString();
                                string strValue = Convert.ToInt16(receiptData, 16).ToString();
                                strValue = strValue.Substring(0, strValue.Length - 1);
                                ((TemperatureSensor)locadevice).Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                                temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                            }
                            //无符号(不会出现负数)
                            else if (attData.AttriButeDataType == 32 || attData.AttriButeDataType == 33)
                            {
                                ushort shortData = Convert.ToUInt16(receiptData, 16);
                                if (shortData > 32767)
                                {
                                    //负数(特殊处理)
                                    string strValue = (shortData - 65536).ToString();
                                    //小数点需要一位
                                    strValue = strValue.Substring(0, strValue.Length - 1);
                                    temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                                }
                                else
                                {
                                    //小数点需要一位
                                    string strValue = shortData.ToString();
                                    strValue = strValue.Substring(0, strValue.Length - 1);
                                    temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                                }
                            }
                            //温度传感器
                            if (locadevice is TemperatureSensor)
                            {
                                ((TemperatureSensor)locadevice).currentTemperature = temperatrue;
                            }
                            //PM2.5传感器
                            else if (locadevice is PMSensor)
                            {
                                ((PMSensor)locadevice).currentTemperature = (int)temperatrue;
                            }
                            //已经接收到状态
                            locadevice.HadReadDeviceStatu = true;
                            //温度值需要保存
                            locadevice.ReSave();
                        }
                    }
                }
@@ -460,25 +567,203 @@
                        //湿度
                        if (attData.AttributeId == (int)AttriButeId.MeasuredValue)
                        {
                            if (attData.AttriButeData == 0)
                            decimal humidity = 0;
                            string receiptData = string.Empty;
                            //两个两个位置替换
                            for (int i = attData.AttriButeDataHex.Length - 1; i >= 0; i = i - 2)
                            {
                                ((TemperatureSensor)locadevice).Humidity = 0;
                                receiptData += attData.AttriButeDataHex[i - 1].ToString() + attData.AttriButeDataHex[i].ToString();
                            }
                            else
                            //有符号(会出现负数)
                            if (attData.AttriButeDataType == 40 || attData.AttriButeDataType == 41)
                            {
                                //小数点需要一位(湿度没有负数)
                                string strValue = attData.AttriButeData.ToString();
                                //小数点需要一位
                                string strValue = Convert.ToInt16(receiptData, 16).ToString();
                                strValue = strValue.Substring(0, strValue.Length - 1);
                                ((TemperatureSensor)locadevice).Humidity = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                                humidity = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                            }
                            //无符号(不会出现负数)
                            else if (attData.AttriButeDataType == 32 || attData.AttriButeDataType == 33)
                            {
                                //小数点需要一位 湿度不会出现负数
                                string strValue = Convert.ToUInt16(receiptData, 16).ToString();
                                strValue = strValue.Substring(0, strValue.Length - 1);
                                humidity = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                            }
                            //湿度传感器
                            if (locadevice is TemperatureSensor)
                            {
                                ((TemperatureSensor)locadevice).currentHumidity = humidity;
                            }
                            //新风的湿度传感器
                            else if (locadevice is HumiditySensor)
                            {
                                ((HumiditySensor)locadevice).currentHumidity = humidity;
                            }
                            //PM2.5传感器
                            else if (locadevice is PMSensor)
                            {
                                ((PMSensor)locadevice).currentHumidity = (int)humidity;
                            }
                            //已经接收到状态
                            locadevice.HadReadDeviceStatu = true;
                            //湿度值需要保存
                            locadevice.ReSave();
                        }
                    }
                }
                #endregion
                #region ■ PM2.5数据
                //PM2.5数据
                else if (report.DeviceStatusReport.CluterID == 1066)
                {
                    foreach (var attData in report.DeviceStatusReport.AttriBute)
                    {
                        //PM2.5
                        if (attData.AttributeId == (int)AttriButeId.MeasuredValue)
                        {
                            if (attData.AttriButeDataType == 57)
                            {
                                ((PMSensor)locadevice).currentPmData = attData.AttriButeData;
                            }
                        }
                        //已经接收到状态
                        locadevice.HadReadDeviceStatu = true;
                    }
                }
                #endregion
                #region ■ 设备基础信息
                else if (report.DeviceStatusReport.CluterID == 0)
                {
                    var listLocalDevice = Common.LocalDevice.Current.GetDevicesByMac(deviceAddr, false);
                    //属性都是一样的
                    foreach (var myDevice in listLocalDevice)
                    {
                        //属性是否改变
                        bool attriButeChanged = false;
                        foreach (var data in report.DeviceStatusReport.AttriBute)
                        {
                            //生产商名字
                            if (data.AttributeId == 4 && data.AttriButeDataHex.Length > 2)
                            {
                                if (data.AttriButeDataHex.Length > 2)
                                {
                                    var value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                                    if (myDevice.ManufacturerName != value)
                                    {
                                        //属性变更了
                                        attriButeChanged = true;
                                    }
                                    myDevice.ManufacturerName = value;
                                }
                            }
                            //型号码(也叫模块ID)
                            if (data.AttributeId == 5)
                            {
                                if (data.AttriButeDataHex.Length > 2)
                                {
                                    var value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                                    if (myDevice.ModelIdentifier != value)
                                    {
                                        //属性变更了
                                        attriButeChanged = true;
                                    }
                                    myDevice.ModelIdentifier = value;
                                }
                            }
                            //生产日期
                            if (data.AttributeId == 6)
                            {
                                if (data.AttriButeDataHex.Length > 2)
                                {
                                    var value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                                    if (myDevice.ProductionDate != value)
                                    {
                                        //属性变更了
                                        attriButeChanged = true;
                                    }
                                    myDevice.ProductionDate = value;
                                }
                            }
                            //电源
                            if (data.AttributeId == 7)
                            {
                                myDevice.PowerSource = data.AttriButeData;
                            }
                            //序列号
                            if (data.AttributeId == 13)
                            {
                                if (data.AttriButeDataHex.Length > 2)
                                {
                                    string value;
                                    if (Common.LocalDevice.Current.IsHdlDevice(myDevice) == false)
                                    {
                                        //第三方设备
                                        value = data.AttriButeDataHex.Substring(2);
                                    }
                                    else
                                    {
                                        //河东设备
                                        value = UserCenterLogic.TranslateHexadecimalIntoText(data.AttriButeDataHex.Substring(2));
                                    }
                                    if (myDevice.SerialNumber != value)
                                    {
                                        //属性变更了
                                        attriButeChanged = true;
                                    }
                                    myDevice.SerialNumber = value;
                                }
                            }
                        }
                        //如果属性变更了
                        if (attriButeChanged == true && myDevice.IsCustomizeImage == false)
                        {
                            //UI重新生成
                            myDevice.IconPath = string.Empty;
                            myDevice.ReSave();
                        }
                    }
                }
                #endregion
            }
            else if (deviceEpoint == 200)
            {
                var localOta = Common.LocalDevice.Current.GetOTADevice(deviceAddr);
                if (localOta != null)
                {
                    #region ■ 固件版本
                    //固件版本
                    if (report.DeviceStatusReport.CluterID == (int)Cluster_ID.Ota)
                    {
                        foreach (var data in report.DeviceStatusReport.AttriBute)
                        {
                            //镜像版本
                            if (data.AttributeId == (int)AttriButeId.ImgVersion)
                            {
                                localOta.ImgVersion = data.AttriButeData;
                            }
                            //硬件版本
                            if (data.AttributeId == (int)AttriButeId.mgHWversion)
                            {
                                localOta.HwVersion = data.AttriButeData;
                            }
                            //镜像ID
                            if (data.AttributeId == (int)AttriButeId.ImgTypeId)
                            {
                                localOta.ImgTypeId = data.AttriButeData;
                            }
                        }
                        localOta.ReSave();
                    }
                    #endregion
                }
            }
            this.DeviceReportPush(report, ReceiveComandDiv.A设备属性上报);
        }
@@ -529,8 +814,7 @@
        /// <param name="receiveData"></param>
        private void DoorLockDeviceReportPush(JObject receiveData)
        {
            //只有徐梅的门锁界面没有打开的情况下,才会处理这个东西
            if (ControlCommonResourse.IsDoorLockPageOpen == false && UserCenterResourse.UserInfo.AuthorityNo == 1)
            if (UserCenterResourse.UserInfo.AuthorityNo == 1)
            {
                var device = Common.LocalDevice.Current.GetDevice(receiveData.Value<string>("DeviceAddr"), receiveData.Value<int>("Epoint"));
                if (device.Type != DeviceType.DoorLock)
@@ -538,31 +822,27 @@
                    //它不是门锁
                    return;
                }
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DoorLockAlarmsResult>(receiveData["Data"].ToString());
                if (info.Clusterid == 257)
                {
                    //常开模式关闭
                    if (info.AlarmCode == 217 || info.AlarmCode == 219)
                    //216:锁上设置的常开
                    if (info.AlarmCode == 216)
                    {
                        HdlThreadLogic.Current.RunThread(async () =>
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            //温居城的要网关中是否有逻辑存在
                            //参数:2-常开自动化;3-失效时间自动化
                            var result = await Shared.Phone.Device.Logic.SkipView.Exist(2);
                            HdlThreadLogic.Current.RunMain(() =>
                            //更新门锁涉及的常开/常关的界面
                            if (UserCenter.DoorLock.DoorLockCommonInfo.UpdateCurrentDoorlockAction != null)
                            {
                                //弹出徐梅的那个窗口
                                DoorLock.DoorLockCommonInfo.NomallyOpenModeInvalidDialog((ZigBee.Device.DoorLock)device, DoorLock.DoorLockCommonInfo.DoorLockMessType.ServicePush,
                                    result != 0 ? true : false, null);
                            });
                                UserCenter.DoorLock.DoorLockCommonInfo.UpdateCurrentDoorlockAction(device.DeviceAddr, true);
                            }
                        });
                    }
                }
                //显示有新消息的特效
                this.ShowHadNewMessageAppeal();
            }
            //显示有新消息的特效
            this.ShowHadNewMessageAppeal();
        }
        #endregion
        #region ■ 设备在线状态更新反馈_______________
@@ -571,7 +851,7 @@
        /// 设备在线状态更新反馈
        /// </summary>
        /// <param name="receiveData"></param>
        private void DeviceOnlineChangePush (JObject receiveData)
        private void DeviceOnlineChangePush(JObject receiveData)
        {
            if (this.dicDeviceEvent.Count == 0)
            {
@@ -756,29 +1036,40 @@
            //显示有新消息的特效
            this.ShowHadNewMessageAppeal();
            //这个东西暂时不处理
            ////只有徐梅的门锁界面没有打开的情况下,才会处理这个东西
            //if (ControlCommonResourse.IsDoorLockPageOpen == false && UserCenterResourse.UserInfo.AuthorityNo == 1)
            //{
            //    var device = Common.LocalDevice.Current.GetDevice(receiveData.Value<string>("DeviceAddr"), receiveData.Value<int>("Epoint"));
            //    if (device.Type != DeviceType.DoorLock)
            //    {
            //        return;
            //    }
            //    //自动化执行 常开关闭
            //    var data = Newtonsoft.Json.JsonConvert.DeserializeObject<LogicPushResult>(receiveData["Data"].ToString());
            //    if (data.ActionData != null && data.ActionData.Actiontype == 8
            //        && data.ActionData.PassDataString == "055704010113")
            //    {
            //        HdlThreadLogic.Current.RunMain(() =>
            //        {
            //            //弹出徐梅的那个窗口
            //            DoorLock.DoorLockCommonInfo.NomallyOpenModeInvalidDialog((ZigBee.Device.DoorLock)device, DoorLock.DoorLockCommonInfo.DoorLockMessType.ServicePush, null);
            //        });
            //    }
            //}
        }
            // 逻辑执行常开模式失效的情况
            if (UserCenterResourse.UserInfo.AuthorityNo == 1)
            {
                //自动化执行 常开关闭
                var data = Newtonsoft.Json.JsonConvert.DeserializeObject<LogicPushResult>(receiveData["Data"].ToString());
                if (data != null && data.ActionData != null)
                {
                    if (data.ActionData.Actiontype == 8 && data.ActionData.PassDataString == "055704010113")
                    {
                        var deviceAddr = data.ActionData.MacStr;
                        var device = Common.LocalDevice.Current.GetDevicesByMac(deviceAddr, false);
                        if (device.Count > 0 && device[0].Type != DeviceType.DoorLock)
                        {
                            return;
                        }
                        HdlThreadLogic.Current.RunThread(async () =>
                        {
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                //提示门锁已经失效
                                new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.NormallyClosed), Direction = AMPopTipDirection.None, CloseTime = 1 }.Show(Common.CommonPage.Instance);
                                //更新门锁涉及的常开/常关的界面
                                if (UserCenter.DoorLock.DoorLockCommonInfo.UpdateCurrentDoorlockAction != null)
                                {
                                    UserCenter.DoorLock.DoorLockCommonInfo.UpdateCurrentDoorlockAction(deviceAddr, false);
                                }
                            });
                        });
                    }
                }
            }
        }
        #endregion
        #region ■ 场景触发上报_______________________