From c3e1b733fc45bd9f0b88bfb560cfa87a270b079b Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期三, 09 九月 2020 17:33:58 +0800
Subject: [PATCH] 20200909

---
 HDL_ON/DAL/Net/MqttCommon.cs |  548 +++++++++++++++++-------------------------------------
 1 files changed, 173 insertions(+), 375 deletions(-)

diff --git a/HDL_ON/DAL/Net/MqttCommon.cs b/HDL_ON/DAL/Net/MqttCommon.cs
index 279056c..a2d2d5c 100644
--- a/HDL_ON/DAL/Net/MqttCommon.cs
+++ b/HDL_ON/DAL/Net/MqttCommon.cs
@@ -1,398 +1,196 @@
 锘縰sing System.Collections.Generic;
 using System;
-using MQTTnet.Client;
 using System.Text;
 using System.Security.Cryptography;
-using MQTTnet;
-using MQTTnet.Client.Options;
-using System.Net.Sockets;
-using System.Threading.Tasks;
-//using System.Net;
-//using Newtonsoft.Json.Linq;
-//using HDL_ON.DAL;
-//using Newtonsoft.Json;
 
 namespace HDL_ON.DAL.Net
 {
-    public static class MqttCommon
+
+    public class RemoteRequestParameters
     {
+        public string RequestVersion;
+        public int RequestSource;
+        public string LoginAccessToken;
+        public int RequestProtocolType;
+
+        public int HdlGatewayGatewayType = 0;
+        public bool IsRedirectSelectEmqServer = false;
         /// <summary>
-        /// MqttClient
+        /// 骞冲彴绫诲瀷瀛楃涓�
         /// </summary>
-        public static IMqttClient mqttClient_A;
-        public static string mqttClientIP;
-        public static string mqttGatewayMAC;
-        static bool remoteIsConnected;
-
-        static bool onConnection = false;
-
-        static MqttCommon()
-        {
-            InitMqtt();
-        }
-
-        /// <summary>
-        /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
-        /// </summary>
-        public static async Task DisConnectRemoteMqttClient(string s = "")
-        {
-            try
-            {
-                if (remoteIsConnected)
-                {
-                    remoteIsConnected = false;
-                    System.Console.WriteLine($"Remote涓诲姩鏂紑_{s}");
-                    //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
-                    await mqttClient_A.DisconnectAsync();
-                }
-            }
-            catch (Exception e)
-            {
-                Console.WriteLine($"Remote鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
-            }
-        }
-
-        static bool isSubscribeSuccess;
-        static async Task SubscribeTopics()
-        {
-            if (remoteIsConnected && !isSubscribeSuccess)
-            {
-                try
-                {
-                    var Topic1 = $"/BusGateWayToApp/{mqttGatewayMAC}/Common/Json";
-                    try
-                    {
-                        await mqttClient_A.SubscribeAsync(Topic1);
-                    }
-                    catch (Exception ex)
-                    {
-                        await DisConnectRemoteMqttClient(ex.Message);
-                        await StartMqtt();
-                        if (remoteIsConnected)
-                        {
-                            await mqttClient_A.SubscribeAsync(Topic1);
-                        }
-                    }
-                }
-                catch { }
-            }
-        }
-
-        static void InitMqtt()
-        {
-            new System.Threading.Thread(async () => {
-                while (true)
-                {
-                    try
-                    {
-                        System.Threading.Thread.Sleep(1000);
-                        //if (!CommonPage.IsRemote)
-                        //    continue;
-                        if (remoteIsConnected)
-                            continue;
-                        await StartMqtt();
-                        await SubscribeTopics();
-                    }
-                    catch { }
-                }
-            })
-            { IsBackground = true }.Start();
-        }
-
+        public string PlatformStr;
         /// <summary>
-        /// 鍚姩A鍗忚Mqtt
+        /// 鍙戝竷涓婚璐熻浇
         /// </summary>
-        public static async Task StartMqtt()
-        {
-            try
-            {
-                if (remoteIsConnected)
-                    return;
-                if (onConnection)
-                    return;
-                onConnection = true;
-                new System.Threading.Thread(async () =>
-                {
-                    if (remoteIsConnected)
-                        return;
-                    try
-                    {
-                        if (mqttClient_A == null)
-                        {
-                            mqttClient_A = new MqttFactory().CreateMqttClient();
-                            mqttClient_A.UseApplicationMessageReceivedHandler(async e =>
-                            {
-                                var aesDecryptTopic = e.ApplicationMessage.Topic;
-                                var aesDecryptPayload = e.ApplicationMessage.Payload;
-                                MainPage.Log(aesDecryptTopic);
-                                MainPage.Log($"Des Topic={aesDecryptTopic}");
-                            });
+        public string PublishPayloadJsonStr;
 
-                            mqttClient_A.UseConnectedHandler(async (e) => {
-                                MainPage.Log("mqtt connected !!");
-                                onConnection = false;
-                            });
-                        }
-                        try
-                        {
-                            int readCount = 0;
-                            BusSocket.Stop();
-                            System.Threading.Thread.Sleep(1000);
-                            BusSocket.Start(6688);
-                            System.Threading.Thread.Sleep(1000);
-                            Control.ReadGatewayIPAddress();
-                            while(true)
-                            {
-                                if (!string.IsNullOrEmpty(mqttClientIP))
-                                {
-                                    break;
-                                }
-                                else if (readCount > 10)
-                                {
-                                    onConnection = false;
-                                    return;
-                                }
-                                else
-                                {
-                                    Control.ReadGatewayIPAddress();
-                                    System.Threading.Thread.Sleep(200);
-                                }
-                            }
-                            BusSocket.Stop();
-                            System.Threading.Thread.Sleep(1000);
-                            BusSocket.Start(6000);
-                            System.Threading.Thread.Sleep(1000);
-
-                            var options = new MqttClientOptionsBuilder()//MQTT杩炴帴鍙傛暟濉厖
-                                .WithClientId(Guid.NewGuid().ToString().Substring(0, 5))//瀹㈡埛绔疘D
-                                .WithTcpServer(mqttClientIP, 1883)//MQTTServerIP.Text, Int32.Parse(MQTTServerPort.Text.ToString()))//TCP鏈嶅姟绔�  1883  锛屽嵆MQTT鏈嶅姟绔�
-                                .WithCredentials("", "")//"", "")//鍑瘉  甯愬彿 瀵嗙爜
-                                .WithCommunicationTimeout(new TimeSpan(0, 0, 60)) //閲嶈繛瓒呮椂鏃堕棿锛岄粯璁�5s
-                                .WithKeepAlivePeriod(new TimeSpan(0, 0, 15)) //淇濇寔杩炴帴鏃堕棿锛岄粯璁�5s锛屽績璺冲寘
-                                .Build();
-                            await mqttClient_A.ConnectAsync(options);
-                            remoteIsConnected = true;
-                        }
-                        catch { }
-                    }
-                    catch (Exception ex)
-                    {
-                    }
-                    finally
-                    {
-                        onConnection = false;
-                    }
-                })
-                { IsBackground = true }.Start();
-            }
-            catch (Exception ex)
-            {
-                MainPage.Log("============>" + ex.Message);
-            }
-        }
-
-
-
-
-        /// <summary>
-        /// 
-        /// </summary>
-        /// <param name="message">闄勫姞鏁版嵁鍖�</param>
-        /// <param name="optionType">鎿嶄綔绫诲瀷锛�0=缃戝叧鎺у埗锛�1=璁㈤槄缃戝叧鏁版嵁;2=璁㈤槄缃戝叧涓婄嚎鏁版嵁</param>
-        /// <returns></returns>
-        public static async Task MqttRemoteSend(byte[] message)
-        {
-            try
-            {
-                if (mqttClient_A == null || !mqttClient_A.IsConnected)
-                {
-                    await StartMqtt();
-                }
-                if (!mqttClient_A.IsConnected)
-                {
-                    return;
-                }
-                var topicName = $"/AppToBusGateWay/{mqttGatewayMAC}/Common/Json";
-                var m = new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce };
-                await mqttClient_A?.PublishAsync(m);
-            }
-            catch (Exception e)
-            {
-            }
-        }
+        public string Mac = "";
+        public string GroupName = "";
     }
-}
 
