From d53e6af2c5f17838fa79659614b15a2a1f383399 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期五, 31 三月 2023 10:04:58 +0800
Subject: [PATCH] 1
---
Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs | 104 +++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 83 insertions(+), 21 deletions(-)
diff --git a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
index 1d20d0c..59f4969 100644
--- a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
+++ b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs
@@ -9,6 +9,7 @@
using MQTTnet;
using System.Text;
using System.Security.Cryptography;
+using System.Collections.Generic;
namespace SmartHome
{
@@ -35,6 +36,10 @@
///// </summary>
//static readonly string TopicToApp = "/Push/NotifySqueeze";
+ /// <summary>
+ /// 鏄惁鑾峰彇杩囧瘑閽�
+ /// </summary>
+ static bool isGetGatewaySecretKey = false;
/// <summary>
/// 闅忔満Key
@@ -66,7 +71,7 @@
/// <summary>
/// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
/// </summary>
- public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "")
+ public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "")
{
try {
if (remoteIsConnected) {
@@ -120,6 +125,7 @@
bNeedStartTip = true;
bNeedConnectTip = true;
CommonConfig.Current.IfGetMqttInfoSuccess = false;
+ isGetGatewaySecretKey = false;//鑾峰彇瀵嗛挜鏍囪涓篺alse
}
await DisConnectRemoteMqttClient (s);
}
@@ -246,8 +252,16 @@
//QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
};
+ //褰撹澶嘙QTT鎶ユ枃鍔犲瘑瀵嗛挜鍙戠敓鍙樻洿鏃�,浜戠閫氳繃璇ヤ富棰橀�氱煡鍏朵粬瀹㈡埛绔埛鏂板瘑閽�,鍏跺畠鍜岃缃戝叧浜や簰鐨勫鎴风鑷璁㈤槄璇ヤ富棰�,璇ヤ富棰樺寘涓嶅姞瀵�
+ var topicFilterPush3 = new MqttTopicFilter {
+ Topic = $"/user/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/custom/mqtt/secret/change",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce,
+ };
+
+
+
Utlis.WriteLine ("寮�濮嬭闃咃紒");
- var result = await RemoteMqttClient.SubscribeAsync (new MqttTopicFilter [] { topicFilterBusGateWayToClient, topicFilterPush2 });
+ var result = await RemoteMqttClient.SubscribeAsync (new MqttTopicFilter [] { topicFilterBusGateWayToClient, topicFilterPush2, topicFilterPush3 });
if (result.Items [0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0) {
isSubscribeSuccess = true;
Utlis.WriteLine ("璁㈤槄鎴愬姛锛�");
@@ -321,6 +335,11 @@
} else if (topic == $"/BusGateWayToClient/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/NotifyBusGateWayInfoChange") {//缃戝叧涓婄嚎锛岄渶瑕佹洿鏂癮eskey
//鏀跺埌缃戝叧涓婄嚎娑堟伅涓婚
ReceiveNotifyBusGateWayInfoChange ();
+ } else if (topic == $"/user/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/custom/mqtt/secret/change") {//璁惧瀵嗛挜鏇存柊閫氱煡锛岄渶瑕佹洿鏂癮eskey
+ //璁惧瀵嗛挜鏇存柊閫氱煡
+ HDLUtils.WriteLine ("璁惧瀵嗛挜鏇存柊閫氱煡");
+ isGetGatewaySecretKey = false;//鑾峰彇瀵嗛挜鏍囪涓篺alse
+ GetGatewaySecretKey ();
} else if (topic == $"/BusGateWayToClient/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/NotifyGateWayOffline") {//缃戝叧鎺夌嚎 //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛�
ReceiveNotifyGateWayOffline ();
} else if (topic == $"/BusGateWayToClient/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway") {
@@ -372,13 +391,16 @@
//}
//(4)===========寮�濮嬭繛鎺ヨ繃绋�==========
- //涔嬪墠宸茬粡鑾峰彇鍙傛暟鎴愬姛杩�
- if (CommonConfig.Current.IfGetMqttInfoSuccess) {
- //鍒ゆ柇鏄惁闇�瑕侀噸鏂拌幏鍙�
- await CheckMQTTConnectAsync ();
- } else {
- //寮�濮嬭幏鍙栬繙绋嬭繛鎺ュ弬鏁�
- await StartMQTTGetInfo ();
+ //4.1 濡傛灉娌¤幏鍙栬繃瀵嗛挜鍒欓噸鏂拌幏鍙栦竴娆�, 鑾峰彇涓�娆℃渶鏂扮殑瀵嗛挜
+ if (isGetGatewaySecretKey || GetGatewaySecretKey ()) {
+ //涔嬪墠宸茬粡鑾峰彇鍙傛暟鎴愬姛杩�
+ if (CommonConfig.Current.IfGetMqttInfoSuccess) {
+ //鍒ゆ柇鏄惁闇�瑕侀噸鏂拌幏鍙�
+ await CheckMQTTConnectAsync ();
+ } else {
+ //寮�濮嬭幏鍙栬繙绋嬭繛鎺ュ弬鏁�
+ await StartMQTTGetInfo ();
+ }
}
} catch (Exception ex) {
@@ -665,9 +687,6 @@
//#endif
CheckIfNeedReadAllDeviceStatus ();
}
-
- GetSingleHomeGatewayPaggerAndMQTTConnectAsync (false);
-
} catch { }
}
@@ -747,6 +766,10 @@
/// </summary>
static void SendPushSignOut ()
{
+#if DEBUG
+ return;
+#endif
+
byte [] message = CommonPage.MyEncodingUTF8.GetBytes (PushSignStr);
MqttRemoteSend (message, 4);
}
@@ -767,6 +790,7 @@
case 0:
topicName = $"/ClientToBusGateWay/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/Common/ON";
if (!string.IsNullOrEmpty (mqttEncryptKey)) {
+ //Utlis.WriteLine ("mqttEncryptKey锛�" + mqttEncryptKey);
message = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey);
}
await RemoteMqttClient.PublishAsync (new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce });
@@ -831,7 +855,7 @@
var mqttInfoRequestResult_Obj = HttpServerRequest.Current.GetMqttRemoteInfo (GetRandomKey ());
if (mqttInfoRequestResult_Obj != null) {
CommonConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj;
- if (UserConfig.Instance.CheckHomeGatewaysNotEmpty()) {
+ if (UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) {
//----绗簩姝ユ壘鍑烘槸鍚﹀瓨鍦ㄥ尮閰嶅綋鍓嶄綇瀹呯殑mac锛屽瓨鍦ㄥ啀杩涜杩滅▼銆�
CommonConfig.Current.HomeGatewayInfo = UserConfig.Instance.HomeGateway;
if (CommonConfig.Current.HomeGatewayInfo != null) {
@@ -848,13 +872,51 @@
}
+
/// <summary>
- /// 鍒嗕韩浣忓畢 鑾峰彇褰撳墠浣忓畢缃戝叧淇℃伅骞朵笖杩炴帴MQTT 鎴栬�呭埛鏂�
- /// 2020-03-17
+ /// 璁惧瀵嗛挜鏇存柊閫氱煡锛岄噸鏂版嬁瀵嗛挜
/// </summary>
- static async Task GetSingleHomeGatewayPaggerAndMQTTConnectAsync (bool bNeedConnect = true)
+ /// <returns></returns>
+ static bool GetGatewaySecretKey ()
{
-
+ bool isSuccess = false;
+ try {
+ if (string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.Id)) return false;
+ var nowhomeId = UserConfig.Instance.CurrentRegion.Id;
+ var requestJson = HttpUtil.GetSignRequestJson (new HomeIdObj () { homeId = nowhomeId });
+ var revertObj = HttpUtil.RequestHttpsPostFroHome (NewAPI.API_POST_GetGatewayList, requestJson);
+ if (revertObj.Code == StateCode.SUCCESS) {
+ var mHomeGatewayRes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<HomeGatewayInfo>> (revertObj.Data.ToString ());
+ if (nowhomeId == UserConfig.Instance.CurrentRegion.Id) {
+ if (mHomeGatewayRes != null) {
+ if (mHomeGatewayRes.Count > 0) {
+ //鍙彇绗竴涓綇瀹�
+ var info = mHomeGatewayRes [0];
+ UserConfig.Instance.HomeGateway = info;
+ UserConfig.Instance.SaveUserConfig ();
+ CommonConfig.Current.HomeGatewayInfo = info;
+ //鏇存柊缃戝叧淇℃伅鍜屽姞瀵咾ey
+ mqttEncryptKey = info.aesKey;
+ CommonConfig.Current.Save ();
+ isSuccess = true;
+ isGetGatewaySecretKey = true;//鑾峰彇瀵嗛挜鏍囪涓簍rue
+
+ } else {
+ //鎵撳嵃閿欒
+ HDLUtils.WriteLine ("gateway is not bound");
+ }
+ }
+ }
+ } else {
+ //鎵撳嵃閿欒
+ HDLUtils.WriteLine ("Failed to get gateway info");
+
+ }
+ return isSuccess;
+ } catch {
+ //return "";
+ return isSuccess;
+ }
}
@@ -887,7 +949,7 @@
public partial class EncryptionService
{
- #region 鍔犲瘑
+#region 鍔犲瘑
/// <summary>
/// 鍔犲瘑涓婚涓築ase64
/// </summary>
@@ -942,10 +1004,10 @@
//浣跨敤AES灏嗘槑鏂囨祦杞垚瀵嗘枃瀛楄妭鏁扮粍
return cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
}
- #endregion
+#endregion
- #region 瑙e瘑
+#region 瑙e瘑
/// <summary>
/// 瑙e瘑涓婚鏁版嵁
/// </summary>
@@ -997,7 +1059,7 @@
//浣跨敤AES灏嗗瘑鏂囨祦杞垚鏄庢枃鐨勫瓧鑺傛暟缁�
return cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
}
- #endregion
+#endregion
--
Gitblit v1.8.0