xm
2020-04-16 6fa9d69da922c8049f5acfcbb9ce9fd26811024c
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayReceiveLogic.cs
@@ -51,8 +51,8 @@
        /// <param name="gatewayId">网关ID</param>
        /// <param name="topic">整个主题</param>
        /// <param name="reportTopic">上报数据的主题</param>
        /// <param name="receiveData">接收的数据</param>
        public void GatewayOverallMsgReceive(string gatewayId, string topic, string reportTopic, JObject receiveData)
        /// <param name="msgData">接收的数据</param>
        public void GatewayOverallMsgReceive(string gatewayId, string topic, string reportTopic, string msgData)
        {
            if (topic == "AppNoLogin")
            {
@@ -67,7 +67,7 @@
                });
                return;
            }
            else if (topic == "BeingSqueezedOffline")
            else if (topic == "ZigbeeGateWayToClient/" + Common.Config.Instance.ConnEmqClientId + "/Push/NotifySqueeze")
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
@@ -93,6 +93,50 @@
                });
                return;
            }
            else if (topic == "ZigbeeGateWayToClient/" + Common.Config.Instance.Home.Id + "_" + Common.Config.Instance.Guid + "/PrimaryUserDelYou")
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //分享住宅已更改,请联系管理员!
                    string msg = Language.StringByID(R.MyInternationalizationString.uShardResidenceHadDeletePleaseTakeToAdmin);
                    var contr = new ShowMsgControl(ShowMsgType.Tip, msg);
                    contr.Show();
                    UserCenterLogic.ReLoginAgain(UserCenterResourse.UserInfo.Account, false);
                });
                return;
            }
            else if (topic == "ZigbeeGateWayToClient/" + Common.Config.Instance.Guid + "/Push/Update")
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //您的权限已经变更,请重新登陆
                    string msg = Language.StringByID(R.MyInternationalizationString.uYouAccessHadChangedPleaseTakeToAdmin);
                    var contr = new ShowMsgControl(ShowMsgType.Tip, msg);
                    contr.Show();
                    UserCenterLogic.ReLoginAgain(UserCenterResourse.UserInfo.Account, false);
                });
                return;
            }
            else if (UserCenterResourse.UserInfo.AuthorityNo == 3)
            {
                if (topic == "ZigbeeGateWayToClient/" + Common.Config.Instance.Guid + "/Push/Deleted"
                || topic == "ZigbeeGateWayToClient/" + Common.Config.Instance.Guid + "/Push/DeletedShareData")
                {
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        //分享数据已经变更,请重新登陆
                        string msg = Language.StringByID(R.MyInternationalizationString.uShardDataIsChangedPleaseLoginAgain);
                        var contr = new ShowMsgControl(ShowMsgType.Tip, msg);
                        contr.Show();
                        UserCenterLogic.ReLoginAgain(UserCenterResourse.UserInfo.Account, false);
                    });
                    return;
                }
            }
            try
            {
                if (HdlGatewayLogic.Current.IsGatewayExist(gatewayId) == false)
@@ -100,52 +144,56 @@
                    //不是自己绑定的网关,则不处理
                    return;
                }
                //设备属性上报
                if (reportTopic == "DeviceStatusReport")
                {
                    //设备属性上报
                    this.DeviceAttributeReportPush(receiveData);
                    this.DeviceAttributeReportPush(JObject.Parse(msgData));
                }
                //传感器上报
                else if (reportTopic == "IASInfoReport")
                {
                    this.SensorDeviceReportPush(receiveData);
                    this.SensorDeviceReportPush(JObject.Parse(msgData));
                }
                //门锁上报
                else if (topic == gatewayId + "/Alarms/SendAlarmInform")
                {
                    this.DoorLockDeviceReportPush(receiveData);
                    this.DoorLockDeviceReportPush(JObject.Parse(msgData));
                }
                //通过外部方式布防撤防成功时报告
                else if (topic == gatewayId + "/Security/EnOrWithdrawSucceedReport")
                {
                    this.SecurityEnOrWithdrawSucceedReport(receiveData);
                    this.SecurityEnOrWithdrawSucceedReport(JObject.Parse(msgData));
                }
                //设备在线状态更新反馈
                else if (reportTopic == "OnlineStatusChange_Respon")
                {
                    this.DeviceOnlineChangePush(receiveData);
                    this.DeviceOnlineChangePush(JObject.Parse(msgData));
                }
                //设备控制状态反馈
                else if (reportTopic == "DeviceDefaultAck")
                {
                    this.DeviceControlResponePush(JObject.Parse(msgData));
                }
                //撤防
                else if (topic == gatewayId + "/Security/WithdrawMode_Respon")
                {
                    this.RemoveSafetyGarrisonPush(receiveData);
                    this.RemoveSafetyGarrisonPush(JObject.Parse(msgData));
                }
                //布防
                else if (topic == gatewayId + "/Security/EnableMode_Respon")
                {
                    this.SetSafetyGarrisonPush(receiveData);
                    this.SetSafetyGarrisonPush(JObject.Parse(msgData));
                }
                //逻辑触发上报
                else if (topic == gatewayId + "/Logic/Execute_Respon")
                {
                    this.LogicExecutePush(receiveData);
                    this.LogicExecutePush(null);
                }
                //场景触发上报
                else if (topic == gatewayId + "/Scene/Exec_Respon")
                {
                    this.SceneExecPush(receiveData);
                    this.SceneExecPush(null);
                }
            }
            catch (Exception ex)
