wxr
2023-07-30 d76b0e40e5bb127fd33aa1f7842447a1df0628d5
HDL_ON/DAL/Mqtt/MqttClient.cs
@@ -12,6 +12,7 @@
using HDL_ON.Entity;
using HDL_ON.UI;
using HDL_ON.DAL.Server;
using HDL_ON;
namespace HDL_ON.DAL.Mqtt
{
@@ -110,6 +111,7 @@
            {
                MqttInfoConfig.Current.IfGetMqttInfoSuccess = false;
            }
            Control.Ins.GatewayOnline_Cloud = Control.Ins.GatewayOnline_Local = false;
            await DisConnectRemoteMqttClient(s);
        }
@@ -159,7 +161,7 @@
            StartCloudMqtt();
        }
        static bool isSubscribeSuccess;
        public static bool isSubscribeSuccess;
        /// <summary>
        /// 订阅主题
        /// </summary>
@@ -204,6 +206,13 @@
                        Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/irCodeStudyDone/up",
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
                    };
                    //App订阅群控状态主题
                    var groupControlStatus = new MqttTopicFilter()
                    {
                        Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/device/group/control/property/send",
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
                    };
                    #region 数据更新推送主题
                    //appHomeRefresh:住宅数据刷新通知--杨涛
@@ -254,13 +263,18 @@
                    #endregion
                    Utlis.WriteLine("开始订阅!");
                    var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] {
                    Utlis.WriteLine("开始订阅!");
                     var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] {
                        pirStatus,pirStudy,
                        appDeviceRefresh,appHomeRefresh,appRoomRefresh,residenceChange,
                        topicFilterPush2, topicAlinkStatus ,mqttkeyChange,
                        deviceOnlinePush,
                        securityStatusChange});
                    //订阅群控状态
                    if (DB_ResidenceData.Instance.HomeGateway.isSupportGroupControl)
                    {
                        await RemoteMqttClient.SubscribeAsync(groupControlStatus);
                    }
                    if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0)
                    {
                        isSubscribeSuccess = true;
@@ -275,14 +289,59 @@
        }
        /// <summary>
        /// 订阅绑定第三方iot账号结果
        /// </summary>
        /// <returns></returns>
        public static async Task<bool> SubscribeAsync3tyIotbind()
        {
            var topicFilter = new MqttTopicFilter()
            {
                Topic = $"/user/{UserInfo.Current.ID}/app/third/bind/send",
                QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
            };
            var result = await RemoteMqttClient.SubscribeAsync(topicFilter);
            if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0)
            {
                Utlis.WriteLine("订阅绑定第三方iot账号结果成功!");
                return true;
            }
            return false;
        }
        /// <summary>
        /// 取消订阅绑定第三方iot账号结果
        /// </summary>
        /// <returns></returns>
        public static async Task<bool> UnsubscribeAsync3tyIotbind()
        {
            string[] strs = new string[] {
                $"/user/{UserInfo.Current.ID}/app/third/bind/send"
            };
            var result = await RemoteMqttClient.UnsubscribeAsync(strs);
            if (result.Items[0].ReasonCode == MQTTnet.Client.Unsubscribing.MqttClientUnsubscribeResultCode.Success)
            {
                Utlis.WriteLine("取消订阅绑定第三方iot账号结果成功!");
                return true;
            }
            return false;
        }
        /// <summary>
        /// 绑定第三方平台接收回调事件
        /// </summary>
        public static Action Bind3tyIotAction;
        /// <summary>
        /// 启动远程Mqtt
        /// </summary>
        public static async Task StartCloudMqtt()
        {
            if (MainPage.InternetStatus == 0)
            //没有网络的状态下尝试一下连接mqtt ,安卓的网络状态变化监听有异常,修改底层麻烦
            if (MainPage.InternetStatus == 0 && MainPage.LinkHdlMqttCount > 1)
            {
                return;
            }
            MainPage.LinkHdlMqttCount++;
            if (!UserInfo.Current.IsLogin)
            {
@@ -317,7 +376,7 @@
                            try
                            {
                                var topic = e.ApplicationMessage.Topic;
                                //MainPage.Log($"收到mqtt主题:{topic}");
                                MainPage.Log($"收到mqtt主题:{topic}");
                                //一端口主题处理
                                if (DB_ResidenceData.Instance.GatewayType == 0 && !DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
                                {
@@ -352,8 +411,14 @@
                                    //新挤下线主题方案 收到挤下线主题
                                    ReceiveNotifySqueezeAsync(mMes);
                                }
                                //绑定第三方平台结果通知
                                else if(topic == $"/user/{UserInfo.Current.ID}/app/third/bind/send")
                                {
                                    var revString = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                                    Bind3tyIotAction?.Invoke();
                                }
                                //设备在线离线状态推送
                                else if(topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/son/session/online")
                                else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/son/session/online")
                                {
                                    try
                                    {
@@ -373,7 +438,7 @@
                                        //}
                                    }
                                    catch(Exception ex)
                                    catch (Exception ex)
                                    {
                                    }
@@ -394,10 +459,10 @@
                                }
                                #region 数据更新推送主题
                                //appHomeRefresh:住宅数据刷新通知
                                else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appHomeRefresh/up"
                                        || topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appDeviceRefresh/up")
                                else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appHomeRefresh/up")
                                {
                                    MainPage.Log("住宅数据刷新通知");
                                    System.Threading.Thread.Sleep(2000);
                                    new HttpServerRequest().GetHomePager();
                                }
                                //appRoomRefresh:房间数据刷新通知
@@ -426,48 +491,6 @@
                                else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appDeviceRefresh/up")
                                {
                                    MainPage.Log("设备数据刷新通知");
                                    var deviceResult = new HttpServerRequest().GetDeviceList();
                                    if (deviceResult.Code == StateCode.SUCCESS)
                                    {
                                        MainPage.Log($"读取设备信息成功");
                                        var deviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<DevcieApiPack>(deviceResult.Data.ToString());
                                        if (deviceList == null)
                                        {
                                            deviceList = new DevcieApiPack();
                                        }
                                        string delFile = "";
                                        if (FunctionList.List.GetDeviceFunctionList().Count > 0)
                                        {
                                            for (int i = 0; i < FunctionList.List.GetDeviceFunctionList().Count;)
                                            {
                                                var localFunction = FunctionList.List.GetDeviceFunctionList()[i];
                                                if (SPK.MusicSpkList().Contains(localFunction.spk))
                                                {
                                                    i++;
                                                    continue;
                                                }
                                                var newFunction = deviceList.list.Find((obj) => obj.deviceId == localFunction.deviceId);
                                                if (delFile == localFunction.savePath)
                                                {
                                                    i++;
                                                    continue;
                                                }
                                                delFile = localFunction.savePath;
                                                FunctionList.List.DeleteFunction(localFunction);
                                            }
                                        }
                                        //处理剩下的新增功能
                                        foreach (var newFunction in deviceList.list)
                                        {
                                            newFunction.SaveFunctionFile();
                                            FunctionList.List.IniFunctionList(newFunction.savePath);
                                        }
                                    }
                                    else
                                    {
                                        MainPage.Log($"读取云端设备数据失败:Code:{deviceResult.Code};  Msg:{deviceResult.message}");
                                    }
                                }
                                //网关密钥变化
                                else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/custom/mqtt/secret/change")
@@ -504,41 +527,36 @@
                                                }
                                                HomePage.LoadEvent_RefreshSecurityStatus();
                                            }
                                        }catch(Exception ex)
                                        }
                                        catch (Exception ex)
                                        {
                                            MainPage.Log($"安防远程信息异常:{ex.Message}");
                                        }
                                    }
                                }
                                #endregion
                                //群控状态
                                else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/device/group/control/property/send")
                                {
                                    var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, tuyaEncryptKey);
                                    var revString = Encoding.UTF8.GetString(bytes);
                                    MainPage.Log($"mqtt 群控状态更新:{revString}");
                                    Control.Ins.UpdataGroupControlStatus(revString, null, true);
                                }
                                //A网关设备状态-包含涂鸦设备
                                //Tag 网络状态解析
                                else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/property/send")
                                {
                                    if (Control.Ins.GatewayOnline_Local)//本地链接,除了涂鸦设备数据之外的云端数据不处理
                                    {
                                        if (FunctionList.List.OtherBrandFunction.Count == 0)
                                        {
                                            MainPage.Log($"A协议更新状态:本地链接,除了涂鸦设备数据之外的云端数据不处理...");
                                            return;
                                        }
                                        else
                                        {
                                            var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, tuyaEncryptKey);
                                            var revString = Encoding.UTF8.GetString(bytes);
                                            MainPage.Log($"mqtt 状态更新:{revString}");
                                            Control.Ins.UpdataFunctionStatus(revString, null, true);
                                            //Control.Ins.MsgInfoList.Add($"mqtt 状态更新:{revString}");
                                            //Control.Ins.MsgInfoList.Add(revString + "\r\n");
                                            //if (FunctionList.List.OtherBrandFunction.Find((obj) => obj.sid == updateTemp.sid) == null)
                                            //{
                                            //    return;
                                            //}
                                        }
                                    }
                                    var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, tuyaEncryptKey);
                                    var revString = Encoding.UTF8.GetString(bytes);
                                    MainPage.Log($"mqtt 状态更新:{revString}");
                                    Control.Ins.UpdataFunctionStatus(revString, null, true);
                                    //Control.Ins.MsgInfoList.Add($"mqtt 状态更新:{revString}");
                                    //Control.Ins.MsgInfoList.Add(revString + "\r\n");
                                    //if (FunctionList.List.OtherBrandFunction.Find((obj) => obj.sid == updateTemp.sid) == null)
                                    //{
                                    //    return;
                                    //}
                                }
                                //一端口数据解析
                                else
