using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using HDL_ON.Entity;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Linq;
|
using Shared.Net;
|
|
namespace HDL_ON.DriverLayer
|
{
|
public class Control_Udp
|
{
|
/// <summary>
|
/// 通讯端口
|
/// </summary>
|
public int port = 0;
|
|
/// <summary>
|
/// 控制失败次数
|
/// </summary>
|
public int controlLostCount = 0;
|
|
/// <summary>
|
/// 所有对一端口的控制都会放到这个集合里
|
/// </summary>
|
static List<Control_Udp> controlList = new List<Control_Udp>(50);
|
|
|
public System.Net.IPEndPoint EndPoint
|
{
|
get
|
{
|
try
|
{
|
return new System.Net.IPEndPoint(System.Net.IPAddress.Parse(new NetWiFi().BroadcastIpAddress.ToString()),6000);
|
}
|
catch
|
{
|
//防止异常导致程序退出
|
return new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 6000);
|
}
|
}
|
}
|
/// <summary>
|
/// 重发验证
|
/// </summary>
|
public static void ReceiveRepeatManager(string receiveFlag,byte []usefulBytes)
|
{
|
for (int i = 0; i < controlList.Count; i++)
|
{
|
try
|
{
|
var control = controlList[i];
|
if (control.SendFlag == receiveFlag)
|
{
|
control.UsefulBytes = usefulBytes;//
|
control.run();
|
}
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine($"control error : {ex.Message}");
|
}
|
}
|
}
|
/// <summary>
|
/// 添加到内存数组里面
|
/// </summary>
|
void add()
|
{
|
/// <summary>
|
/// 达到50条数据后就清理一下
|
/// </summary>
|
if (50 < controlList.Count)
|
{
|
lock (controlList)
|
{
|
for (int i = 0; i < controlList.Count;)
|
{
|
if (controlList[i] == null || 3 <= controlList[i].packet.HaveSendCount)
|
{
|
controlList.RemoveAt(i);
|
}
|
else
|
{
|
i++;
|
}
|
}
|
|
}
|
}
|
controlList.Add(this);
|
}
|
|
//当前数据的关键数据
|
string sendFlag = string.Empty;
|
protected string SendFlag
|
{
|
get
|
{
|
return sendFlag;
|
}
|
set
|
{
|
sendFlag = value;
|
usefulBytes = null;
|
}
|
}
|
|
private byte[] usefulBytes;
|
/// <summary>
|
/// 获取回来的有用信息,如果获取回来的数据为null,就会抛出异常信息
|
/// </summary>
|
public byte[] UsefulBytes
|
{
|
get
|
{
|
if (null == usefulBytes)
|
{
|
// throw new Exception("不好意思,网络不稳定或者远程设备不在线,请稍候再试!");
|
}
|
|
return this.usefulBytes;
|
}
|
set
|
{
|
usefulBytes = value;
|
}
|
}
|
|
//发送数据了之后当前线程等待或者运行的信号
|
System.Threading.ManualResetEvent allDone = new System.Threading.ManualResetEvent(false);
|
|
/// <summary>
|
/// 发送了数据后,线程就是等待状态,直到接收到反馈或者超时后退出
|
/// </summary>
|
void wait()
|
{
|
allDone.Reset();
|
allDone.WaitOne();
|
}
|
|
/// <summary>
|
/// 让当前线程继续执行
|
/// </summary>
|
void run()
|
{
|
allDone.Set();
|
packet.HaveSendCount = 4;
|
}
|
|
//数据发送处理
|
void managerSendCount(object o)
|
{
|
add();
|
//Bus socket无法控制,重启机制
|
if (controlLostCount > 10)
|
{
|
UdpSocket._BusSocket.Stop();
|
new System.Threading.Thread(() =>
|
{
|
System.Threading.Thread.Sleep(1000);
|
UdpSocket._BusSocket.Start(UdpSocket._BusSocket.Port);
|
controlLostCount = 0;
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
try
|
{
|
MainPage.Log("发送数据:" + SendFlag);
|
UdpSocket._BusSocket.AsyncBeginSend(packet);
|
packet.HaveSendCount--;
|
|
//这里是重发两次
|
while (packet.HaveSendCount < 3)
|
{
|
if (packet.FlagDateTime.AddMilliseconds(1000).Ticks <= DateTime.Now.Ticks)
|
{
|
MainPage.Log("重发数据:" + SendFlag);
|
UdpSocket._BusSocket.AsyncBeginSend(packet);
|
controlLostCount++;
|
}
|
System.Threading.Thread.Sleep(1000);
|
}
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log("managerSendCount:" + ex.ToString());
|
}
|
finally
|
{
|
allDone.Set();
|
}
|
}
|
|
/// <summary>
|
/// 当前数据包
|
/// </summary>
|
Packet packet;
|
/// <summary>
|
/// 读取网关IP
|
/// </summary>
|
public void SearchLocalGateway(bool broadcast = false)
|
{
|
try
|
{
|
var sendJob = new JObject { { "id", Control.Ins.msg_id.ToString() }, { "time_stamp", Utlis.GetTimestamp ()} };
|
var bodyString = JsonConvert.SerializeObject(sendJob);
|
|
var sendBytes = Control.Ins.ConvertSendBodyData(CommunicationTopic.SearchLoaclGateway, bodyString, false);
|
if (broadcast)
|
{
|
//广播
|
packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 8585));
|
}
|
else
|
{
|
//组播发送
|
packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.0.168.188"), 8585));
|
}
|
packet.HaveSendCount = 4;
|
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
|
thread.IsBackground = true;
|
thread.Start(packet);
|
//wait();不需要等待
|
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"Send bus data error {ex.Message}");
|
}
|
}
|
|
/// <summary>
|
/// 读取网关IP
|
/// </summary>
|
public void SendLinkCommand(string topic,string bodyString)
|
{
|
try
|
{
|
var sendBytes = Control.Ins.ConvertSendBodyData(topic, bodyString, false);
|
//组播发送
|
packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.0.168.188"), 8585));
|
packet.HaveSendCount = 4;
|
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
|
thread.IsBackground = true;
|
thread.Start(packet);
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"Send bus data error {ex.Message}");
|
}
|
}
|
|
/// <summary>
|
/// 发送udp A协议数据
|
/// </summary>
|
public void SendLocalHdlLinkData(byte[] sendBytes,string id, int resend = 3)
|
{
|
packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(Control.Ins.reportIp), 8585));
|
packet.HaveSendCount = 3 - resend;//重发次数
|
sendFlag = id;
|
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
|
thread.IsBackground = true;
|
thread.Start(packet);
|
//if (isWait)
|
//{
|
// wait();
|
//}
|
MainPage.Log($"发送Hdl-Link数据,IP:{Control.Ins.reportIp}:8585");
|
}
|
}
|
|
}
|