@@ -173,10 +221,235 @@
            var deviceAddr = receiveData.Value<string>("DeviceAddr");
            var deviceEpoint = receiveData.Value<int>("Epoint");
            var tempDevice = new CommonDevice { DeviceAddr = deviceAddr, DeviceEpoint = deviceEpoint };
            tempDevice.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.DeviceStatusReportData>(receiveData["Data"].ToString());
            var report = new CommonDevice { DeviceAddr = deviceAddr, DeviceEpoint = deviceEpoint };
            report.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.DeviceStatusReportData>(receiveData["Data"].ToString());
            if (report.DeviceStatusReport.AttriBute.Count == 0)
            {
                //网关有些奇葩,没有属性它也会发过来
                return;
            }
            this.DeviceReportPush(tempDevice, ReceiveComandDiv.A设备属性上报);
            //处理网关上报的数据,然后变更本地缓存
            var locadevice = Common.LocalDevice.Current.GetDevice(deviceAddr, deviceEpoint);
            if (locadevice != null)
            {
                //有反馈,这个设备肯定是在线的
                locadevice.IsOnline = 1;
                locadevice.LastDateTime = DateTime.Now;
                #region ■ 开关功能
                //开关功能
                if (report.DeviceStatusReport.CluterID == 6)
                {
                    if (locadevice is LightBase)
                    {
                        locadevice.DeviceStatusReport = report.DeviceStatusReport;
                        ((LightBase)locadevice).OnOffStatus = report.DeviceStatusReport.AttriBute[0].AttriButeData;
                        //已经接收到状态
                        locadevice.HadReadDeviceStatu = true;
                    }
                }
                #endregion
                #region ■ 窗帘数据
                //窗帘数据
                else if (report.DeviceStatusReport.CluterID == 258)
                {
                    //窗帘类型
                    if (report.DeviceStatusReport.AttriBute[0].AttributeId == 0)
                    {
                        locadevice.DeviceStatusReport = report.DeviceStatusReport;
                        ((Rollershade)locadevice).WcdType = report.DeviceStatusReport.AttriBute[0].AttriButeData;
                        //这个东西要保存
                        locadevice.ReSave();
                    }
                    //窗帘百分比
                    else if (report.DeviceStatusReport.AttriBute[0].AttributeId == 8)
                    {
                        locadevice.DeviceStatusReport = report.DeviceStatusReport;
                        ((Rollershade)locadevice).WcdCurrentPositionLiftPercentage = report.DeviceStatusReport.AttriBute[0].AttriButeData;
                        //已经接收到状态
                        locadevice.HadReadDeviceStatu = true;
                    }
                }
                #endregion
                #region ■ 空调数据
                //空调数据
                else if (report.DeviceStatusReport.CluterID == 513)
                {
                    locadevice.DeviceStatusReport = report.DeviceStatusReport;
                    foreach (var attData in report.DeviceStatusReport.AttriBute)
                    {
                        var curTemp = attData.AttriButeData / 100;
                        if (attData.AttributeId == 0)
                        {
                            //此属性表明室内当前的温度 * 100,实际温度为“LocalTemperature / 100”,单位:℃
                            ((AC)locadevice).currentLocalTemperature = curTemp;
                            //已经接收到状态
                            locadevice.HadReadDeviceStatu = true;
                        }
                        else if (attData.AttributeId == 17)
                        {
                            //此属性表明室内当前的温度 * 100,实际温度为“LocalTemperature / 100”,单位:℃
                            ((AC)locadevice).currentCoolingSetpoint = curTemp;
                            //已经接收到状态
                            locadevice.HadReadDeviceStatu = true;
                        }
                        else if (attData.AttributeId == 18)
                        {
                            //此属性表明此设备当前的制热温度,实际温度为“HeatingSetpoint / 100”,单位:℃。
                            ((AC)locadevice).currentHeatingSetpoint = curTemp;
                            //已经接收到状态
                            locadevice.HadReadDeviceStatu = true;
                        }
                        else if (attData.AttributeId == 28)
                        {
                            //此属性描述恒温设备正处于哪种模式
                            ((AC)locadevice).currentSystemMode = attData.AttriButeData;
                            //已经接收到状态
                            locadevice.HadReadDeviceStatu = true;
                        }
                        else if (attData.AttributeId == 4096)
                        {
                            //此属性表明此设备当前的自动温度,实际温度为“AutoSetpoint / 100”,单位:℃。
                            ((AC)locadevice).currentAutoSetpoint = curTemp;
                            //已经接收到状态
                            locadevice.HadReadDeviceStatu = true;
                        }
                        else if (attData.AttributeId == 4097)
                        {
                            //过虑网清洗标志:42
                            ((AC)locadevice).CleanStatu = attData.AttriButeData == 42;
                        }
                        else if (attData.AttributeId == 4099)
                        {
                            //转换为二进制
                            var value = Convert.ToString(attData.AttriButeData, 2).PadLeft(16, '0');
                            //这五个设置是放在后面的
                            value = value.Substring(value.Length - 5, 5);
                            for (int i = 0; i < value.Length; i++)
                            {
                                //自定义的空调模式
                                ((AC)locadevice).listSupportMode[i] = Convert.ToInt32(value[i].ToString());
                            }
                            locadevice.ReSave();
                        }
                    }
                }
                //空调数据
                else if (report.DeviceStatusReport.CluterID == 514)
                {
                    locadevice.DeviceStatusReport = report.DeviceStatusReport;
                    foreach (var attData in report.DeviceStatusReport.AttriBute)
                    {
                        if (attData.AttributeId == 0)
                        {
                            //风扇模式
                            ((AC)locadevice).currentFanMode = attData.AttriButeData;
                            //已经接收到状态
                            locadevice.HadReadDeviceStatu = true;
                        }
                        else if (attData.AttributeId == 4096)
                        {
                            //风扇扫风
                            ((AC)locadevice).currentFanSwingMode = attData.AttriButeData;
                            //已经接收到状态
                            locadevice.HadReadDeviceStatu = true;
                        }
                        else if (attData.AttributeId == 4097)
                        {
                            //转换为二进制
                            var value = Convert.ToString(attData.AttriButeData, 2).PadLeft(16, '0');
                            //这个设置是放在后面的
                            value = value.Substring(value.Length - 1, 1);
                            //启用摆风功能
                            ((AC)locadevice).UseSwingFunction = value == "1";
                            locadevice.ReSave();
                        }
                    }
                }
                #endregion
                #region ■ 亮度数据
                //亮度数据
                else if (report.DeviceStatusReport.CluterID == 8)
                {
                    locadevice.DeviceStatusReport = report.DeviceStatusReport;
                    if (locadevice.Type == DeviceType.DimmableLight && report.DeviceStatusReport.AttriBute[0].AttributeId == 0)
                    {
                        //此属性表明当前亮度程度
                        ((DimmableLight)locadevice).Level = report.DeviceStatusReport.AttriBute[0].AttriButeData;
                        //已经接收到状态
                        locadevice.HadReadDeviceStatu = true;
                    }
                }
                #endregion
                #region ■ 温度数据
                //温度数据
                else if (report.DeviceStatusReport.CluterID == 1026)
                {
                    foreach (var attData in report.DeviceStatusReport.AttriBute)
                    {
                        //温度
                        if (attData.AttributeId == (int)AttriButeId.MeasuredValue)
                        {
                            if (attData.AttriButeData == 0)
                            {
                                ((TemperatureSensor)locadevice).Temperatrue = 0;
                            }
                            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
                            {
                                //小数点需要一位
                                string strValue = attData.AttriButeData.ToString();
                                strValue = strValue.Substring(0, strValue.Length - 1);
                                ((TemperatureSensor)locadevice).Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                            }
                            //已经接收到状态
                            locadevice.HadReadDeviceStatu = true;
                        }
                    }
                }
                #endregion
                #region ■ 湿度数据
                //湿度数据
                else if (report.DeviceStatusReport.CluterID == 1029)
                {
                    foreach (var attData in report.DeviceStatusReport.AttriBute)
                    {
                        //湿度
                        if (attData.AttributeId == (int)AttriButeId.MeasuredValue)
                        {
                            if (attData.AttriButeData == 0)
                            {
                                ((TemperatureSensor)locadevice).Humidity = 0;
                            }
                            else
                            {
                                //小数点需要一位(湿度没有负数)
                                string strValue = attData.AttriButeData.ToString();
                                strValue = strValue.Substring(0, strValue.Length - 1);
                                ((TemperatureSensor)locadevice).Humidity = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                            }
                        }
                        //已经接收到状态
                        locadevice.HadReadDeviceStatu = true;
                    }
                }
                #endregion
            }
            this.DeviceReportPush(report, ReceiveComandDiv.A设备属性上报);
        }
        #endregion
