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/DriverLayer/Control.cs |   75 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/Control.cs b/HDL_ON/DAL/DriverLayer/Control.cs
index ed155f1..131f154 100644
--- a/HDL_ON/DAL/DriverLayer/Control.cs
+++ b/HDL_ON/DAL/DriverLayer/Control.cs
@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.Text;
 using HDL_ON.Entity;
 
 namespace HDL_ON.DriverLayer
@@ -88,14 +89,14 @@
         /// <summary>
         /// 鎵撳紑Udp
         /// </summary>
-        public void OpenUdp()
+        public void OpenUdp(int port)
         {
             if (myUdp == null)
             {
                 myUdp = new Control_Udp();
             }
 
-            UdpSocket._BusSocket.Start();
+            UdpSocket._BusSocket.Start(port);
         }
         /// <summary>
         /// 鎵撳紑tcp瀹㈡湇绔�
@@ -122,7 +123,12 @@
         /// </summary>
         public void SearchLoaclGateway()
         {
-            OpenUdp();
+
+#if DEBUG
+            DB_ResidenceData.residenceData.GatewayType = 1;
+#endif
+            OpenUdp(DB_ResidenceData.residenceData.GatewayType == 0 ? 6000 : 8585);
+
             if (DB_ResidenceData.residenceData.GatewayType == 0)
             {
                 myUdp.ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) });
@@ -224,6 +230,69 @@
             sendSidObj.type = "device_sid";
             sendSidObj.command = "get_list_response";
         }
+        /// <summary>
+        /// 杞崲閫氳鏁版嵁
+        /// </summary>
+        public byte[] ConvertSendBodyData(string topic, string bodyDataString)
+        {
+            string topicString = "Topic:" + topic + "/\r\n";
+            byte[] bodyBytes = Encoding.ASCII.GetBytes(bodyDataString);
+            string lenghtString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n";
+
+            string sendDataString = topicString + lenghtString + bodyDataString;
+            byte[] sendDataBytes = Encoding.ASCII.GetBytes(sendDataString);
+
+            return sendDataBytes;
+        }
+        /// <summary>
+        /// 杞崲鎺ユ敹鍒扮殑鏁版嵁
+        /// </summary>
+        /// <returns></returns>
+        public string ConvertReceiveData(byte[] bytes)
+        {
+            string receiveString = "";
+
+            string topic = "";
+            int lenght = 0;
+
+            var reString = Encoding.UTF8.GetString(bytes);
+            var res = reString.Split("\r\n\r\n");
+            
+
+            if (res.Length == 2)
+            {
+                var topics = res[0].Split("\r\n");
+                MainPage.Log(res[1]);
+                foreach (var ts in topics)
+                {
+                    var key = ts.Split(":");
+                    switch (key[0])
+                    {
+                        case "Topic":
+                            topic = key[1];
+                            break;
+                        case "Lenght":
+                            lenght = Convert.ToInt32(key[1]);
+                            break;
+                    }
+                }
+
+                switch (topic)
+                {
+                    case CommunicationTopic.SearchLoaclGateway:
+                        var jt = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(res[1]);
+                        if (jt != null)
+                        {
+                            var device = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.DeviceModule>(jt.objects.ToString());
+                            //Newtonsoft.Json.Linq.JObject.FromObject(jt.objects);
+                        }
+                        break;
+                }
+
+            }
+            return receiveString;
+        }
+
 
     }
 }
\ No newline at end of file

--
Gitblit v1.8.0