From 8da70e1a39328e1769b02bb1e05303916bb30eb5 Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期一, 01 二月 2021 13:55:31 +0800
Subject: [PATCH] 水阀功能

---
 HDL_ON/DAL/Mqtt/MqttClient.cs |   82 +++++++++++++++++++++++++---------------
 1 files changed, 51 insertions(+), 31 deletions(-)

diff --git a/HDL_ON/DAL/Mqtt/MqttClient.cs b/HDL_ON/DAL/Mqtt/MqttClient.cs
index 2a95958..405c696 100644
--- a/HDL_ON/DAL/Mqtt/MqttClient.cs
+++ b/HDL_ON/DAL/Mqtt/MqttClient.cs
@@ -20,6 +20,7 @@
         /// 鍔犲瘑閫氳KEY
         /// </summary>
         static string mqttEncryptKey = "";
+        static string tuyaEncryptKey = "";
         //static string checkGatewayTopicBase64 = "";
         static bool hadGateway = true;
         /// <summary>
@@ -56,37 +57,37 @@
         /// </summary>
         static string PushSignStr = DateTime.Now.Ticks.ToString();
 
-        /// <summary>
-        /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
+        /// <summary>
+        /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
         /// </summary>
-        static async Task DisConnectRemoteMqttClient(string s = "")
-        {
+        static async Task DisConnectRemoteMqttClient(string s = "")
+        {
             try
-            {
+            {
                 if (remoteIsConnected)
                 {
                     remoteIsConnected = false;
                     isSubscribeSuccess = false;
-                    Utlis.WriteLine($"Remote涓诲姩鏂紑_{s}");
-                    //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
+                    Utlis.WriteLine($"Remote涓诲姩鏂紑_{s}");
+                    //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
                     await RemoteMqttClient.DisconnectAsync();
 
                     if (Control.Ins.IsRemote)
                     {
                         Control.Ins.GatewayOnline = false;
                     }
-                }
+                }
             }
             catch (Exception e)
             {
-                Utlis.WriteLine($"Remote鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
-            }
+                Utlis.WriteLine($"Remote鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
+            }
         }
 
-        /// <summary>
-        /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
+        /// <summary>
+        /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
         /// </summary>
-        static async Task DisConnectRemoteMqttClientWhenStart(string s = "")
+        static async Task DisConnectRemoteMqttClientWhenStart(string s = "")
         {
             try
             {
@@ -97,7 +98,7 @@
             }
             catch (Exception e)
             {
-                Utlis.WriteLine($"RemoteStart鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
+                Utlis.WriteLine($"RemoteStart鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
             }
         }
 
@@ -184,10 +185,14 @@
                         QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce,
                         //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
                     };
-
                     var topicAlinkStatus = new MqttTopicFilter()
                     {
-                        Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/app/thing/property/send",
+                        Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.RegionID}/app/thing/property/send",
+                        QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+                    };
+                    var topicAlinkGatewayStatus = new MqttTopicFilter()
+                    {
+                        Topic = $"/user/{DB_ResidenceData.Instance.HomeGateway.gatewayId}/app/thing/property/send",
                         QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
                     };
                     //var topicBusStatus = new MqttTopicFilter()
@@ -197,7 +202,7 @@
                     //};
 
                     Utlis.WriteLine("寮�濮嬭闃咃紒");
-                    var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2, topicAlinkStatus });
+                    var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2, topicAlinkStatus, topicAlinkGatewayStatus });
                     if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0)
                     {
                         Control.Ins.GatewayOnline = true;
@@ -274,15 +279,22 @@
                                 }
                                 else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway")
                                 {
-                                    
                                     var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                                     ReceiveCheckGateway(ss);
+                                }
+                                //娑傞甫璁惧鐘舵��
+                                else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.RegionID}/app/thing/property/send")
+                                {
+                                    var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, tuyaEncryptKey);
+                                    MainPage.Log($"tuya mqtt A-Link data");
+                                    //A鍗忚鏁版嵁澶勭悊
+                                    var revString = Encoding.UTF8.GetString(bytes);
+                                    Control.Ins.UpdataFunctionStatus(revString, null);
                                 }
                                 else
                                 {
                                     SetGatewayOnlineResetCheck();
-                                    var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.Instance.HomeGateway.aesKey);
-                                    
+                                    var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey);
                                     //bus鏁版嵁瑙f瀽
                                     if (DB_ResidenceData.Instance.GatewayType == 0)
                                     {
@@ -296,14 +308,6 @@
                                         {
                                             packet.Bytes = e.ApplicationMessage.Payload;
                                         }
-#if DEBUG
-                                        //string ddd = "";
-                                        //foreach (var bb in packet.Bytes)
-                                        //{
-                                        //    ddd += bb + ",";
-                                        //}
-                                        //MainPage.Log($"mqtt bus data:{ddd}");
-#endif
                                         packet.Manager();
                                     }
                                     else
@@ -311,7 +315,7 @@
                                         MainPage.Log($"mqtt A-Link data");
                                         //A鍗忚鏁版嵁澶勭悊
                                         var revString = Encoding.UTF8.GetString(bytes);
-                                        Control.Ins.UpdataFunctionStatus(revString,null);
+                                        Control.Ins.UpdataFunctionStatus(revString, null);
                                     }
                                 }
                             }
@@ -444,7 +448,9 @@
                 return;
             }
 
-            if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null)
+            //if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null)
+            //娌℃湁缃戝叧鎯呭喌涓嬶紝涔熼渶瑕佽繛鎺qtt锛屾秱楦︾涓夋柟璁惧涓嶉渶瑕佺綉鍏�
+            if (MqttInfoConfig.Current.mMqttInfo != null)
             {
                 try
                 {
@@ -463,6 +469,20 @@
                     MqttInfoConfig.Current.Save();
 
                     mqttEncryptKey = MqttInfoConfig.Current.HomeGatewayInfo.aesKey;
+
+                    if (MqttInfoConfig.Current.HomeGatewayInfo == null)
+                    {
+                        MqttInfoConfig.Current.HomeGatewayInfo = new Server.HomeGatewayInfo();
+                    }
+                    //瑙e瘑瀵嗛挜瑙勫垯锛氬凡鐜版湁鐨勪綇瀹匢D涓哄熀鍑�,浠庡彸杈逛竴涓�鑾峰彇鍊�,鏈�鍚庡鏋滀笉澶�16浣�,鍒欏線鍙宠ˉ闆�
+                    string aesKey = string.Empty;
+                    for (int i = DB_ResidenceData.Instance.CurrentRegion.RegionID.Length - 1; i >= 0; i--)
+                    {
+                        aesKey += DB_ResidenceData.Instance.CurrentRegion.RegionID[i].ToString();
+                        if (aesKey.Length == 16) { break; }
+                    }
+                    aesKey = aesKey.PadRight(16, '0');
+                    tuyaEncryptKey = aesKey;
 
                     var options1 = new MQTTnet.Client.Options.MqttClientOptionsBuilder()
                                         .WithClientId(clientId)
@@ -561,7 +581,7 @@
                 //寮圭獥鎻愮ず琚尋涓嬬嚎
                 HDLCommon.Current.CheckLogout();
             });
-           
+
 
             //UserInfo.Current.LastTime = DateTime.MinValue;
             //UserInfo.Current.SaveUserInfo();

--
Gitblit v1.8.0