-public class RemoteRequestParameters
-{
-    public string RequestVersion;
-    public int RequestSource;
-    public string LoginAccessToken;
-    public int RequestProtocolType;
-
-    public string Mac = "";
-    public string GroupName = "";
-}
-
-public class MqttRemoteInfo
-{
-    public List<RemoteMACInfo> pageData;
-
-    public int pageIndex = 0;
-    public int pageSize = 10;
-    public int totalCount = 3;
-    public int totalPages = 1;
-    public bool hasPreviousPage = false;
-    public bool hasNextPage = false;
-}
-
-public class MqttInfo
-{
-    public string connEmqDomainPort;
-    public string connEmqClientId;
-    public string connEmqUserName;
-    public string connEmqPwd;
-}
-
-public class RemoteMACInfo
-{
-    public string mac;
-    public string macMark;
-    public string isValid;
-    public string aesKey;
-    public bool isNewBusproGateway;
-    public string groupName;
-    public string projectName;
-    public string userName;
-
-    //app鑷畾涔夋暟鎹�
-    public string md5_mac_string;
-    public string LoginAccessToken;
-}
-
-namespace Shared.Securitys
-{
-    public partial class EncryptionService
+    public class MqttRemoteInfo
     {
+        public List<RemoteMACInfo> pageData;
 
-        #region 鍔犲瘑
-        /// <summary>
-        /// 鍔犲瘑涓婚涓築ase64
-        /// </summary>
-        /// <param name="pToEncrypt"></param>
-        /// <param name="key"></param>
-        /// <returns></returns>
-        public static string AesEncryptTopic(string pToEncrypt, string key)
-        {
-            if (string.IsNullOrEmpty(pToEncrypt)) return null;
-            if (string.IsNullOrEmpty(key)) return pToEncrypt;
-            //闇�瑕佸姞瀵嗗唴瀹圭殑鏄庢枃娴�
-            Byte[] toEncryptArray = Encoding.UTF8.GetBytes(pToEncrypt);
+        public int pageIndex = 0;
+        public int pageSize = 10;
+        public int totalCount = 3;
+        public int totalPages = 1;
+        public bool hasPreviousPage = false;
+        public bool hasNextPage = false;
+    }
 
-            //閰嶇疆AES鍔犲瘑Key(瀵嗛挜銆佸悜閲忋�佹ā寮忋�佸~鍏�)
-            RijndaelManaged rm = new RijndaelManaged
-            {
-                Key = Encoding.UTF8.GetBytes(key),
-                IV = Encoding.UTF8.GetBytes(key),
-                Mode = CipherMode.CBC,
-                Padding = PaddingMode.PKCS7
-            };
+    public class MqttInfo
+    {
+        public string connEmqDomainPort;
+        public string connEmqClientId;
+        public string connEmqUserName;
+        public string connEmqPwd;
 
-            //鍒涘缓AES鍔犲瘑鍣ㄥ璞�
-            ICryptoTransform cTransform = rm.CreateEncryptor();
-
-            //浣跨敤AES灏嗘槑鏂囨祦杞垚瀵嗘枃瀛楄妭鏁扮粍
-            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
-
-            //灏咥ES鐢熸垚鐨勫瘑鏂囧瓧鑺傛暟缁勮浆鎴怋ase64瀛楃涓�
-            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
-        }
-
-
-        /// <summary>
-        /// 鍔犲瘑璐熻浇涓轰簩杩涘埗娴�
-        /// </summary>
-        /// <param name="toEncryptArray"></param>
-        /// <param name="key"></param>
-        /// <returns></returns>
-        public static byte[] AesEncryptPayload(byte[] toEncryptArray, string key)
-        {
-            if (string.IsNullOrEmpty(key)) return toEncryptArray;
-            //閰嶇疆AES鍔犲瘑Key(瀵嗛挜銆佸悜閲忋�佹ā寮忋�佸~鍏�)
-            var rm = new RijndaelManaged
-            {
-                Key = Encoding.UTF8.GetBytes(key),
-                IV = Encoding.UTF8.GetBytes(key),
-                Mode = CipherMode.CBC,
-                Padding = PaddingMode.PKCS7
-            };
-
-            //鍒涘缓AES鍔犲瘑鍣ㄥ璞�
-            var cTransform = rm.CreateEncryptor();
-            //浣跨敤AES灏嗘槑鏂囨祦杞垚瀵嗘枃瀛楄妭鏁扮粍
-            return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
-        }
-        #endregion
-
-
-        #region 瑙e瘑
-        /// <summary>
-        /// 瑙e瘑涓婚鏁版嵁
-        /// </summary>
-        /// <param name="pToDecrypt"></param>
-        /// <param name="key"></param>
-        /// <returns></returns>
-        public static string AesDecryptTopic(string pToDecrypt, string key)
-        {
-            //AES瀵嗘枃Base64杞垚瀛楃涓�
-            Byte[] toEncryptArray = Convert.FromBase64String(pToDecrypt);
-
-            //閰嶇疆AES鍔犲瘑Key(瀵嗛挜銆佸悜閲忋�佹ā寮忋�佸~鍏�)
-            RijndaelManaged rm = new RijndaelManaged
-            {
-                Key = Encoding.UTF8.GetBytes(key),
-                IV = Encoding.UTF8.GetBytes(key),
-                Mode = CipherMode.CBC,
-                Padding = PaddingMode.PKCS7
-            };
-
-            //鍒涘缓AES瑙e瘑鍣ㄥ璞�
-            ICryptoTransform cTransform = rm.CreateDecryptor();
-
-            //浣跨敤AES灏嗗瘑鏂囨祦杞垚鏄庢枃鐨勫瓧鑺傛暟缁�
-            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
-
-            //杞垚瀛楃涓�
-            return Encoding.UTF8.GetString(resultArray);
-        }
-
-        /// <summary>
-        /// 閲囩敤Aes瑙e瘑璐熻浇鏁版嵁
-        /// </summary>
-        /// <param name="toEncryptArray"></param>
-        /// <param name="key"></param>
-        /// <returns></returns>
-        public static byte[] AesDecryptPayload(byte[] toEncryptArray, string key)
-        {
-            //閰嶇疆AES鍔犲瘑Key(瀵嗛挜銆佸悜閲忋�佹ā寮忋�佸~鍏�)
-            var rm = new RijndaelManaged
-            {
-                Key = Encoding.UTF8.GetBytes(key),
-                IV = Encoding.UTF8.GetBytes(key),
-                Mode = CipherMode.CBC,
-                Padding = PaddingMode.PKCS7
-            };
-
-            //鍒涘缓AES瑙e瘑鍣ㄥ璞�
-            var cTransform = rm.CreateDecryptor();
-
-            //浣跨敤AES灏嗗瘑鏂囨祦杞垚鏄庢枃鐨勫瓧鑺傛暟缁�
-            return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
-        }
-        #endregion
-
+        public List<RemoteMACInfo> AccountAllGateways;
 
     }
-}
+
+    public class RemoteMACInfo
+    {
+        public string mac;
+        public string macMark;
+        public string isValid;
+        public string aesKey;
+        public bool isNewBusproGateway;
+        public string groupName;
+        public string projectName;
+        public string userName;
+
+        public string clientId;
+
+        //app鑷畾涔夋暟鎹�
+        public string md5_mac_string;
+        public string LoginAccessToken;
+    }
+
+    namespace Shared.Securitys
+    {
+        public partial class EncryptionService
+        {
+
+            #region 鍔犲瘑
+            /// <summary>
+            /// 鍔犲瘑涓婚涓築ase64
+            /// </summary>
+            /// <param name="pToEncrypt"></param>
+            /// <param name="key"></param>
+            /// <returns></returns>
+            public static string AesEncryptTopic(string pToEncrypt, string key)
+            {
+                if (string.IsNullOrEmpty(pToEncrypt)) return null;
+                if (string.IsNullOrEmpty(key)) return pToEncrypt;
+                //闇�瑕佸姞瀵嗗唴瀹圭殑鏄庢枃娴�
+                Byte[] toEncryptArray = Encoding.UTF8.GetBytes(pToEncrypt);
+
+                //閰嶇疆AES鍔犲瘑Key(瀵嗛挜銆佸悜閲忋�佹ā寮忋�佸~鍏�)
+                RijndaelManaged rm = new RijndaelManaged
+                {
+                    Key = Encoding.UTF8.GetBytes(key),
+                    IV = Encoding.UTF8.GetBytes(key),
+                    Mode = CipherMode.CBC,
+                    Padding = PaddingMode.PKCS7
+                };
+
+                //鍒涘缓AES鍔犲瘑鍣ㄥ璞�
+                ICryptoTransform cTransform = rm.CreateEncryptor();
+
+                //浣跨敤AES灏嗘槑鏂囨祦杞垚瀵嗘枃瀛楄妭鏁扮粍
+                Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
+
+                //灏咥ES鐢熸垚鐨勫瘑鏂囧瓧鑺傛暟缁勮浆鎴怋ase64瀛楃涓�
+                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
+            }
 
 
+            /// <summary>
+            /// 鍔犲瘑璐熻浇涓轰簩杩涘埗娴�
+            /// </summary>
+            /// <param name="toEncryptArray"></param>
+            /// <param name="key"></param>
+            /// <returns></returns>
+            public static byte[] AesEncryptPayload(byte[] toEncryptArray, string key)
+            {
+                if (string.IsNullOrEmpty(key)) return toEncryptArray;
+                //閰嶇疆AES鍔犲瘑Key(瀵嗛挜銆佸悜閲忋�佹ā寮忋�佸~鍏�)
+                var rm = new RijndaelManaged
+                {
+                    Key = Encoding.UTF8.GetBytes(key),
+                    IV = Encoding.UTF8.GetBytes(key),
+                    Mode = CipherMode.CBC,
+                    Padding = PaddingMode.PKCS7
+                };
+
+                //鍒涘缓AES鍔犲瘑鍣ㄥ璞�
+                var cTransform = rm.CreateEncryptor();
+                //浣跨敤AES灏嗘槑鏂囨祦杞垚瀵嗘枃瀛楄妭鏁扮粍
+                return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
+            }
+            #endregion
+
+
+            #region 瑙e瘑
+            /// <summary>
+            /// 瑙e瘑涓婚鏁版嵁
+            /// </summary>
+            /// <param name="pToDecrypt"></param>
+            /// <param name="key"></param>
+            /// <returns></returns>
+            public static string AesDecryptTopic(string pToDecrypt, string key)
+            {
+                //AES瀵嗘枃Base64杞垚瀛楃涓�
+                Byte[] toEncryptArray = Convert.FromBase64String(pToDecrypt);
+
+                //閰嶇疆AES鍔犲瘑Key(瀵嗛挜銆佸悜閲忋�佹ā寮忋�佸~鍏�)
+                RijndaelManaged rm = new RijndaelManaged
+                {
+                    Key = Encoding.UTF8.GetBytes(key),
+                    IV = Encoding.UTF8.GetBytes(key),
+                    Mode = CipherMode.CBC,
+                    Padding = PaddingMode.PKCS7
+                };
+
+                //鍒涘缓AES瑙e瘑鍣ㄥ璞�
+                ICryptoTransform cTransform = rm.CreateDecryptor();
+
+                //浣跨敤AES灏嗗瘑鏂囨祦杞垚鏄庢枃鐨勫瓧鑺傛暟缁�
+                Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
+
+                //杞垚瀛楃涓�
+                return Encoding.UTF8.GetString(resultArray);
+            }
+
+            /// <summary>
+            /// 閲囩敤Aes瑙e瘑璐熻浇鏁版嵁
+            /// </summary>
+            /// <param name="toEncryptArray"></param>
+            /// <param name="key"></param>
+            /// <returns></returns>
+            public static byte[] AesDecryptPayload(byte[] toEncryptArray, string key)
+            {
+                //閰嶇疆AES鍔犲瘑Key(瀵嗛挜銆佸悜閲忋�佹ā寮忋�佸~鍏�)
+                var rm = new RijndaelManaged
+                {
+                    Key = Encoding.UTF8.GetBytes(key),
+                    IV = Encoding.UTF8.GetBytes(key),
+                    Mode = CipherMode.CBC,
+                    Padding = PaddingMode.PKCS7
+                };
+
+                //鍒涘缓AES瑙e瘑鍣ㄥ璞�
+                var cTransform = rm.CreateDecryptor();
+
+                //浣跨敤AES灏嗗瘑鏂囨祦杞垚鏄庢枃鐨勫瓧鑺傛暟缁�
+                return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
+            }
+            #endregion
+
+
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.8.0