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();
|
}
|
}
|
}
|