From bb6ad792b598927a5459a5fb6f6c27fb1aa9e94e Mon Sep 17 00:00:00 2001 From: wei <kaede@kaededeMacBook-Air.local> Date: 星期四, 17 十二月 2020 14:06:36 +0800 Subject: [PATCH] 20201217-1 --- HDL_ON/DAL/Mqtt/MqttClient.cs | 93 ++++++++++++++++++++++++++++++---------------- 1 files changed, 60 insertions(+), 33 deletions(-) diff --git a/HDL_ON/DAL/Mqtt/MqttClient.cs b/HDL_ON/DAL/Mqtt/MqttClient.cs index dba1acf..b9b58d5 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,9 +8,9 @@ using MQTTnet; using System.Text; using System.Security.Cryptography; -using System.IO; using HDL_ON.DriverLayer; using HDL_ON.Entity; +using HDL_ON.UI; namespace HDL_ON.DAL.Mqtt { @@ -22,7 +21,7 @@ /// </summary> static string mqttEncryptKey = ""; //static string checkGatewayTopicBase64 = ""; - + static bool hadGateway = true; /// <summary> /// 鎸や笅绾夸富棰� /// </summary> @@ -133,7 +132,7 @@ static void InitMqtt() { new System.Threading.Thread(async () => { - while (true) + while (hadGateway) { try { @@ -168,7 +167,7 @@ //2020-05-14 璁㈤槄涓婚璐ㄩ噺鏀逛负0 var topicFilterBusGateWayToClient = new MqttTopicFilter() { - Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/#", + Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/#", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce }; @@ -180,8 +179,14 @@ //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce }; + var topicAlinkStatus = new MqttTopicFilter() + { + Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/app/thing/property/send", + 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 , topicAlinkStatus }); if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0) { isSubscribeSuccess = true; @@ -205,8 +210,7 @@ /// </summary> public static async Task StartCloudMqtt() { - - if (OnAppConfig.Instance.internetStatus == 0) + if (MainPage.InternetStatus == 0) { return; } @@ -228,7 +232,7 @@ try { #region 鍒濆鍖栬繙绋婱qtt - + remoteMqttIsConnecting = true; RemoteMqttClient = new MqttFactory().CreateMqttClient(); @@ -246,17 +250,17 @@ //鏂版尋涓嬬嚎涓婚鏂规 鏀跺埌鎸や笅绾夸富棰� ReceiveNotifySqueezeAsync(mMes); } - else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyBusGateWayInfoChange") + else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/NotifyBusGateWayInfoChange") {//缃戝叧涓婄嚎锛岄渶瑕佹洿鏂癮eskey //鏀跺埌缃戝叧涓婄嚎娑堟伅涓婚 ReceiveNotifyBusGateWayInfoChange(); } - else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyGateWayOffline") + else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/NotifyGateWayOffline") {//缃戝叧鎺夌嚎 //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛� ReceiveNotifyGateWayOffline(); } - else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway") + else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway") { var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); ReceiveCheckGateway(ss); @@ -264,7 +268,10 @@ else { SetGatewayOnlineResetCheck(); - if (Entity.DB_ResidenceData.residenceData.GatewayType == 0) + var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.Instance.HomeGateway.aesKey); + + //bus鏁版嵁瑙f瀽 + if (DB_ResidenceData.Instance.GatewayType == 0) { var packet = new Packet(); @@ -281,6 +288,8 @@ else { //A鍗忚鏁版嵁澶勭悊 + var revString = Encoding.UTF8.GetString(bytes); + Control.Ins.UpdataFunctionStatus(revString); } } } @@ -301,14 +310,27 @@ if (RemoteMqttClient.ConnectedHandler == null) { RemoteMqttClient.UseConnectedHandler(async (e) => { + if (DB_ResidenceData.Instance.HomeGateway.gatewayStatus) + { + Control.Ins.GatewayOnline = true; + } + else + { + //new System.Threading.Thread(() => { + // while(true) + // { + // System.Threading.Thread.Sleep(1000); + // if (DB_ResidenceData.Instance.HomeGateway.GatewayOnline) + // { + // Control.Ins.GatewayOnline = true; + // break; + // } + // } + //}) { IsBackground = true }.Start(); + } IfNeedReadAllDeviceStatus = true; Utlis.WriteLine($"============>Mqtt杩滅▼杩炴帴鎴愬姛"); SendPushSignOut(); - - //if (Control.Ins.IsRemote) { - // //Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); - // MainPage.AddTip (Language.StringByID (SimpleControl.R.MyInternationalizationString.LinkSuccess)); - //} }); } #endregion @@ -365,7 +387,7 @@ { try { - if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == Entity.DB_ResidenceData.residenceData.residenceGatewayMAC) + if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == Entity.DB_ResidenceData.Instance.residenceGatewayMAC) { await MQTTConnectAsync(); } @@ -426,8 +448,7 @@ //url = HttpUtil.GetProxyEMQUrl (url); //#if DEBUG - // url = HttpUtil.GetProxyEMQUrl (url); - + //url = HttpUtil.GetProxyEMQUrl (url); //#endif var clientId = MqttInfoConfig.Current.mMqttInfo.clientId; var username = MqttInfoConfig.Current.mMqttInfo.userName; @@ -519,7 +540,9 @@ /// </summary> static void ReceiveNotifySqueezeAsync(string mMes) { - +#if DEBUG + return; +#endif if (mMes == PushSignStr) return;//鏄嚜宸辩殑鐧诲綍鎺ㄩ�佷笉澶勭悊 //鏂紑杩滅▼杩炴帴 @@ -531,13 +554,15 @@ DisConnectRemoteMqttClient("鎸や笅绾�"); - UserInfo.Current.LastTime = DateTime.MinValue; - UserInfo.Current.SaveUserInfo(); + HDLCommon.Current.CheckLogout(); - Application.RunOnMainThread(() => { - MainPage.GoLoginPage(UserInfo.Current); - //寮圭獥鎻愮ず琚尋涓嬬嚎 - }); + //UserInfo.Current.LastTime = DateTime.MinValue; + //UserInfo.Current.SaveUserInfo(); + + //Application.RunOnMainThread(() => { + // MainPage.GoLoginPage(UserInfo.Current); + // //寮圭獥鎻愮ず琚尋涓嬬嚎 + //}); //2020-08-11 鍒犻櫎鎺ㄩ�佹暟鎹� //HDLRequest.Current.PushserivceSignOut (); @@ -583,7 +608,7 @@ switch (optionType) { case 0: - topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/ON"; + topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/ON"; if (!string.IsNullOrEmpty(mqttEncryptKey)) { message = Securitys.EncryptionService.AesEncryptPayload(message, mqttEncryptKey); @@ -591,7 +616,7 @@ await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); break; case 3: - topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway"; + topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway"; Utlis.WriteLine("CheckGateway"); await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); break; @@ -631,7 +656,8 @@ { if (Control.Ins.IsRemote) { - Control.Ins.GatewayOnline = true; + if (!Control.Ins.GatewayOnline) + Control.Ins.GatewayOnline = true; } } @@ -648,16 +674,17 @@ MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj; await MQTTConnectAsync(); //1.鍒ゆ柇鏄惁缁戝畾浜嗙綉鍏筹紝鑾峰彇缃戝叧杩滅▼杩炴帴鐨勫姞瀵咾EY - if (DB_ResidenceData.residenceData.CheckWhetherGatewayIsBound()) + if (DB_ResidenceData.Instance.CheckWhetherGatewayIsBound()) { //2.鎵惧嚭鏄惁瀛樺湪鍖归厤褰撳墠浣忓畢鐨刴ac锛屽瓨鍦ㄥ啀杩涜杩滅▼銆� - MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.residenceData.HomeGateway; + MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.Instance.HomeGateway; //3.寮�濮嬭繛鎺� await MQTTConnectAsync(); } else { Utlis.WriteLine("============>杩樻病缁戝畾缃戝叧"); + hadGateway = false; } } } -- Gitblit v1.8.0