@@ -580,6 +598,10 @@
                        {
                            IfNeedReadAllDeviceStatus = true;
                            Control.Ins.GatewayOnline_Cloud = true;
                            if(MainPage.InternetStatus == 0)
                            {
                                MainPage.InternetStatus = 1;
                            }
                            Utlis.WriteLine($"============>Mqtt远程连接成功");
                            SendPushSignOut();
                        });
@@ -621,7 +643,7 @@
                }
                catch (Exception ex)
                {
                    Utlis.WriteLine($"error:" + ex.Message);
                    Utlis.WriteLine($"mqtt连接异常 error:" + ex.Message);
                    //mqtt连接异常,清空本地mqtt信息,可能需要重新获取:wxr
                    MqttInfoConfig.Current.Refresh();
                }
@@ -809,12 +831,13 @@
        static void ReceiveNotifySqueezeAsync(string mMes)
        {
#if DEBUG
            return;
            //return;
#endif
            if (mMes == PushSignStr) return;//是自己的登录推送不处理//或者当前不是远程链接状态
            //测试账号,不挤下线
            switch (UserInfo.Current.userMobileInfo)
            {
                case "18316672920":
                case "15626203746":
                case "464027401@qq.com":
                    return;
@@ -1124,7 +1147,7 @@
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MainPage.Log(ex.Message);
            }
            return reData;
        }