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 |  152 +++++++++++++++++++++++++++++++++-----------------
 1 files changed, 99 insertions(+), 53 deletions(-)

diff --git a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
index b949444..4d25cae 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
@@ -119,25 +119,26 @@
 
 
 
-        /// <summary>
-        /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
+        /// <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);
+        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}");
-            }
+                System.Console.WriteLine ($"============>MqttRemote鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
+            }
         }
         static DateTime dateTime = DateTime.MinValue;
 
@@ -267,7 +268,6 @@
                             RemoteMqttClient.UseConnectedHandler (async (e) => {
                                 IfNeedReadAllDeviceStatus = true;
 
-
                                 System.Console.WriteLine ($"============>Mqtt杩滅▼杩炴帴鎴愬姛");
                                 if (CommonPage.IsRemote) {
                                     Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink);
@@ -395,6 +395,7 @@
                 await DisConnectRemoteMqttClient ("StartRemoteMqtt");
                 await RemoteMqttClient.ConnectAsync (options1);
                 remoteIsConnected = true;
+                IsDisConnectingWithSendCatch = false;
                 //await MqttRemoteSend (new byte [] { 0 }, 1);
                 //await MqttRemoteSend (new byte [] { 0 }, 2);
                 //await MqttRemoteSend (new byte [] { 0 }, 4);
@@ -402,67 +403,112 @@
             }
         }
 
-     
+
+        ///// <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 {
-                if (!remoteIsConnected) {
-                    System.Console.WriteLine ($"============>Mqtt 鏈繛鎺� 鍙栨秷鍙戦��");
-                    return;
-                }
+                //if (!remoteIsConnected) {
+                //    System.Console.WriteLine ($"============>Mqtt 鏈繛鎺� 鍙栨秷鍙戦��");
+                //    return;
+                //}
 
-                var topicName = @"/" + MainPage.LoginUser.AccountString.ToLower () + @"/" + UserConfig.Instance.GatewayMAC.Replace (".", "") + @"/" + currentGuid;
+                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 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 ();
-                    }
+                    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 鎴栬�呭埛鏂�  

--
Gitblit v1.8.0