From 9137222ece9b978eb97b5598f2eee6e070bcf9d6 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期五, 28 二月 2020 15:22:16 +0800
Subject: [PATCH] 2020-02-28 更新

---
 Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs |  394 +++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 250 insertions(+), 144 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
index 7d6ebf0..4d25cae 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
+++ b/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>
         /// 鎵嬫満鏍囪瘑
@@ -41,21 +42,103 @@
         //static bool thisShowTip = true;
         static string mqttRequestParToken = "";
 
-        /// <summary>
-        /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
+        static MqttCommon ()
+        {
+            InitMqtt ();
+        }
+
+        /// <summary>
+        /// 淇濇椿閲嶈繛鍜岄噸璁㈤槄 绾跨▼
         /// </summary>
-        public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "")
-        {
-            try {
-                if (remoteIsConnected) {
-                    remoteIsConnected = false;
-                    System.Console.WriteLine ($"============>MqttRemote涓诲姩鏂紑_{s}");
-                    //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
-                    await RemoteMqttClient.DisconnectAsync ();
-                }
+        /// <returns></returns>
+        public static async System.Threading.Tasks.Task InitMqtt ()
+        {
+            new System.Threading.Thread (async () => {
+                while (true) {
+                    System.Threading.Thread.Sleep (100);
+                    if (!CommonPage.IsRemote) continue;
+
+                    await StartCloudMqtt ();
+                    await CheckingSubscribeTopics ();
+
+                }
+            }) { IsBackground = true }.Start ();
+        }
+
+        /// <summary>
+        /// 鏄惁璁㈤槄鎴愬姛
+        /// </summary>
+        static bool isSubscribeTopicSuccess = false;
+        /// <summary>
+        /// 妫�鏌ヤ富棰樻槸鍚﹁闃呭け璐�
+        /// </summary>
+        /// <returns></returns>
+        static async Task CheckingSubscribeTopics ()
+        {
+            if (!remoteIsConnected) {
+                return;
+            }
+            try {
+                if (!isSubscribeTopicSuccess) {
+
+                    var topicFilterCommon = new TopicFilter () {
+                        Topic = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/Common/#",
+                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
+                    };
+
+                    //缃戝叧閲嶆柊鐧诲綍涓婚
+                    var topicFilterGateWayInfoChange = new TopicFilter () {
+                        Topic = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyBusGateWayInfoChange",
+                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
+                    };
+
+                    //鎸や笅绾夸富棰�
+                    var topicFilterNotifySqueeze = new TopicFilter () {
+                        Topic = $"/BusGateWayToClient/{mMqttInfo.connEmqClientId}/Push/NotifySqueeze",
+                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
+                    };
+
+                    //缃戝叧鎺夌嚎涓婚
+                    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;
+                    }
+                }
             } catch (Exception e) {
-                System.Console.WriteLine ($"============>MqttRemote鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
-            }
+               
+            }
+        }
+
+
+
+        /// <summary>
+        /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
+        /// </summary>
+        public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "")
+        {
+            try {
+                if (remoteIsConnected) {
+                    remoteIsConnected = false;
+                    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);
+                    }
+                    System.Console.WriteLine ($"============>MqttRemoteDisConnectRemoteMqttClient");
+
+                }
+            } catch (Exception e) {
+                System.Console.WriteLine ($"============>MqttRemote鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
+            }
         }
         static DateTime dateTime = DateTime.MinValue;
 