@@ -199,6 +472,15 @@
                HdlAlarmsLogic.Current.SaveSafeguardAlarmInfo(ias);
            }
            //处理网关上报的数据,然后变更本地缓存
            var locadevice = Common.LocalDevice.Current.GetDevice(ias.DeviceAddr, ias.DeviceEpoint);
            if (locadevice != null)
            {
                ((IASZone)locadevice).iASInfo = ias.iASInfo;
                //记录回复时间
                locadevice.LastDateTime = DateTime.Now;
            }
            this.DeviceReportPush(ias, ReceiveComandDiv.A传感器上报);
            //显示有新消息的特效
@@ -215,9 +497,25 @@
        /// <param name="receiveData"></param>
        private void DoorLockDeviceReportPush(JObject receiveData)
        {
            //保存门锁信息到本地
            HdlAlarmsLogic.Current.SaveDoorLockAlarmInfo(receiveData);
            //只有徐梅的门锁界面没有打开的情况下,才会处理这个东西
            if (ControlCommonResourse.IsDoorLockPageOpen == false)
            {
                var device = Common.LocalDevice.Current.GetDevice(receiveData.Value<string>("DeviceAddr"), receiveData.Value<int>("Epoint"));
                if ((device is ZigBee.Device.DoorLock) == false)
                {
                    //它不是门锁
                    return;
                }
                var info = Newtonsoft.Json.JsonConvert.DeserializeObject<DoorLockAlarmsResult>(receiveData["Data"].ToString());
                if (info.Clusterid == 257)
                {
                    //常开模式开启
                    if (info.AlarmCode == 216)
                    {
                        DoorLock.DoorLockCommonInfo.NomallyOpenModeInvalidDialog((ZigBee.Device.DoorLock)device, DoorLock.DoorLockCommonInfo.DoorLockMessType.ServicePush, null);
                    }
                }
            }
            //显示有新消息的特效
            this.ShowHadNewMessageAppeal();
        }
