From 407fae6f07a2a982a2a814c2f145c40733c966cb Mon Sep 17 00:00:00 2001 From: JLChen <551775569@qq.com> Date: 星期三, 12 一月 2022 15:29:34 +0800 Subject: [PATCH] 2022-01-12 1.更新 --- Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs | 106 +++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 83 insertions(+), 23 deletions(-) diff --git a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs index 761d256..59f4969 100644 --- a/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs +++ b/Crabtree/SmartHome/UI/SimpleControl/MqttCommon.cs @@ -1,7 +1,6 @@ 锘�/* 鏇存柊浜咵MQ杩炴帴鏂瑰紡 */ -using System.Collections.Generic; using System; using MQTTnet.Client; using System.Threading.Tasks; @@ -10,7 +9,7 @@ using MQTTnet; using System.Text; using System.Security.Cryptography; -using System.IO; +using System.Collections.Generic; namespace SmartHome { @@ -37,6 +36,10 @@ ///// </summary> //static readonly string TopicToApp = "/Push/NotifySqueeze"; + /// <summary> + /// 鏄惁鑾峰彇杩囧瘑閽� + /// </summary> + static bool isGetGatewaySecretKey = false; /// <summary> /// 闅忔満Key @@ -68,7 +71,7 @@ /// <summary> /// 鏂紑杩滅▼Mqtt鐨勯摼鎺� /// </summary> - static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "") + public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "") { try { if (remoteIsConnected) { @@ -122,6 +125,7 @@ bNeedStartTip = true; bNeedConnectTip = true; CommonConfig.Current.IfGetMqttInfoSuccess = false; + isGetGatewaySecretKey = false;//鑾峰彇瀵嗛挜鏍囪涓篺alse } await DisConnectRemoteMqttClient (s); } @@ -248,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 ("璁㈤槄鎴愬姛锛�"); @@ -323,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") { @@ -374,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) { @@ -667,9 +687,6 @@ //#endif CheckIfNeedReadAllDeviceStatus (); } - - GetSingleHomeGatewayPaggerAndMQTTConnectAsync (false); - } catch { } } @@ -749,6 +766,10 @@ /// </summary> static void SendPushSignOut () { +#if DEBUG + return; +#endif + byte [] message = CommonPage.MyEncodingUTF8.GetBytes (PushSignStr); MqttRemoteSend (message, 4); } @@ -769,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 }); @@ -833,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) { @@ -850,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; + } } @@ -889,7 +949,7 @@ public partial class EncryptionService { - #region 鍔犲瘑 +#region 鍔犲瘑 /// <summary> /// 鍔犲瘑涓婚涓築ase64 /// </summary> @@ -944,10 +1004,10 @@ //浣跨敤AES灏嗘槑鏂囨祦杞垚瀵嗘枃瀛楄妭鏁扮粍 return cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length); } - #endregion +#endregion - #region 瑙e瘑 +#region 瑙e瘑 /// <summary> /// 瑙e瘑涓婚鏁版嵁 /// </summary> @@ -999,7 +1059,7 @@ //浣跨敤AES灏嗗瘑鏂囨祦杞垚鏄庢枃鐨勫瓧鑺傛暟缁� return cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length); } - #endregion +#endregion -- Gitblit v1.8.0