From 454ac814944cf956ff02b84b70ba2ec68e5e5ea1 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期三, 15 一月 2020 19:38:47 +0800
Subject: [PATCH] 2020-01-15 1.优化MQTT连接。 2.增加本地通信加密。
---
Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs | 176 ++++++++++++++++++++++++++++------------------------------
1 files changed, 84 insertions(+), 92 deletions(-)
diff --git a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
index 7d6ebf0..2536317 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
@@ -41,6 +41,73 @@
//static bool thisShowTip = true;
static string mqttRequestParToken = "";
+ static MqttCommon ()
+ {
+ InitMqtt ();
+ }
+
+ 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 topicFilters = new TopicFilter [] { topicFilterCommon, topicFilterGateWayInfoChange, topicFilterNotifySqueeze };
+ var result = await RemoteMqttClient.SubscribeAsync (topicFilters);
+ if (result.Items [0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS2) {
+ isSubscribeTopicSuccess = true;
+ }
+ }
+ } catch (Exception e) {
+
+ }
+ }
+
+
+
/// <summary>
/// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
/// </summary>
@@ -92,16 +159,16 @@
//杩藉姞锛氭病鏈夎繙绋嬭繛鎺ョ殑鏉冮檺
if (remoteMqttIsConnecting
|| remoteIsConnected) {
- System.Console.WriteLine ($"============>MqttremoteMqttIsConnecting:{remoteMqttIsConnecting} remoteIsConnected:{remoteIsConnected} ");
+ //System.Console.WriteLine ($"============>MqttremoteMqttIsConnecting:{remoteMqttIsConnecting} remoteIsConnected:{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杩欎釜鏂规硶澶勭悊
@@ -175,6 +242,7 @@
if (RemoteMqttClient.DisconnectedHandler == null) {
RemoteMqttClient.UseDisconnectedHandler (async (e) => {
System.Console.WriteLine ($"============>Mqtt杩滅▼杩炴帴鏂紑");
+ isSubscribeTopicSuccess = false;
await DisConnectRemoteMqttClient ("StartRemoteMqtt.DisconnectedHandler");
//await StartRemoteMqtt();
//if (thisShowTip) {
@@ -211,8 +279,7 @@
});
}
#endregion
- }
- try {
+ //}
mqttRequestParToken = MainPage.LoginUser.LoginTokenString;
//--绗竴姝ワ細鑾峰彇mqtt閾炬帴鍙傛暟
@@ -271,11 +338,6 @@
}
}
-
-
- } catch (Exception ex) {
- Console.WriteLine (ex.Message);
- }
} catch (Exception ex) {
System.Console.WriteLine ($"============>Mqtt 杩滅▼杩炴帴閫氳杩炴帴鍑哄紓甯�:{ex.Message}");
} finally {
@@ -310,22 +372,14 @@
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);
+ //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>
@@ -351,59 +405,14 @@
try {
RemoteMqttClient.PublishAsync (m);
} catch (Exception e) {
- await DisConnectRemoteMqttClient (e.Message);
- await StartCloudMqtt ();
- if (remoteIsConnected) {
- RemoteMqttClient.PublishAsync (m);
- }
+ //await DisConnectRemoteMqttClient (e.Message);
+ //await StartCloudMqtt ();
+ //if (remoteIsConnected) {
+ // RemoteMqttClient.PublishAsync (m);
+ //}
}
//}
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";
@@ -411,29 +420,12 @@
try {
Console.WriteLine ("CheckGateway");
- await RemoteMqttClient.PublishAsync (m1);
+ RemoteMqttClient.PublishAsync (m1);
} catch (Exception e) {
Console.WriteLine ($"CheckGateway Fail:{e.Message}");
- await DisConnectRemoteMqttClient (e.Message);
- await StartCloudMqtt ();
+ //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);
- }
- }
-
- //}
break;
}
--
Gitblit v1.8.0