From d4160d80c79245c1d7d0cd450ba48cb7850e411d Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期一, 23 十一月 2020 10:45:54 +0800
Subject: [PATCH] 20201123

---
 HDL_ON/Common/A_Protocol_Common.cs |  108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 108 insertions(+), 0 deletions(-)

diff --git a/HDL_ON/Common/A_Protocol_Common.cs b/HDL_ON/Common/A_Protocol_Common.cs
new file mode 100644
index 0000000..8bec7c9
--- /dev/null
+++ b/HDL_ON/Common/A_Protocol_Common.cs
@@ -0,0 +1,108 @@
+锘匡豢using System;
+using System.Text;
+using Newtonsoft.Json.Linq;
+
+namespace HDL_ON.Common
+{
+    public class A_Protocal_Common
+    {
+        public static A_Protocal_Common apc;
+
+        public A_Protocal_Common()
+        {
+            apc = new A_Protocal_Common();
+        }
+
+
+        /// <summary>  
+        /// 鑾峰彇13浣嶆椂闂存埑
+        /// </summary>  
+        /// <param name="time">鏃堕棿</param>  
+        /// <returns>long</returns>  
+        public long Get_time_stamp()
+        {
+            //DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
+            long t = DateTime.Now.Ticks / 10000;   //闄�10000璋冩暣涓�13浣�      
+            return t;
+        }
+
+        /// <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 topic = "";
+            int lenght = 0;
+
+            var reString = System.Text.Encoding.UTF8.GetString(bytes);
+            var res = reString.Split("/r/n/r/n");
+
+
+            if (res.Length == 2)
+            {
+                var topics = res[0].Split("/r/n");
+                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;
+                }
+            }
+        }
+    }
+    /// <summary>
+    /// 閫氳鍥炲鏁版嵁鍖�
+    /// </summary>
+    public class ResponsePack
+    {
+        public string id;
+        public string code;
+        public string time_stamp;
+        public object objects;
+    }
+
+    /// <summary>
+    /// 閫氳涓婚
+    /// </summary>
+    public static class CommunicationTopic
+    {
+        /// <summary>
+        /// 鎼滅储鏈湴缃戝叧
+        /// </summary>
+        public const string SearchLoaclGateway = "/user/all/custom/gateway/search";
+    }
+}
\ No newline at end of file

--
Gitblit v1.8.0