wxr
2020-11-23 d4160d80c79245c1d7d0cd450ba48cb7850e411d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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";
    }
}