From 31da051cc6648c750ddeac9878369a84367d6f9a Mon Sep 17 00:00:00 2001 From: wxr <464027401@qq.com> Date: 星期四, 26 十一月 2020 09:29:54 +0800 Subject: [PATCH] 20201126;网关搜索 --- HDL_ON/DAL/Mqtt/MqttClient.cs | 102 +++++++++++++-------------------------------------- 1 files changed, 26 insertions(+), 76 deletions(-) diff --git a/HDL_ON/DAL/Mqtt/MqttClient.cs b/HDL_ON/DAL/Mqtt/MqttClient.cs index f65e6ce..c06c840 100644 --- a/HDL_ON/DAL/Mqtt/MqttClient.cs +++ b/HDL_ON/DAL/Mqtt/MqttClient.cs @@ -51,11 +51,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 +73,7 @@ if (Control.Ins.IsRemote) { - //涓嶆槸鏃犵綉缁� - if (OnAppConfig.Instance.internetStatus != 0) - { - Control.Ins.GatewayOnline = false; - } + Control.Ins.GatewayOnline = false; } } } @@ -95,21 +90,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 +116,6 @@ await DisConnectRemoteMqttClient(s); } - //static DateTime dateTime = DateTime.MinValue; /// <summary> /// 澶栫綉鐨凪QTT鏄惁姝e湪杩炴帴 /// </summary> @@ -148,13 +137,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 +164,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 +189,8 @@ MqttRemoteSend(new byte[] { 0 }, 3); //杩炴帴鎴愬姛鍚庢娴嬫槸鍚﹂渶瑕侀�氳繃杩滅▼鑾峰彇Key - CheckIfNeedGetLocalPasswordFromRemote(); + //CheckIfNeedGetLocalPasswordFromRemote(); } - - } catch (Exception ex) { @@ -222,9 +199,6 @@ } } - static DateTime mFlagDateTime; - - //static readonly object SendLocker = new object (); /// <summary> /// 鍚姩杩滅▼Mqtt /// </summary> @@ -260,6 +234,7 @@ //(1)褰揫杩炴帴浜戠鐨凪qtt鎴愬姛鍚嶿鎴栬�匸浠ュ強鍚庨潰App閫氳繃浜戠Mqtt杞彂鏁版嵁缁欑綉鍏虫垚鍔熷悗],澶勭悊鎺ユ敹鍒颁簯绔暟鎹寘鍝嶅簲鏃跺湪mqttServerClient_ApplicationMessageReceived杩欎釜鏂规硶澶勭悊 if (RemoteMqttClient.ApplicationMessageReceivedHandler == null) { + //澶勭悊鎺ユ敹鍒扮殑鏁版嵁 RemoteMqttClient.UseApplicationMessageReceivedHandler((e) => { try { @@ -276,7 +251,8 @@ ReceiveNotifyBusGateWayInfoChange(); } else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyGateWayOffline") - {//缃戝叧鎺夌嚎 //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛� + {//缃戝叧鎺夌嚎 + //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛� ReceiveNotifyGateWayOffline(); } else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway") @@ -286,20 +262,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 +294,6 @@ Utlis.WriteLine($"杩滅▼杩炴帴鏂紑"); isSubscribeSuccess = false; await DisConnectRemoteMqttClient("UseDisconnectedHandler"); - }); } //(3)ConnectedHandler @@ -476,7 +456,6 @@ { remoteIsConnected = true; IsDisConnectingWithSendCatch = false; - UnsupportedProtocolVersionCount = 0; } else { @@ -488,22 +467,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 +641,14 @@ /// <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) - { - //----绗簩姝ユ壘鍑烘槸鍚﹀瓨鍦ㄥ尮閰嶅綋鍓嶄綇瀹呯殑mac锛屽瓨鍦ㄥ啀杩涜杩滅▼銆� - MqttInfoConfig.Current.HomeGatewayInfo = UserConfig.Instance.GatewayList[0]; - if (MqttInfoConfig.Current.HomeGatewayInfo != null) - { - //----绗笁姝� 寮�濮嬭繛鎺� - await MQTTConnectAsync(); - } - } - else - { - Utlis.WriteLine("============>杩樻病缁戝畾缃戝叧"); - } - } - else - { - Utlis.WriteLine("============>MqttInfo null"); + MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj; + await MQTTConnectAsync(); } } + } } -- Gitblit v1.8.0