@@ -79,29 +162,19 @@
         public static async System.Threading.Tasks.Task StartCloudMqtt ()
         {
 
-            //Application.RunOnMainThread (() => {
-            //    if (5 < (DateTime.Now - dateTime).TotalSeconds) {
-            //        return;
-            //    }
-            //    //MainPage.Loading.Start (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.Connecting));
-            //    dateTime = DateTime.Now;
-            //});
             if (!MainPage.LoginUser.IsLogin) {
                 return;
             }
             //杩藉姞锛氭病鏈夎繙绋嬭繛鎺ョ殑鏉冮檺
-            if (remoteMqttIsConnecting
-               || remoteIsConnected) {
-                System.Console.WriteLine ($"============>MqttremoteMqttIsConnecting:{remoteMqttIsConnecting}  remoteIsConnected:{remoteIsConnected} ");
-
+            if (remoteMqttIsConnecting || remoteIsConnected) {
                 return;
             }
 
+            remoteMqttIsConnecting = true;
             await System.Threading.Tasks.Task.Factory.StartNew (async () => {
                 try {
-                    lock (RemoteMqttClient) {
+                    //lock (RemoteMqttClient) {
                         //琛ㄧず鍚庨潰灏嗚繘琛岃繛鎺�
-                        remoteMqttIsConnecting = true;
 
                         #region 鍒濆鍖栬繙绋婱qtt
                         //(3)褰揫杩炴帴浜戠鐨凪qtt鎴愬姛鍚嶿鎴栬�匸浠ュ強鍚庨潰App閫氳繃浜戠Mqtt杞彂鏁版嵁缁欑綉鍏虫垚鍔熷悗],澶勭悊鎺ユ敹鍒颁簯绔暟鎹寘鍝嶅簲鏃跺湪mqttServerClient_ApplicationMessageReceived杩欎釜鏂规硶澶勭悊
@@ -119,6 +192,8 @@
                                     //2020-01-11 淇敼璁㈤槄涓婚鍦板潃
                                     if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyBusGateWayInfoChange") {//缃戝叧涓婄嚎锛岄渶瑕佹洿鏂癮eskey                                                                                                                     //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛�
                                         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") {
@@ -175,6 +250,7 @@
                         if (RemoteMqttClient.DisconnectedHandler == null) {
                             RemoteMqttClient.UseDisconnectedHandler (async (e) => {
                                 System.Console.WriteLine ($"============>Mqtt杩滅▼杩炴帴鏂紑");
+                                isSubscribeTopicSuccess = false;
                                 await DisConnectRemoteMqttClient ("StartRemoteMqtt.DisconnectedHandler");
                                 //await StartRemoteMqtt();
                                 //if (thisShowTip) {
@@ -190,29 +266,43 @@
                         }
                         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");
-                                        //Application.RunOnMainThread (() => {
-                                        //    MainPage.Loading.Hide ();
-                                        //});
                                     } 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);
+                                    //}
                                 }
+
 
                             });
                         }
                         #endregion
-                    }
-                    try {
+                    //}
 
                         mqttRequestParToken = MainPage.LoginUser.LoginTokenString;
                         //--绗竴姝ワ細鑾峰彇mqtt閾炬帴鍙傛暟
@@ -271,11 +361,6 @@
                             }
                         }
 
-
-
-                    } catch (Exception ex) {
-                        Console.WriteLine (ex.Message);
-                    }
                 } catch (Exception ex) {
                     System.Console.WriteLine ($"============>Mqtt 杩滅▼杩炴帴閫氳杩炴帴鍑哄紓甯�:{ex.Message}");
                 } finally {
@@ -310,139 +395,120 @@
                 await DisConnectRemoteMqttClient ("StartRemoteMqtt");
                 await RemoteMqttClient.ConnectAsync (options1);
                 remoteIsConnected = true;
-                await MqttRemoteSend (new byte [] { 0 }, 1);
-                await MqttRemoteSend (new byte [] { 0 }, 2);
-                await MqttRemoteSend (new byte [] { 0 }, 4);
+                IsDisConnectingWithSendCatch = false;
+                //await MqttRemoteSend (new byte [] { 0 }, 1);
+                //await MqttRemoteSend (new byte [] { 0 }, 2);
+                //await MqttRemoteSend (new byte [] { 0 }, 4);
 
             }
         }
 
-        //public static async System.Threading.Tasks.Task InitMqtt () {
 