@@ -238,9 +536,39 @@
                return;
            }
            var tempDevice = new CommonDevice() { DeviceAddr = receiveData.Value<string>("DeviceAddr"), DeviceEpoint = receiveData.Value<int>("Epoint") };
            tempDevice.IsOnline = Newtonsoft.Json.JsonConvert.DeserializeObject<int>(receiveData["Data"]["IsOnline"].ToString());
            tempDevice.IsOnline = Convert.ToInt32(receiveData["Data"]["IsOnline"].ToString());
            //处理网关上报的数据,然后变更本地缓存
            var locadevice = Common.LocalDevice.Current.GetDevice(tempDevice.DeviceAddr, tempDevice.DeviceEpoint);
            if (locadevice != null)
            {
                locadevice.IsOnline = tempDevice.IsOnline;
                //记录回复时间
                locadevice.LastDateTime = DateTime.Now;
                locadevice.ReSave();
            }
            this.DeviceReportPush(tempDevice, ReceiveComandDiv.A设备在线上报);
        }
        #endregion
        #region ■ 设备控制状态反馈___________________
        /// <summary>
        /// 设备控制状态反馈
        /// </summary>
        /// <param name="receiveData"></param>
        private void DeviceControlResponePush(JObject receiveData)
        {
            if (this.dicDeviceEvent.Count == 0)
            {
                //没有添加监听
                return;
            }
            var tempDevice = new CommonDevice() { DeviceAddr = receiveData.Value<string>("DeviceAddr"), DeviceEpoint = receiveData.Value<int>("Epoint") };
            this.DeviceReportPush(tempDevice, ReceiveComandDiv.A节点控制反馈);
        }
        #endregion
