JLChen
2020-02-20 965290476c027deb96891cde1095d9a100581a6b
Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
@@ -21,6 +21,7 @@
        static string checkGatewayTopicBase64 = "";
        static RemoteMACInfo CurRemoteMACInfo = null;
        static MqttInfo mMqttInfo = null;
        public static bool IsGatewayOnline = true;
        /// <summary>
        /// 手机标识
@@ -97,7 +98,15 @@
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
                    };
                    var topicFilters = new TopicFilter [] { topicFilterCommon, topicFilterGateWayInfoChange, topicFilterNotifySqueeze };
                    //网关掉线主题
                    var topicFilterNotifyGateWayOffline = new TopicFilter () {
                        Topic = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyGateWayOffline",
                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
                    };
                    var topicFilters = new TopicFilter [] { topicFilterCommon, topicFilterGateWayInfoChange, topicFilterNotifySqueeze, topicFilterNotifyGateWayOffline };
                    var result = await RemoteMqttClient.SubscribeAsync (topicFilters);
                    if (result.Items [0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS2) {
                        isSubscribeTopicSuccess = true;
@@ -121,6 +130,10 @@
                    System.Console.WriteLine ($"============>MqttRemote主动断开_{s}");
                    //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
                    await RemoteMqttClient.DisconnectAsync ();
                    if (CommonPage.IsRemote) {
                        Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
                    }
                }
            } catch (Exception e) {
                System.Console.WriteLine ($"============>MqttRemote断开通讯连接出异常:{e.Message}");
@@ -178,6 +191,8 @@
                                    //2020-01-11 修改订阅主题地址
                                    if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyBusGateWayInfoChange") {//网关上线,需要更新aeskey                                                                                                                     //----第二步:读取账号下面的网关列表
                                        await ReceiveNotifyBusGateWayInfoChange ();
                                    } else if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyGateWayOffline") {//网关掉线                                                                                                                    //----第二步:读取账号下面的网关列表
                                        ReceiveNotifyGateWayOffline ();
                                    } else if (aesDecryptTopic == $"/BusGateWayToClient/{mMqttInfo.connEmqClientId}/Push/NotifySqueeze") {//订阅挤下线问题
                                        await ReceiveNotifySqueezeAsync (aesDecryptPayload);
                                    }else if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/Common/CheckGateway") {
@@ -250,20 +265,39 @@
                        }
                        if (RemoteMqttClient.ConnectedHandler == null) {
                            RemoteMqttClient.UseConnectedHandler (async (e) => {
                                IfNeedReadAllDeviceStatus = true;
                                Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
                                System.Console.WriteLine ($"============>Mqtt远程连接成功");
                                if (CommonPage.IsRemote) {
                                    Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink);
                                }
                                if (CurRemoteMACInfo != null) {
                                    if (CurRemoteMACInfo.isValid == "InValid") {
                                    IsGatewayOnline = CurRemoteMACInfo.isValid != "InValid";
                                    if (!IsGatewayOnline) {
                                        //网关不在线
                                        if (CommonPage.IsRemote) {
                                            Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline);
                                        }
                                        MainPage.AddTip ("Remote failed,gateway offline");
                                    } else {
                                        //网关在线
                                        //重新一次所有设备状态
                                        IfNeedReadAllDeviceStatus = false;
                                        Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
                                        //
                                        MqttRemoteSend (new byte [] { 0 }, 3);
                                    }
                                    //if (CurRemoteMACInfo.isValid == "InValid") {
                                    //    MainPage.AddTip ("Remote failed,gateway offline");
                                    //} else {
                                    //    MqttRemoteSend (new byte [] { 0 }, 3);
                                    //}
                                }
                            });
                        }
@@ -488,12 +522,33 @@
           
        }
        /// <summary>
        /// 是否需要读取一次所有设备状态
        /// </summary>
        static bool IfNeedReadAllDeviceStatus = true;
        /// <summary>
        /// 收到网关上线消息
        /// </summary>
        static async Task ReceiveNotifyBusGateWayInfoChange ()
        {
            System.Console.WriteLine ("============>Mqtt 网关上线");
            IsGatewayOnline = true;
            if (CommonPage.IsRemote) {
                if (MainPage.WiFiStatus != "CrabtreeAdd/CloudLink.png") {
                    Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink);
                }
//#if DEBUG
                MainPage.AddTip ("Gateway login online");
//#endif
                if (IfNeedReadAllDeviceStatus) {
                    IfNeedReadAllDeviceStatus = false;
                    Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
                }
            }
            //当前住宅不是分享来
            if (!UserConfig.Instance.CurrentRegion.IsOthreShare) {
@@ -513,6 +568,24 @@
            }
        }
        /// <summary>
        /// 收到网关掉线信息
        /// </summary>
        static void ReceiveNotifyGateWayOffline ()
        {
            System.Console.WriteLine ("============>Mqtt GateWayOffline");
            IsGatewayOnline = false;
            if (CommonPage.IsRemote) {
                Utlis.ShowAppLinkStatus (AppLinkStatus.CloudOffline);
                MainPage.AddTip ("Remote failed,gateway offline");
                //if (MainPage.WiFiStatus != "CrabtreeAdd/CloudUnlink.png") {
                //    Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
                //}
            }
        }
        /// <summary>
        /// 收到挤下线推送