From 653f2dbcad10e90f40ab478d5e95c5e1d92e484d Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期二, 01 十二月 2020 17:15:29 +0800
Subject: [PATCH] 20201201
---
HDL_ON/DAL/Mqtt/MqttClient.cs | 105 ++++++++++++++++------------------------------------
1 files changed, 33 insertions(+), 72 deletions(-)
diff --git a/HDL_ON/DAL/Mqtt/MqttClient.cs b/HDL_ON/DAL/Mqtt/MqttClient.cs
index f65e6ce..1682fc1 100644
--- a/HDL_ON/DAL/Mqtt/MqttClient.cs
+++ b/HDL_ON/DAL/Mqtt/MqttClient.cs
@@ -1,7 +1,6 @@
锘�/*
鏇存柊浜咵MQ杩炴帴鏂瑰紡
*/
-using System.Collections.Generic;
using System;
using MQTTnet.Client;
using System.Threading.Tasks;
@@ -9,8 +8,8 @@
using MQTTnet;
using System.Text;
using System.Security.Cryptography;
-using System.IO;
using HDL_ON.DriverLayer;
+using HDL_ON.Entity;
namespace HDL_ON.DAL.Mqtt
{
@@ -51,11 +50,10 @@
/// </summary>
public static IMqttClient RemoteMqttClient = new MqttFactory().CreateMqttClient();
-
/// <summary>
/// 鎺ㄩ�佹爣璇�
/// </summary>
- static string PushSignStr = System.DateTime.Now.Ticks.ToString();
+ static string PushSignStr = DateTime.Now.Ticks.ToString();
/// <summary>
/// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
@@ -74,11 +72,7 @@
if (Control.Ins.IsRemote)
{
- //涓嶆槸鏃犵綉缁�
- if (OnAppConfig.Instance.internetStatus != 0)
- {
- Control.Ins.GatewayOnline = false;
- }
+ Control.Ins.GatewayOnline = false;
}
}
}
@@ -95,21 +89,16 @@
{
try
{
- //if (remoteIsConnected) {
remoteIsConnected = false;
isSubscribeSuccess = false;
Utlis.WriteLine($"RemoteStart涓诲姩鏂紑_{s}");
await RemoteMqttClient.DisconnectAsync();
-
- //}
}
catch (Exception e)
{
Utlis.WriteLine($"RemoteStart鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
}
-
}
-
/// <summary>
/// 鏂紑mqtt杩炴帴
@@ -126,7 +115,6 @@
await DisConnectRemoteMqttClient(s);
}
- //static DateTime dateTime = DateTime.MinValue;
/// <summary>
/// 澶栫綉鐨凪QTT鏄惁姝e湪杩炴帴
/// </summary>
@@ -148,13 +136,7 @@
try
{
System.Threading.Thread.Sleep(500);
- //if (!UserInfo.Current.IsLogin) {
- // continue;
- //}
-
if (!Control.Ins.IsRemote) continue;
-
- //if (BusSocket.IsEnterBackground) continue;
await StartCloudMqtt();
await SubscribeTopics();
@@ -181,10 +163,6 @@
{
try
{
-
- //var topicFilterPush = new TopicFilter { QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce,
- // Topic = $"/BusGateWayToClient/{MqttInfoConfig.Instance.CurRemoteMACInfo.clientId}/Push/NotifySqueeze" };
-
//2020-05-14 璁㈤槄涓婚璐ㄩ噺鏀逛负0
var topicFilterBusGateWayToClient = new MqttTopicFilter()
{
@@ -210,10 +188,8 @@
MqttRemoteSend(new byte[] { 0 }, 3);
//杩炴帴鎴愬姛鍚庢娴嬫槸鍚﹂渶瑕侀�氳繃杩滅▼鑾峰彇Key
- CheckIfNeedGetLocalPasswordFromRemote();
+ //CheckIfNeedGetLocalPasswordFromRemote();
}
-
-
}
catch (Exception ex)
{
@@ -222,9 +198,6 @@
}
}
- static DateTime mFlagDateTime;
-
- //static readonly object SendLocker = new object ();
/// <summary>
/// 鍚姩杩滅▼Mqtt
/// </summary>
@@ -260,6 +233,7 @@
//(1)褰揫杩炴帴浜戠鐨凪qtt鎴愬姛鍚嶿鎴栬�匸浠ュ強鍚庨潰App閫氳繃浜戠Mqtt杞彂鏁版嵁缁欑綉鍏虫垚鍔熷悗],澶勭悊鎺ユ敹鍒颁簯绔暟鎹寘鍝嶅簲鏃跺湪mqttServerClient_ApplicationMessageReceived杩欎釜鏂规硶澶勭悊
if (RemoteMqttClient.ApplicationMessageReceivedHandler == null)
{
+ //澶勭悊鎺ユ敹鍒扮殑鏁版嵁
RemoteMqttClient.UseApplicationMessageReceivedHandler((e) => {
try
{
@@ -276,7 +250,8 @@
ReceiveNotifyBusGateWayInfoChange();
}
else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyGateWayOffline")
- {//缃戝叧鎺夌嚎 //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛�
+ {//缃戝叧鎺夌嚎
+ //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛�
ReceiveNotifyGateWayOffline();
}
else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway")
@@ -286,20 +261,25 @@
}
else
{
-
SetGatewayOnlineResetCheck();
-
- var packet = new Packet();
-
- if (!string.IsNullOrEmpty(mqttEncryptKey))
+ if (Entity.DB_ResidenceData.residenceData.GatewayType == 0)
{
- packet.Bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey);
+ var packet = new Packet();
+
+ if (!string.IsNullOrEmpty(mqttEncryptKey))
+ {
+ packet.Bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey);
+ }
+ else
+ {
+ packet.Bytes = e.ApplicationMessage.Payload;
+ }
+ packet.Manager();
}
else
{
- packet.Bytes = e.ApplicationMessage.Payload;
+ //A鍗忚鏁版嵁澶勭悊
}
- packet.Manager();
}
}
catch { }
@@ -313,7 +293,6 @@
Utlis.WriteLine($"杩滅▼杩炴帴鏂紑");
isSubscribeSuccess = false;
await DisConnectRemoteMqttClient("UseDisconnectedHandler");
-
});
}
//(3)ConnectedHandler
@@ -476,7 +455,6 @@
{
remoteIsConnected = true;
IsDisConnectingWithSendCatch = false;
- UnsupportedProtocolVersionCount = 0;
}
else
{
@@ -488,22 +466,9 @@
catch (Exception ex)
{
- if (ex.Message == MqttCommunicationTimedOutException)
- {
- Console.WriteLine("Connect error TimedOut: " + ex.Message);
- }
- else
- {
- //閲嶆柊涓績鏈嶅姟鍣ㄨ幏鍙栧弬鏁版爣璁�
- MqttInfoConfig.Current.IfGetMqttInfoSuccess = false;
- Console.WriteLine("Connect error: " + ex.Message);
- }
-
- //Console.WriteLine ("Connect error: " + ex.Message);
- if (IfDEBUG)
- {
- MainPage.ShowAlertOnMainThread("Connect error: " + ex.Message);
- }
+ //閲嶆柊涓績鏈嶅姟鍣ㄨ幏鍙栧弬鏁版爣璁�
+ MqttInfoConfig.Current.IfGetMqttInfoSuccess = false;
+ Console.WriteLine("Connect error: " + ex.Message);
}
finally
{
@@ -675,30 +640,26 @@
/// <returns></returns>
static async Task GetMqttInfoAndMQTTConnectAsync()
{
- var mqttInfoRequestResult_Obj =new Server.HttpServerRequest().GetMqttRemoteInfo(GetRandomKey());
+ var mqttInfoRequestResult_Obj = new Server.HttpServerRequest().GetMqttRemoteInfo(GetRandomKey());
if (mqttInfoRequestResult_Obj != null)
{
- MqttInfoConfig.Cur.mMqttInfo = mqttInfoRequestResult_Obj;
- if (UserConfig.Instance.GatewayList != null && UserConfig.Instance.GatewayList.Count > 0)
+ MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj;
+ await MQTTConnectAsync();
+ //1.鍒ゆ柇鏄惁缁戝畾浜嗙綉鍏筹紝鑾峰彇缃戝叧杩滅▼杩炴帴鐨勫姞瀵咾EY
+ if (DB_ResidenceData.residenceData.CheckWhetherGatewayIsBound())
{
- //----绗簩姝ユ壘鍑烘槸鍚﹀瓨鍦ㄥ尮閰嶅綋鍓嶄綇瀹呯殑mac锛屽瓨鍦ㄥ啀杩涜杩滅▼銆�
- MqttInfoConfig.Current.HomeGatewayInfo = UserConfig.Instance.GatewayList[0];
- if (MqttInfoConfig.Current.HomeGatewayInfo != null)
- {
- //----绗笁姝� 寮�濮嬭繛鎺�
- await MQTTConnectAsync();
- }
+ //2.鎵惧嚭鏄惁瀛樺湪鍖归厤褰撳墠浣忓畢鐨刴ac锛屽瓨鍦ㄥ啀杩涜杩滅▼銆�
+ MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.residenceData.HomeGateway;
+ //3.寮�濮嬭繛鎺�
+ await MQTTConnectAsync();
}
else
{
Utlis.WriteLine("============>杩樻病缁戝畾缃戝叧");
}
}
- else
- {
- Utlis.WriteLine("============>MqttInfo null");
- }
}
+
}
}
--
Gitblit v1.8.0