@@ -401,10 +729,12 @@
        #endregion
        #region ■ 添加设备事件_______________________
        /// <summary>
        /// 添加获取设备属性的事件(推送已经强制指定运行于主线程,属性上报的对象:device.DeviceStatusReport)
        /// 添加获取设备属性的事件(属性上报的对象:device.DeviceStatusReport)
        /// </summary>
        /// <param name="mainKeys">标识事件的主键(可以随便填,主要是针对多个界面一起使用的情况)</param>
        /// <param name="comand">命令区分</param>
@@ -474,46 +804,47 @@
        #region ■ 一般方法___________________________
        /// <summary>
        /// 设备上报推送
        /// 设备上报推送(调用此方法,他会推送到各自的界面)
        /// </summary>
        /// <param name="common"></param>
        /// <param name="comand"></param>
        private void DeviceReportPush(CommonDevice common, ReceiveComandDiv comand)
        public void DeviceReportPush(CommonDevice common, ReceiveComandDiv comand)
        {
            if (this.dicDeviceEvent.Count == 0)
            {
                //没有添加监听
                return;
            }
            lock (this.dicDeviceEvent)
            //lock (this.dicDeviceEvent)
            {
                var list = new List<Action<CommonDevice>>();
                foreach (string keys in this.dicDeviceEvent.Keys)
                try
                {
                    if (this.dicCommandDiv[keys] != comand)
                    foreach (string keys in this.dicDeviceEvent.Keys)
                    {
                        //命令区分不一致,则不调用回调函数
                        continue;
                        if (this.dicCommandDiv[keys] != comand)
                        {
                            //命令区分不一致,则不调用回调函数
                            continue;
                        }
                        //命令区分一致时,则调用回调函数
                        list.Add(this.dicDeviceEvent[keys]);
                    }
                    //命令区分一致时,则调用回调函数
                    list.Add(this.dicDeviceEvent[keys]);
                }
                catch { return; }
                //有可能在回调函数中移除了事件,导致报错,所以先收集,再调用
                foreach (var action in list)
                {
                    Application.RunOnMainThread(() =>
                    try
                    {
                        try
                        {
                            action?.Invoke(common);
                        }
                        catch (Exception ex)
                        {
                            //Log出力
                            string msg = "推送错误! 当前激活的界面[" + UserCenterResourse.NowActionFormID + "]";
                            HdlLogLogic.Current.WriteLog(ex, msg);
                        }
                    });
                        action?.Invoke(common);
                    }
                    catch (Exception ex)
                    {
                        //Log出力
                        string msg = "推送错误! 当前激活的界面[" + UserCenterResourse.NowActionFormID + "]";
                        HdlLogLogic.Current.WriteLog(ex, msg);
                    }
                }
            }
        }
@@ -567,6 +898,10 @@
        /// <summary>
        /// 设备在线上报
        /// </summary>
        A设备在线上报 = 3
        A设备在线上报 = 3,
        /// <summary>
        /// 当客户端发送控制设备指令,如打开或关闭设备、调节亮度、颜色。如果被控制的节点设备在线,节点设备将反馈
        /// </summary>
        A节点控制反馈 = 4
    }
}