|
@ -1,4 +1,5 @@ |
|
|
using System.Collections.Concurrent; |
|
|
using EC.Util.Common; |
|
|
|
|
|
using System.Collections.Concurrent; |
|
|
using System.Net; |
|
|
using System.Net; |
|
|
using System.Timers; |
|
|
using System.Timers; |
|
|
using Timer = System.Timers.Timer; |
|
|
using Timer = System.Timers.Timer; |
|
@ -97,11 +98,11 @@ public class TcpManager |
|
|
|
|
|
|
|
|
private void Server_ClientConnected(object? sender, TcpClientConnectedEventArgs e) |
|
|
private void Server_ClientConnected(object? sender, TcpClientConnectedEventArgs e) |
|
|
{ |
|
|
{ |
|
|
string clientKey = e.TcpClient.Client.RemoteEndPoint?.ToString() ?? ""; |
|
|
string clientAddr = e.TcpClient.ClientAddr(); |
|
|
if (clientKey == "") return; |
|
|
if (clientAddr == ":") return; |
|
|
string clientIp = clientKey.Split(':')[0]; |
|
|
string clientIp = clientAddr.Split(':')[0]; |
|
|
string clientPort = clientKey.Split(':')[1]; |
|
|
string clientPort = clientAddr.Split(':')[1]; |
|
|
Console.WriteLine("上线:{0}", clientIp); |
|
|
Console.WriteLine("主机上线:{0}", clientIp); |
|
|
if (!TryGetDevice(clientIp, out ClientMessage clientMsg)) |
|
|
if (!TryGetDevice(clientIp, out ClientMessage clientMsg)) |
|
|
{ |
|
|
{ |
|
|
clientMsg = new() |
|
|
clientMsg = new() |
|
@ -119,11 +120,11 @@ public class TcpManager |
|
|
|
|
|
|
|
|
private void Server_ClientDisconnected(object? sender, TcpClientDisconnectedEventArgs e) |
|
|
private void Server_ClientDisconnected(object? sender, TcpClientDisconnectedEventArgs e) |
|
|
{ |
|
|
{ |
|
|
string clientKey = e.TcpClient.Client.RemoteEndPoint?.ToString() ?? ""; |
|
|
string clientAddr = e.TcpClient.ClientAddr(); |
|
|
if (clientKey == "") return; |
|
|
if (clientAddr == ":") return; |
|
|
string clientIp = clientKey.Split(':')[0]; |
|
|
string clientIp = clientAddr.Split(':')[0]; |
|
|
string clientPort = clientKey.Split(':')[1]; |
|
|
string clientPort = clientAddr.Split(':')[1]; |
|
|
Console.WriteLine("下线:{0}", clientIp); |
|
|
Console.WriteLine("主机下线:{0}", clientIp); |
|
|
if (TryGetDevice(clientIp, out ClientMessage clientMsg)) |
|
|
if (TryGetDevice(clientIp, out ClientMessage clientMsg)) |
|
|
{ |
|
|
{ |
|
|
ProcessDeviceStateEvent(ref clientMsg, 0); |
|
|
ProcessDeviceStateEvent(ref clientMsg, 0); |
|
@ -132,12 +133,16 @@ public class TcpManager |
|
|
|
|
|
|
|
|
private void Server_DatagramReceived(object? sender, TcpDatagramReceivedEventArgs<byte[]> e) |
|
|
private void Server_DatagramReceived(object? sender, TcpDatagramReceivedEventArgs<byte[]> e) |
|
|
{ |
|
|
{ |
|
|
string clientKey = e.TcpClient.TcpClient.Client.RemoteEndPoint?.ToString() ?? ""; |
|
|
string clientAddr = e.ClientState.Client.ClientAddr(); |
|
|
if (clientKey == "") return; |
|
|
if (clientAddr == ":") return; |
|
|
string clientIp = clientKey.Split(':')[0]; |
|
|
string clientIp = clientAddr.Split(':')[0]; |
|
|
|
|
|
string clientPort = clientAddr.Split(':')[1]; |
|
|
if (TryGetDevice(clientIp, out ClientMessage clientMsg)) |
|
|
if (TryGetDevice(clientIp, out ClientMessage clientMsg)) |
|
|
{ |
|
|
{ |
|
|
|
|
|
if (clientMsg.Client == null || !clientMsg.Client.Connected) clientMsg.Client = e.ClientState.Client; |
|
|
|
|
|
clientMsg.Host ??= new() { Id = -1, Ip = clientIp, Port = clientPort }; |
|
|
clientMsg.AddData(e.Datagram); |
|
|
clientMsg.AddData(e.Datagram); |
|
|
|
|
|
ProcessDeviceStateEvent(ref clientMsg, 1); |
|
|
AnalysisClientMessage(ref clientMsg); |
|
|
AnalysisClientMessage(ref clientMsg); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -148,96 +153,105 @@ public class TcpManager |
|
|
|
|
|
|
|
|
private void AnalysisClientMessage(ref ClientMessage clientMsg) |
|
|
private void AnalysisClientMessage(ref ClientMessage clientMsg) |
|
|
{ |
|
|
{ |
|
|
List<byte[]> msglist = clientMsg.GetMessageList(); |
|
|
List<byte[]> msgList = clientMsg.GetMessageList(); |
|
|
if (msglist == null || msglist.Count == 0) return; |
|
|
foreach (byte[] msg in msgList) |
|
|
for (int i = 0; i < msglist.Count; i++) |
|
|
|
|
|
{ |
|
|
{ |
|
|
byte[] msg = msglist[i]; |
|
|
|
|
|
bool vaild = msg.Length >= 19 && msg[0] == 0xAA && msg[1] == 0xAA; |
|
|
bool vaild = msg.Length >= 19 && msg[0] == 0xAA && msg[1] == 0xAA; |
|
|
Console.WriteLine("Recv from {0}:{1} => {2}, {3}", clientMsg.Ip, clientMsg.Port, DataMessage.ToHexString(msg), vaild); |
|
|
Console.WriteLine("Recv from {0}:{1} => {2}, vaild:{3}", clientMsg.ClientIp, clientMsg.ClientPort, DataMessage.ToHexString(msg), vaild); |
|
|
if (!vaild) continue; |
|
|
if (!vaild) continue; |
|
|
DataMessage mm = new(); |
|
|
DataMessage mm = new(); |
|
|
mm.Decode(msglist[i]); |
|
|
mm.Decode(msg); |
|
|
|
|
|
if (clientMsg.Host.Id != mm.DeviceId) clientMsg.Host.Id = mm.DeviceId; |
|
|
switch (mm.FunctionNum) |
|
|
switch (mm.FunctionNum) |
|
|
{ |
|
|
{ |
|
|
case 0x00: |
|
|
case 0x00: |
|
|
Console.WriteLine("主机登录:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("主机登录:{0}", clientMsg.ClientAddr); |
|
|
if (clientMsg.Host == null || clientMsg.Host.Id != mm.DeviceId) |
|
|
|
|
|
{ |
|
|
|
|
|
clientMsg.Host = new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString() }; |
|
|
|
|
|
UpdateDevice(clientMsg.Ip, clientMsg.Host); |
|
|
|
|
|
} |
|
|
|
|
|
ProcessDeviceStateEvent(ref clientMsg, 1); |
|
|
|
|
|
ResponseHostLogin_10(clientMsg, mm); |
|
|
ResponseHostLogin_10(clientMsg, mm); |
|
|
ClientMessage taskClientMsg = clientMsg; |
|
|
RequestHostAutoUploadState_24(clientMsg); |
|
|
if (taskClientMsg.SensorsEmpty && taskClientMsg.SensorsLock.TryEnterWriteLock(1000)) |
|
|
RequestSensorList_07(clientMsg); |
|
|
{ |
|
|
|
|
|
Task.Run(() => |
|
|
|
|
|
{ |
|
|
|
|
|
while (taskClientMsg.SensorsEmpty) |
|
|
|
|
|
{ |
|
|
|
|
|
RequestSensorList_07(taskClientMsg); |
|
|
|
|
|
Thread.Sleep(1000); |
|
|
|
|
|
} |
|
|
|
|
|
taskClientMsg.SensorsLock.ExitWriteLock(); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 0x01: |
|
|
case 0x01: |
|
|
Console.WriteLine("心跳:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("主机心跳:{0}", clientMsg.ClientAddr); |
|
|
if (clientMsg.Host == null || clientMsg.Host.Id != mm.DeviceId) |
|
|
|
|
|
{ |
|
|
|
|
|
clientMsg.Host = new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString() }; |
|
|
|
|
|
UpdateDevice(clientMsg.Ip, clientMsg.Host); |
|
|
|
|
|
} |
|
|
|
|
|
ProcessDeviceStateEvent(ref clientMsg, 1); |
|
|
|
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 0x12: |
|
|
case 0x12: |
|
|
Console.WriteLine("传感器地址设置响应:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("传感器地址设置响应:{0}", clientMsg.ClientAddr); |
|
|
SetDataRequest(mm, 0x02); |
|
|
|
|
|
|
|
|
SetDataResponse(mm, 0x02); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 0x13: |
|
|
case 0x13: |
|
|
Console.WriteLine("传感器模式设置响应:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("传感器模式设置响应:{0}", clientMsg.ClientAddr); |
|
|
SetDataRequest(mm, 0x03); |
|
|
|
|
|
|
|
|
SetDataResponse(mm, 0x03); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 0x14: |
|
|
case 0x14: |
|
|
Console.WriteLine("传感器轮询状态响应:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("传感器轮询状态响应:{0}", clientMsg.ClientAddr); |
|
|
int sensorAddr = mm.Data[0] + mm.Data[1] * 256; |
|
|
int sensorAddr = mm.Data[0] + mm.Data[1] * 256; |
|
|
int state = mm.Data[2] + mm.Data[3] * 256; |
|
|
int state = mm.Data[2] + mm.Data[3] * 256; |
|
|
int offline = state % 2; |
|
|
ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, state); |
|
|
int alarm = state / 2 % 2; |
|
|
|
|
|
ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, offline, alarm); |
|
|
SetDataResponse(mm, 0x04); |
|
|
SetDataRequest(mm, 0x04); |
|
|
|
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 0x15: |
|
|
case 0x15: |
|
|
Console.WriteLine("传感器复位响应:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("传感器复位响应:{0}", clientMsg.ClientAddr); |
|
|
SetDataRequest(mm, 0x05); |
|
|
|
|
|
|
|
|
SetDataResponse(mm, 0x05); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 0x17: |
|
|
case 0x17: |
|
|
Console.WriteLine("返回传感器列表:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("主机返回传感器列表:{0}", clientMsg.ClientAddr); |
|
|
if (clientMsg.SensorsEmpty) |
|
|
|
|
|
{ |
|
|
|
|
|
for (int j = 2; j < mm.Data.Length; j++) |
|
|
for (int j = 2; j < mm.Data.Length; j++) |
|
|
{ |
|
|
{ |
|
|
sensorAddr = Convert.ToByte((mm.Data[j] + mm.Data[++j] * 256)); |
|
|
sensorAddr = Convert.ToByte((mm.Data[j] + mm.Data[++j] * 256)); |
|
|
|
|
|
if (clientMsg.SensorDict.ContainsKey(sensorAddr)) continue; |
|
|
SensorState sensor = new(mm.DeviceId, sensorAddr, 0, 0); |
|
|
SensorState sensor = new(mm.DeviceId, sensorAddr, 0, 0); |
|
|
clientMsg.SensorDict[sensorAddr] = sensor; |
|
|
clientMsg.SensorDict[sensorAddr] = sensor; |
|
|
} |
|
|
} |
|
|
RequestSensorsStateActively_08(clientMsg); |
|
|
RequestSensorsState_04(clientMsg); |
|
|
} |
|
|
|
|
|
SetDataRequest(mm, 0x07); |
|
|
SetDataResponse(mm, 0x07); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 0x18: |
|
|
case 0x18: |
|
|
Console.WriteLine("传感器主动状态响应:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("传感器主动状态响应:{0}", clientMsg.ClientAddr); |
|
|
|
|
|
sensorAddr = mm.Data[0] + mm.Data[1] * 256; |
|
|
|
|
|
state = mm.Data[2] + mm.Data[3] * 256; |
|
|
|
|
|
ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, state); |
|
|
|
|
|
ResponseHostUploadState_08(clientMsg, mm); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case 0x19: |
|
|
|
|
|
Console.WriteLine("传感器关闭响应:{0}", clientMsg.ClientAddr); |
|
|
|
|
|
|
|
|
|
|
|
SetDataResponse(mm, 0x09); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case 0x30: |
|
|
|
|
|
Console.WriteLine("传感器启动响应:{0}", clientMsg.ClientAddr); |
|
|
|
|
|
|
|
|
|
|
|
SetDataResponse(mm, 0x20); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
SetDataRequest(mm, 0x08); |
|
|
case 0x31: |
|
|
|
|
|
Console.WriteLine("传感器全部启动响应:{0}", clientMsg.ClientAddr); |
|
|
|
|
|
|
|
|
|
|
|
SetDataResponse(mm, 0x21); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case 0x34: |
|
|
|
|
|
Console.WriteLine("主机启动自动上传响应:{0}", clientMsg.ClientAddr); |
|
|
|
|
|
|
|
|
|
|
|
SetDataResponse(mm, 0x24); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case 0x35: |
|
|
|
|
|
Console.WriteLine("主机关闭自动上传响应:{0}", clientMsg.ClientAddr); |
|
|
|
|
|
|
|
|
|
|
|
SetDataResponse(mm, 0x25); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -252,8 +266,10 @@ public class TcpManager |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void ProcessSensorStateEvent(ref ClientMessage client, DataMessage mm, int sensorAddr, int offline, int alarm) |
|
|
private void ProcessSensorStateEvent(ref ClientMessage client, DataMessage mm, int sensorAddr, int state) |
|
|
{ |
|
|
{ |
|
|
|
|
|
int offline = state % 2; |
|
|
|
|
|
int alarm = state / 2 % 2; |
|
|
if (!client.SensorDict.TryGetValue(sensorAddr, out SensorState? sensor)) |
|
|
if (!client.SensorDict.TryGetValue(sensorAddr, out SensorState? sensor)) |
|
|
{ |
|
|
{ |
|
|
sensor = new(mm.DeviceId, sensorAddr, offline, alarm); |
|
|
sensor = new(mm.DeviceId, sensorAddr, offline, alarm); |
|
@ -269,7 +285,7 @@ public class TcpManager |
|
|
if (alarm == 1) |
|
|
if (alarm == 1) |
|
|
{ |
|
|
{ |
|
|
ReportAlarm(sensor.DeviceId, sensorAddr); |
|
|
ReportAlarm(sensor.DeviceId, sensorAddr); |
|
|
//RequestSensorReset_05(clientMsg, sensorAddr);
|
|
|
RequestSensorReset_05(client, sensorAddr); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -299,41 +315,24 @@ public class TcpManager |
|
|
|
|
|
|
|
|
#region Send
|
|
|
#region Send
|
|
|
|
|
|
|
|
|
public bool SendMessage(string ip, byte[] bytes) |
|
|
/// <summary>
|
|
|
{ |
|
|
/// 0x10:主机响应0x00登录
|
|
|
if (Server == null || !Server.IsRunning()) return false; |
|
|
/// </summary>
|
|
|
if (!TryGetDevice(ip, out ClientMessage clientMsg)) return false; |
|
|
/// <param name="client"></param>
|
|
|
string cmd = DataMessage.ToHexString(bytes); |
|
|
/// <param name="mm"></param>
|
|
|
DataMessage mm = new(); |
|
|
/// <returns></returns>
|
|
|
mm.Decode(bytes); |
|
|
public bool ResponseHostLogin_10(ClientMessage client, DataMessage mm) |
|
|
bool flag = Server.SendAsync(clientMsg.Client, bytes); |
|
|
|
|
|
Console.WriteLine("Send to {0}:{1} => {2}, {3}", ip, clientMsg.Port, cmd, flag); |
|
|
|
|
|
return flag; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public bool SendRequest(ref DataRequest request) |
|
|
|
|
|
{ |
|
|
|
|
|
if (request.Request == null) return false; |
|
|
|
|
|
request.Request.FrameNum = FrameNumber; |
|
|
|
|
|
FrameNumber++; |
|
|
|
|
|
bool send = SendMessage(request.Request.ReceiveIp, request.Request.Encode()); |
|
|
|
|
|
return send; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public bool SendNoRequest(DataMessage msg) |
|
|
|
|
|
{ |
|
|
|
|
|
msg.FrameNum = FrameNumber; |
|
|
|
|
|
FrameNumber++; |
|
|
|
|
|
bool send = SendMessage(msg.ReceiveIp, msg.Encode()); |
|
|
|
|
|
return send; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public bool SendResponse(DataMessage msg) |
|
|
|
|
|
{ |
|
|
{ |
|
|
bool send = SendMessage(msg.ReceiveIp, msg.Encode()); |
|
|
if (!client.IsOnline) return false; |
|
|
return send; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x10, 0); |
|
|
|
|
|
return SendResponse(msg, mm.FrameNum); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 0x01:心跳
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
public bool SendHostHeart_01(ClientMessage client) |
|
|
public bool SendHostHeart_01(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
@ -342,6 +341,12 @@ public class TcpManager |
|
|
return SendNoRequest(msg); |
|
|
return SendNoRequest(msg); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 0x02:设置传感器地址
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <param name="newSensorAddr"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
public bool RequestSensorChange_02(ClientMessage client, int newSensorAddr) |
|
|
public bool RequestSensorChange_02(ClientMessage client, int newSensorAddr) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
@ -351,9 +356,15 @@ public class TcpManager |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
return SendRequest(ref request); |
|
|
return SendRequestTry(ref request); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 0x04:请求传感器状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <param name="sensorAddr"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
public bool RequestSensorState_04(ClientMessage client, int sensorAddr) |
|
|
public bool RequestSensorState_04(ClientMessage client, int sensorAddr) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
@ -364,13 +375,17 @@ public class TcpManager |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
return SendRequest(ref request); |
|
|
return SendRequestTry(ref request); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 0x04:请求传感器状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
public bool RequestSensorsState_04(ClientMessage client) |
|
|
public bool RequestSensorsState_04(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
if (client.SensorsEmpty) return false; |
|
|
|
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x04, 2); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x04, 2); |
|
|
foreach (var item in client.SensorDict.Values) |
|
|
foreach (var item in client.SensorDict.Values) |
|
|
{ |
|
|
{ |
|
@ -380,11 +395,17 @@ public class TcpManager |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
SendRequest(ref request); |
|
|
SendRequestTry(ref request); |
|
|
} |
|
|
} |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 0x05:复位传感器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <param name="sensorAddr"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
public bool RequestSensorReset_05(ClientMessage client, int sensorAddr) |
|
|
public bool RequestSensorReset_05(ClientMessage client, int sensorAddr) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
@ -395,9 +416,14 @@ public class TcpManager |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
return SendRequest(ref request); |
|
|
return SendRequestTry(ref request); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 0x07:请求传感器列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
public bool RequestSensorList_07(ClientMessage client) |
|
|
public bool RequestSensorList_07(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
@ -406,27 +432,50 @@ public class TcpManager |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
return SendRequest(ref request); |
|
|
return SendRequestMust(ref request); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool RequestSensorStateActively_08(ClientMessage client, int sensorAddr) |
|
|
/// <summary>
|
|
|
|
|
|
/// 0x08:主机响应0x18主动上传状态
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <param name="mm"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool ResponseHostUploadState_08(ClientMessage client, DataMessage mm) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.SensorDict.TryGetValue(sensorAddr, out _)) return false; |
|
|
|
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x08, 2); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x08, 2); |
|
|
|
|
|
msg.Data = new byte[] { mm.Data[0], mm.Data[1] }; |
|
|
|
|
|
return SendResponse(msg, mm.FrameNum); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 0x09:关闭传感器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <param name="sensorAddr"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool RequestSensorTurnOff_09(ClientMessage client, int sensorAddr) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!client.IsOnline) return false; |
|
|
|
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x09, 2); |
|
|
msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; |
|
|
msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; |
|
|
DataRequest request = new() |
|
|
DataRequest request = new() |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
return SendRequest(ref request); |
|
|
return SendRequestTry(ref request); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool RequestSensorsStateActively_08(ClientMessage client) |
|
|
/// <summary>
|
|
|
|
|
|
/// 0x09:关闭传感器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool RequestSensorsTurnOff_09(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
if (client.SensorsEmpty) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x09, 2); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x08, 2); |
|
|
|
|
|
foreach (var item in client.SensorDict.Values) |
|
|
foreach (var item in client.SensorDict.Values) |
|
|
{ |
|
|
{ |
|
|
int sensorAddr = item.Addr; |
|
|
int sensorAddr = item.Addr; |
|
@ -435,42 +484,38 @@ public class TcpManager |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
SendRequest(ref request); |
|
|
SendRequestTry(ref request); |
|
|
} |
|
|
} |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool ResponseHostLogin_10(ClientMessage client, DataMessage mm) |
|
|
/// <summary>
|
|
|
{ |
|
|
/// 0x20:启动传感器
|
|
|
if (!client.IsOnline) return false; |
|
|
/// </summary>
|
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x10, 0); |
|
|
/// <param name="client"></param>
|
|
|
msg.FrameNum = mm.FrameNum; |
|
|
/// <param name="sensorAddr"></param>
|
|
|
return SendResponse(msg); |
|
|
/// <returns></returns>
|
|
|
} |
|
|
public bool RequestSensorTurnOn_20(ClientMessage client, int sensorAddr) |
|
|
|
|
|
|
|
|
public bool RequestSensorTurnOff_09(ClientMessage client, int sensorAddr) |
|
|
|
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
if (client.SensorsEmpty) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x20, 2); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x19, 2); |
|
|
|
|
|
foreach (var item in client.SensorDict.Values) |
|
|
|
|
|
{ |
|
|
|
|
|
int sensorAddr = item.Addr; |
|
|
|
|
|
msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; |
|
|
msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; |
|
|
DataRequest request = new() |
|
|
DataRequest request = new() |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
SendRequest(ref request); |
|
|
return SendRequestTry(ref request); |
|
|
} |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool RequestSensorsTurnOff_09(ClientMessage client) |
|
|
/// <summary>
|
|
|
|
|
|
/// 0x20:启动传感器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool RequestSensorsTurnOn_20(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
if (client.SensorsEmpty) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x20, 2); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x19, 2); |
|
|
|
|
|
foreach (var item in client.SensorDict.Values) |
|
|
foreach (var item in client.SensorDict.Values) |
|
|
{ |
|
|
{ |
|
|
int sensorAddr = item.Addr; |
|
|
int sensorAddr = item.Addr; |
|
@ -479,52 +524,57 @@ public class TcpManager |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
SendRequest(ref request); |
|
|
SendRequestTry(ref request); |
|
|
} |
|
|
} |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool RequestSensorTurnOn_20(ClientMessage client, int sensorAddr) |
|
|
/// <summary>
|
|
|
|
|
|
/// 0x21:启动全部传感器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool RequestSensorsTurnOn_21(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
if (client.SensorsEmpty) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x21, 0); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x20, 2); |
|
|
|
|
|
msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; |
|
|
|
|
|
DataRequest request = new() |
|
|
DataRequest request = new() |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
return SendRequest(ref request); |
|
|
return SendRequestTry(ref request); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool RequestSensorsTurnOn_20(ClientMessage client) |
|
|
/// <summary>
|
|
|
|
|
|
/// 0x24:启动自动上传
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool RequestHostAutoUploadState_24(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
if (client.SensorsEmpty) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x24, 0); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x20, 2); |
|
|
|
|
|
foreach (var item in client.SensorDict.Values) |
|
|
|
|
|
{ |
|
|
|
|
|
int sensorAddr = item.Addr; |
|
|
|
|
|
msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; |
|
|
|
|
|
DataRequest request = new() |
|
|
DataRequest request = new() |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
SendRequest(ref request); |
|
|
return SendRequestMust(ref request); |
|
|
} |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool RequestSensorsTurnOn_21(ClientMessage client) |
|
|
/// <summary>
|
|
|
|
|
|
/// 0x25:关闭自动上传
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="client"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public bool RequestSensorsTurnOn_25(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (!client.IsOnline) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
if (client.SensorsEmpty) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x25, 0); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x21, 0); |
|
|
|
|
|
DataRequest request = new() |
|
|
DataRequest request = new() |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
return SendRequest(ref request); |
|
|
return SendRequestTry(ref request); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#endregion Send
|
|
|
#endregion Send
|
|
@ -577,13 +627,6 @@ public class TcpManager |
|
|
return DeviceDict.Remove(clientIp, out _); |
|
|
return DeviceDict.Remove(clientIp, out _); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private bool UpdateDevice(string clientIp, TcpAlarmHost host) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!DeviceDict.ContainsKey(clientIp)) return false; |
|
|
|
|
|
DeviceDict[clientIp].Host = host; |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public List<ClientMessage> GetDeviceList() |
|
|
public List<ClientMessage> GetDeviceList() |
|
|
{ |
|
|
{ |
|
|
return DeviceDict.Values.ToList(); |
|
|
return DeviceDict.Values.ToList(); |
|
@ -593,36 +636,129 @@ public class TcpManager |
|
|
|
|
|
|
|
|
#region Set
|
|
|
#region Set
|
|
|
|
|
|
|
|
|
private byte FrameNumber { get; set; } = 0; |
|
|
private byte Frame { get; set; } = 0; |
|
|
|
|
|
|
|
|
private DataMessage GetSendMessageHead(int deviceId, ClientMessage client, byte fun_num, byte datalen) |
|
|
private byte FrameInc |
|
|
|
|
|
{ get { return (byte)(++Frame % byte.MaxValue); } } |
|
|
|
|
|
|
|
|
|
|
|
private int SendTryTime { get; set; } = 5; |
|
|
|
|
|
|
|
|
|
|
|
private int SendTryInterval { get; set; } = 200; |
|
|
|
|
|
|
|
|
|
|
|
private int ReqWaitTime { get; set; } = 3 * 10; |
|
|
|
|
|
|
|
|
|
|
|
private int ReqWaitInterval { get; set; } = 100; |
|
|
|
|
|
|
|
|
|
|
|
private DataMessage GetSendMessageHead(int deviceId, ClientMessage client, byte funNum, byte dataLen) |
|
|
{ |
|
|
{ |
|
|
DataMessage msg = new() |
|
|
DataMessage msg = new() |
|
|
{ |
|
|
{ |
|
|
DeviceId = deviceId, |
|
|
DeviceId = deviceId, |
|
|
SendIp = client.Host.Ip, |
|
|
SendIp = client.LocalIp, |
|
|
//SendIp = client.Client.Client.LocalEndPoint.ToString().Split(':')[0],
|
|
|
SendPort = client.LocalPort.ToInt(), |
|
|
SendPort = Server.Port, |
|
|
ReceiveIp = client.ClientIp, |
|
|
ReceiveIp = client.Ip, |
|
|
ReceivePort = client.ClientPort.ToInt(), |
|
|
ReceivePort = int.Parse(client.Port), |
|
|
FunctionNum = funNum, |
|
|
FunctionNum = fun_num, |
|
|
DataLen = dataLen |
|
|
DataLen = datalen |
|
|
|
|
|
}; |
|
|
}; |
|
|
return msg; |
|
|
return msg; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void SetDataRequest(DataMessage msg, byte functionNum) |
|
|
private bool SendMessage(string ip, byte[] bytes) |
|
|
{ |
|
|
{ |
|
|
lock (this) |
|
|
if (Server == null || !Server.IsRunning()) return false; |
|
|
|
|
|
if (!TryGetDevice(ip, out ClientMessage clientMsg)) return false; |
|
|
|
|
|
string cmd = DataMessage.ToHexString(bytes); |
|
|
|
|
|
bool send = false; |
|
|
|
|
|
for (int i = 0; i < SendTryTime; i++) |
|
|
{ |
|
|
{ |
|
|
//for (int i = 0; i < Reqlist.Count; i++)
|
|
|
send = Server.SendAsync(clientMsg.Client, bytes); |
|
|
//{
|
|
|
if (send) break; |
|
|
// if (Reqlist[i].Request.FunctionNum == functionNum && Reqlist[i].Request.FrameNum == msg.FrameNum)
|
|
|
Thread.Sleep(SendTryInterval); |
|
|
// {
|
|
|
|
|
|
// Reqlist[i].Responce = msg;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
Console.WriteLine("Send to {0}:{1} => {2}, send:{3}", clientMsg.ClientIp, clientMsg.ClientPort, cmd, send); |
|
|
|
|
|
return send; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private bool SendRequestTry(ref DataRequest request) |
|
|
|
|
|
{ |
|
|
|
|
|
if (request.Request == null) return false; |
|
|
|
|
|
request.Request.FrameNum = FrameInc; |
|
|
|
|
|
bool send = SendMessage(request.Request.ReceiveIp, request.Request.Encode()); |
|
|
|
|
|
if (!send) return false; |
|
|
|
|
|
bool respond = false; |
|
|
|
|
|
SetDataRequest(request); |
|
|
|
|
|
for (int i = 0; i < ReqWaitTime; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
respond = IsResponse(request); |
|
|
|
|
|
if (respond) break; |
|
|
|
|
|
Thread.Sleep(ReqWaitInterval); |
|
|
|
|
|
} |
|
|
|
|
|
RemoveDataRequest(request); |
|
|
|
|
|
return respond; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private bool SendRequestMust(ref DataRequest request) |
|
|
|
|
|
{ |
|
|
|
|
|
if (request.Request == null) return false; |
|
|
|
|
|
request.Request.FrameNum = FrameInc; |
|
|
|
|
|
bool send, respond = false; |
|
|
|
|
|
do |
|
|
|
|
|
{ |
|
|
|
|
|
send = SendMessage(request.Request.ReceiveIp, request.Request.Encode()); |
|
|
|
|
|
if (!send) continue; |
|
|
|
|
|
SetDataRequest(request); |
|
|
|
|
|
for (int i = 0; i < ReqWaitTime; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
respond = IsResponse(request); |
|
|
|
|
|
if (respond) break; |
|
|
|
|
|
Thread.Sleep(ReqWaitInterval); |
|
|
|
|
|
} |
|
|
|
|
|
} while (!send && !respond); |
|
|
|
|
|
RemoveDataRequest(request); |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private bool SendNoRequest(DataMessage request) |
|
|
|
|
|
{ |
|
|
|
|
|
request.FrameNum = FrameInc; |
|
|
|
|
|
bool send = SendMessage(request.ReceiveIp, request.Encode()); |
|
|
|
|
|
return send; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private bool SendResponse(DataMessage request, byte frameNum) |
|
|
|
|
|
{ |
|
|
|
|
|
request.FrameNum = frameNum; |
|
|
|
|
|
bool send = SendMessage(request.ReceiveIp, request.Encode()); |
|
|
|
|
|
return send; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private ConcurrentDictionary<string, DataRequest> DataRequestDict { get; } = new(); |
|
|
|
|
|
|
|
|
|
|
|
private void SetDataRequest(DataRequest request) |
|
|
|
|
|
{ |
|
|
|
|
|
string key = $"{request.Request.FunctionNum}-{request.Request.FrameNum}"; |
|
|
|
|
|
DataRequestDict[key] = request; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void SetDataResponse(DataMessage msg, byte funcNum) |
|
|
|
|
|
{ |
|
|
|
|
|
string key = $"{funcNum}-{msg.FrameNum}"; |
|
|
|
|
|
DataRequest? item = DataRequestDict[key]; |
|
|
|
|
|
if (item != null) item.Responce = msg; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void RemoveDataRequest(DataRequest request) |
|
|
|
|
|
{ |
|
|
|
|
|
string key = $"{request.Request.FunctionNum}-{request.Request.FrameNum}"; |
|
|
|
|
|
DataRequestDict.Remove(key); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private bool IsResponse(DataRequest request) |
|
|
|
|
|
{ |
|
|
|
|
|
string key = $"{request.Request.FunctionNum}-{request.Request.FrameNum}"; |
|
|
|
|
|
DataRequest? item = DataRequestDict[key]; |
|
|
|
|
|
return item != null && item.Responce != null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#endregion Set
|
|
|
#endregion Set
|
|
|