From e87985ec1dcb69beedaf9f95e8e7aba14b7c08d6 Mon Sep 17 00:00:00 2001 From: JLChen <551775569@qq.com> Date: 星期三, 09 十二月 2020 18:50:14 +0800 Subject: [PATCH] 2020-12-09 1.信息中心,增加下划线和点击Alter提醒详情。 --- HDL_ON/DAL/Mqtt/MqttClient.cs | 150 +++++++++++++++++++------------------------------ 1 files changed, 59 insertions(+), 91 deletions(-) diff --git a/HDL_ON/DAL/Mqtt/MqttClient.cs b/HDL_ON/DAL/Mqtt/MqttClient.cs index f65e6ce..321d8b1 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,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 { @@ -21,7 +21,7 @@ /// </summary> static string mqttEncryptKey = ""; //static string checkGatewayTopicBase64 = ""; - + static bool hadGateway = true; /// <summary> /// 鎸や笅绾夸富棰� /// </summary> @@ -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> @@ -143,18 +132,12 @@ static void InitMqtt() { new System.Threading.Thread(async () => { - while (true) + while (hadGateway) { 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 +164,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 }; @@ -200,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; @@ -210,10 +195,8 @@ MqttRemoteSend(new byte[] { 0 }, 3); //杩炴帴鎴愬姛鍚庢娴嬫槸鍚﹂渶瑕侀�氳繃杩滅▼鑾峰彇Key - CheckIfNeedGetLocalPasswordFromRemote(); + //CheckIfNeedGetLocalPasswordFromRemote(); } - - } catch (Exception ex) { @@ -222,15 +205,11 @@ } } - static DateTime mFlagDateTime; - - //static readonly object SendLocker = new object (); /// <summary> /// 鍚姩杩滅▼Mqtt /// </summary> public static async Task StartCloudMqtt() { - if (OnAppConfig.Instance.internetStatus == 0) { return; @@ -253,13 +232,14 @@ try { #region 鍒濆鍖栬繙绋婱qtt - + remoteMqttIsConnecting = true; RemoteMqttClient = new MqttFactory().CreateMqttClient(); //(1)褰揫杩炴帴浜戠鐨凪qtt鎴愬姛鍚嶿鎴栬�匸浠ュ強鍚庨潰App閫氳繃浜戠Mqtt杞彂鏁版嵁缁欑綉鍏虫垚鍔熷悗],澶勭悊鎺ユ敹鍒颁簯绔暟鎹寘鍝嶅簲鏃跺湪mqttServerClient_ApplicationMessageReceived杩欎釜鏂规硶澶勭悊 if (RemoteMqttClient.ApplicationMessageReceivedHandler == null) { + //澶勭悊鎺ユ敹鍒扮殑鏁版嵁 RemoteMqttClient.UseApplicationMessageReceivedHandler((e) => { try { @@ -270,36 +250,46 @@ //鏂版尋涓嬬嚎涓婚鏂规 鏀跺埌鎸や笅绾夸富棰� 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)) + Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.residenceData.HomeGateway.aesKey); + //bus鏁版嵁瑙f瀽 + if (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鍗忚鏁版嵁澶勭悊 + var revString = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); + Control.Ins.UpdataFunctionStatus(revString); } - packet.Manager(); } } catch { } @@ -313,7 +303,6 @@ Utlis.WriteLine($"杩滅▼杩炴帴鏂紑"); isSubscribeSuccess = false; await DisConnectRemoteMqttClient("UseDisconnectedHandler"); - }); } //(3)ConnectedHandler @@ -323,11 +312,7 @@ IfNeedReadAllDeviceStatus = true; Utlis.WriteLine($"============>Mqtt杩滅▼杩炴帴鎴愬姛"); SendPushSignOut(); - - //if (Control.Ins.IsRemote) { - // //Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); - // MainPage.AddTip (Language.StringByID (SimpleControl.R.MyInternationalizationString.LinkSuccess)); - //} + FunctionList.List.ReadAllFunctionStatus(); }); } #endregion @@ -445,8 +430,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; @@ -476,7 +460,6 @@ { remoteIsConnected = true; IsDisConnectingWithSendCatch = false; - UnsupportedProtocolVersionCount = 0; } else { @@ -488,22 +471,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 +586,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 +594,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; @@ -664,7 +634,8 @@ { if (Control.Ins.IsRemote) { - Control.Ins.GatewayOnline = true; + if (!Control.Ins.GatewayOnline) + Control.Ins.GatewayOnline = true; } } @@ -675,30 +646,27 @@ /// <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("============>杩樻病缁戝畾缃戝叧"); + hadGateway = false; } } - else - { - Utlis.WriteLine("============>MqttInfo null"); - } } + } } -- Gitblit v1.8.0