From 026ed1573ad8b9ba28f0e2c0bf0c9cd85f1dd2ed Mon Sep 17 00:00:00 2001 From: JLChen <551775569@qq.com> Date: 星期三, 02 十二月 2020 16:32:00 +0800 Subject: [PATCH] 2020-12-02 1.增加子账号设备分享。 2.房间删除时间优化。3.消息中心接口对接。4.版本检测更新接口对接。 --- HDL_ON/DAL/Mqtt/MqttClient.cs | 117 +++++++++++++++++++--------------------------------------- 1 files changed, 39 insertions(+), 78 deletions(-) diff --git a/HDL_ON/DAL/Mqtt/MqttClient.cs b/HDL_ON/DAL/Mqtt/MqttClient.cs index f65e6ce..ea06685 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,14 +163,10 @@ { 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() { - Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/#", + Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/#", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce }; @@ -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 { @@ -270,36 +244,42 @@ //鏂版尋涓嬬嚎涓婚鏂规 鏀跺埌鎸や笅绾夸富棰� 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); } 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 { @@ -616,7 +581,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); @@ -624,7 +589,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; @@ -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