-        //    while (true) {
-        //        await StartCloudMqtt ();
-        //        System.Threading.Thread.Sleep (100);
+        ///// <summary>
+        ///// 
+        ///// </summary>
+        ///// <param name="message">闄勫姞鏁版嵁鍖�</param>
+        ///// <param name="optionType">鎿嶄綔绫诲瀷锛�0=缃戝叧鎺у埗锛�1=璁㈤槄缃戝叧鏁版嵁;2=璁㈤槄缃戝叧涓婄嚎鏁版嵁;3=璁㈤槄缃戝叧鏄惁鍦ㄧ嚎涓婚  4=璁㈤槄鎸や笅绾夸富棰� </param>
+        ///// <returns></returns>
+        //public static async Task MqttRemoteSend (byte [] message, int optionType = 0)
+        //{
+        //    try {
+        //        if (!remoteIsConnected) {
+        //            System.Console.WriteLine ($"============>Mqtt 鏈繛鎺� 鍙栨秷鍙戦��");
+        //            return;
+        //        }
+
+        //        var topicName = @"/" + MainPage.LoginUser.AccountString.ToLower () + @"/" + UserConfig.Instance.GatewayMAC.Replace (".", "") + @"/" + currentGuid;
+        //        switch (optionType) {
+        //        case 0:
+        //            var messageSend = message;
+        //            if (string.IsNullOrEmpty (mqttEncryptKey)) {
+        //                topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/OldON";
+        //            } else {
+        //                topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/NewON";
+        //                messageSend = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey);
+        //            }
+        //            //base64鍔犲瘑
+        //            var m = new MqttApplicationMessage { Topic = topicName, Payload = messageSend, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce };
+        //            //if (remoteIsConnected) {
+        //            try {
+        //                RemoteMqttClient.PublishAsync (m);
+        //            } catch (Exception e) {
+        //                //await DisConnectRemoteMqttClient (e.Message);
+        //                //await StartCloudMqtt ();
+        //                //if (remoteIsConnected) {
+        //                //    RemoteMqttClient.PublishAsync (m);
+        //                //}
+        //            }
+        //            //}
+        //            break;
+        //        case 3:
+        //            topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/CheckGateway";
+
+        //            var m1 = new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce };
+
+        //            try {
+        //                Console.WriteLine ("CheckGateway");
+        //                RemoteMqttClient.PublishAsync (m1);
+        //            } catch (Exception e) {
+        //                Console.WriteLine ($"CheckGateway Fail:{e.Message}");
+        //                //await DisConnectRemoteMqttClient (e.Message);
+        //                //await StartCloudMqtt ();
+        //            }
+        //            break;
+        //        }
+
+
+        //    } catch (Exception e) {
+
         //    }
         //}
-
 
         /// <summary>
         /// 
         /// </summary>
         /// <param name="message">闄勫姞鏁版嵁鍖�</param>
