hxb
2020-09-23 2921da3e4ba99f0df28a172d47daa69e375431bc
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Common;
using HDLCloudMonitorSupportCtrlOldUdpGate.Context;
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
 
namespace HDLCloudMonitorSupportCtrlOldUdpGate
{
    public class MockBusproUdpGateway
    {
        public static async Task StartUdpClient()
        {
            var udpSender = new UdpClient(0);
 
            udpSender.Connect(Config.UdpServerIP, Config.UdpServerPort);
 
            //定时发网关心跳
            new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        var sendBytes = new byte[56];
 
                        //工程备注
                        byte[] projectBytes = Encoding.ASCII.GetBytes(Config.ProjectName);
                        Array.Copy(projectBytes, 0, sendBytes, 0, projectBytes.Length);
 
                        //群组备注
                        byte[] groupBytes = Encoding.ASCII.GetBytes(Config.GroupName);
                        Array.Copy(groupBytes, 0, sendBytes, 20, groupBytes.Length);
 
                        //远程访问的用户名  
                        byte[] userBytes = Encoding.ASCII.GetBytes(Config.UserName);
                        Array.Copy(userBytes, 0, sendBytes, 40, userBytes.Length);
 
                        byte[] macBytes = Encoding.ASCII.GetBytes(Config.Mac);   //4141434D41434241 、MacMark: fd3b738d-0a43-4602-83bd-979cefdac13b
                        Array.Copy(macBytes, 0, sendBytes, 48, macBytes.Length);
 
                        var data = new Data { AddData = sendBytes };
                        data.SubnetID = 1;
                        data.DeviceID = 2;
                        data.Command = 0x300B;
                        udpSender.Send(data.SendBytes, data.SendBytes.Length);
 
                        Thread.Sleep(10 * 1000);
                    }
                    catch (Exception ex)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(1));
                    }
                }
            })
            {
                IsBackground=true
            }.Start();
 
            //接收Udp转发过来的数据
            new Thread(async () =>
            {
                do
                {
                    try
                    {
                        //等待接收云端响应
                        UdpReceiveResult udpReceiveResult = await udpSender.ReceiveAsync();
                        string receiveData = BinaryHp.ToHexadecimalStr(udpReceiveResult.Buffer, false);
 
                        if (receiveData.Contains("300C") || receiveData.Contains("300c"))
                        {
                           // Console.WriteLine($"心跳响应:{receiveData}");
                        }
                        else if (receiveData.Contains("300F") || receiveData.Contains("300f"))
                        {
                            //验证
                            var sendBytes = new byte[] { 0xF8 };
                            var data = new Data { AddData = sendBytes };
                            data.SubnetID = 1;
                            data.DeviceID = 2;
                            data.Command = 0x3010;
                            udpSender.Send(data.SendBytes, data.SendBytes.Length);
                        }
                        else if (receiveData.Contains("000E") || receiveData.Contains("000e"))
                        {
                            #region 获取网关备注         
 
                            var buff = new byte[udpReceiveResult.Buffer.Length - 25 - 2];
                            Array.Copy(udpReceiveResult.Buffer, 25, buff, 0, buff.Length);
                            string reqStr = Encoding.UTF8.GetString(buff);
                            if (reqStr.Length == 36)
                            {
                                GlobalRecordContext.ReceiveResponseTime = DateTime.Now;
 
                                //到达网关用时
                                GlobalRecordContext.CtrlRecordDto.ArrivalsMilliseconds = (GlobalRecordContext.ReceiveResponseTime - GlobalRecordContext.CtrlStartTime).TotalMilliseconds;
                                                                
                               
                                Console.WriteLine($"  网关收到:{ GlobalRecordContext.ReceiveResponseTime.ToString(Consts.TimeFormat1)}");
                                //Console.WriteLine("Udp 网关收到时间-开始控制时间值:"+ GlobalRecordContext.CtrlRecordDto.ArrivalsMilliseconds.ToString()+" ms");
 
 
                                //把收到数据原样返回
                                var data = new Data { AddData = buff };
                                data.SubnetID = 1;
                                data.DeviceID = 2;
                                data.Command = 0x000F;
                                udpSender.Send(data.SendBytes, data.SendBytes.Length);
                            }
                            else
                            {
                               // Console.WriteLine($"收到转发的数据(非36):{reqStr}");
                            }
                            #endregion
                        }
                        else
                        {
                            //Console.WriteLine($"其他:{receiveData}");
                        }
                    }
                    catch (Exception ex)
                    {
                    }
 
                } while (true);
            })
            {
                IsBackground=true
            }.Start();
        }
    }
}