-        /// <param name="optionType">鎿嶄綔绫诲瀷锛�0=缃戝叧鎺у埗锛�1=璁㈤槄缃戝叧鏁版嵁;2=璁㈤槄缃戝叧涓婄嚎鏁版嵁;3=璁㈤槄缃戝叧鏄惁鍦ㄧ嚎涓婚  4=璁㈤槄鎸や笅绾夸富棰� </param>
+        /// <param name="optionType">鎿嶄綔绫诲瀷锛�0=缃戝叧鎺у埗锛�1=璁㈤槄缃戝叧鏁版嵁;2=璁㈤槄缃戝叧涓婄嚎鏁版嵁</param>
         /// <returns></returns>
         public static async Task MqttRemoteSend (byte [] message, int optionType = 0)
         {
             try {
-                var topicName = @"/" + MainPage.LoginUser.AccountString.ToLower () + @"/" + UserConfig.Instance.GatewayMAC.Replace (".", "") + @"/" + currentGuid;
+                //if (!remoteIsConnected) {
+                //    System.Console.WriteLine ($"============>Mqtt 鏈繛鎺� 鍙栨秷鍙戦��");
+                //    return;
+                //}
+
+                string topicName;
                 switch (optionType) {
                 case 0:
-                    var messageSend = message;
-                    if (string.IsNullOrEmpty (mqttEncryptKey)) {
-                        topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/OldON";
-                    } else {
-                        topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/NewON";
-                        messageSend = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey);
+                    topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/ON";
+                    if (!string.IsNullOrEmpty (mqttEncryptKey)) {
+                        message = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey);
                     }
-                    //base64鍔犲瘑
-                    var m = new MqttApplicationMessage { Topic = topicName, Payload = messageSend, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce };
-                    //if (remoteIsConnected) {
-                    try {
-                         RemoteMqttClient.PublishAsync (m);
-                    } catch (Exception e) {
-                        await DisConnectRemoteMqttClient (e.Message);
-                        await StartCloudMqtt ();
-                        if (remoteIsConnected) {
-                            RemoteMqttClient.PublishAsync (m);
-                        }
-                    }
-                    //}
+                    await RemoteMqttClient.PublishAsync (new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce });
                     break;
-                case 1:
-                    topicName = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/Common/#";
-                    //if (remoteIsConnected) {
-                    try {
-                        await RemoteMqttClient.SubscribeAsync (topicName);
-                    } catch (Exception e) {
-                        await DisConnectRemoteMqttClient (e.Message);
-                        await StartCloudMqtt ();
-                        if (remoteIsConnected) {
-                            await RemoteMqttClient.SubscribeAsync (topicName);
-                        }
-                    }
-                    // }
-                    break;
-                case 2:
-                    var macStr = CurRemoteMACInfo.mac.ToUpper ();
-                    char [] cArrs = macStr.ToCharArray ();
-                    Array.Reverse (cArrs);
-                    var sss = string.Join (string.Empty, cArrs);
-
-                    using (var provider = new MD5CryptoServiceProvider ()) {
-                        byte [] buffer = provider.ComputeHash (Encoding.Default.GetBytes (sss));
-                        StringBuilder builder = new StringBuilder ();
-                        for (int i = 0; i < buffer.Length; i++) {
-                            builder.Append (buffer [i].ToString ("x2"));
-                        }
-                        CurRemoteMACInfo.md5_mac_string = builder.ToString ().ToUpper ();
-                    }
-
-                    //topicName = $"/NotifyBusGateWayInfoChagne/{CurRemoteMACInfo.md5_mac_string}";
-                    //2020-01-11 淇敼璁㈤槄涓婚鍦板潃
-                    topicName = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/NotifyBusGateWayInfoChange";
-                    //if (remoteIsConnected) {
-                    try {
-                        await RemoteMqttClient.SubscribeAsync (topicName);
-                    } catch (Exception e) {
-                        await DisConnectRemoteMqttClient (e.Message);
-                        await StartCloudMqtt ();
-                        if (remoteIsConnected) {
-                            await RemoteMqttClient.SubscribeAsync (topicName);
-                        }
-                    }
-                    //}
-                    break;
-
                 case 3:
                     topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/CheckGateway";
-
-                    var m1 = new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce };
-
-                    try {
-                        Console.WriteLine ("CheckGateway");
-                        await RemoteMqttClient.PublishAsync (m1);
-                    } catch (Exception e) {
-                        Console.WriteLine ($"CheckGateway Fail:{e.Message}");
-                        await DisConnectRemoteMqttClient (e.Message);
-                        await StartCloudMqtt ();
-                    }
-                    break;
-
-                case 4:
-                    //2020-01-13 淇敼鎸や笅绾夸富棰�
-                    topicName = $"/BusGateWayToClient/{mMqttInfo.connEmqClientId}/Push/NotifySqueeze";
-                    //if (remoteIsConnected) {
-                    try {
-                        await RemoteMqttClient.SubscribeAsync (topicName);
-                    } catch (Exception e) {
-                        await DisConnectRemoteMqttClient (e.Message);
-                        await StartCloudMqtt ();
-                        if (remoteIsConnected) {
-                            await RemoteMqttClient.SubscribeAsync (topicName);
-                        }
-                    }
-
-                    //}
+                    Console.WriteLine ("CheckGateway");
+                    await RemoteMqttClient.PublishAsync (new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce });
                     break;
                 }
-
-
             } catch (Exception e) {
+                // System.Console.WriteLine ($"============>Mqtt MqttRemoteSend catch");
+                 if (!IsDisConnectingWithSendCatch) {
+                    IsDisConnectingWithSendCatch = true;
+                    await DisConnectRemoteMqttClient ("MqttRemoteSendCatch");
+                }
 
             }
         }
 
+        /// <summary>
+        /// SendCatch 鍚庢墽琛屼竴娆℃柇寮�鎿嶄綔
+        /// </summary>
+        static bool IsDisConnectingWithSendCatch = false;
+
+     
 
         /// <summary>
         /// 鍒嗕韩浣忓畢 鑾峰彇褰撳墠浣忓畢缃戝叧淇℃伅骞朵笖杩炴帴MQTT 鎴栬�呭埛鏂�  
@@ -502,12 +568,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) {
 
@@ -527,6 +614,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>
         /// 鏀跺埌鎸や笅绾挎帹閫�
@@ -549,11 +654,12 @@
             MainPage.LoginUser.LastTime = DateTime.Now.AddDays (-30);
             MainPage.LoginUser.SaveUserInfo ();
 
-            DisConnectRemoteMqttClient ("LoginOut");
+            DisConnectRemoteMqttClient ("鎸や笅绾�");
 
             MainPage.ShowAlertOnMainThread (ErrorCode.LoginInAnotherDevice);
 
             Application.RunOnMainThread (() => {
+               
                 MainPage.WiFiStatus = "CrabtreeAdd/WiFi.png";
                 UserMiddle.btnLinkStatus.UnSelectedImagePath = MainPage.WiFiStatus;
                 new AccountLogin ().Show ();

--
Gitblit v1.8.0