|
|
@ -151,9 +151,12 @@ public class TcpManager |
|
|
|
|
|
|
|
#region Analysis
|
|
|
|
|
|
|
|
private int SendCmdInterval { get; } = 3000; |
|
|
|
|
|
|
|
private void AnalysisClientMessage(ref ClientMessage clientMsg) |
|
|
|
{ |
|
|
|
List<byte[]> msgList = clientMsg.GetMessageList(); |
|
|
|
ClientMessage taskClientMsg = clientMsg; |
|
|
|
foreach (byte[] msg in msgList) |
|
|
|
{ |
|
|
|
bool vaild = msg.Length >= 19 && msg[0] == 0xAA && msg[1] == 0xAA; |
|
|
@ -167,10 +170,18 @@ public class TcpManager |
|
|
|
case 0x00: |
|
|
|
Console.WriteLine("主机登录:{0}", clientMsg.ClientAddr); |
|
|
|
ResponseHostLogin_10(clientMsg, mm); |
|
|
|
Thread.Sleep(200); |
|
|
|
RequestHostAutoUploadState_24(clientMsg); |
|
|
|
Thread.Sleep(200); |
|
|
|
RequestSensorList_07(clientMsg); |
|
|
|
|
|
|
|
if (clientMsg.Init_0_21) break; |
|
|
|
if (!Monitor.TryEnter(clientMsg.Init_0_21, 500)) break; |
|
|
|
Task.Run(() => |
|
|
|
{ |
|
|
|
while (!taskClientMsg.Init_0_21) |
|
|
|
{ |
|
|
|
Thread.Sleep(SendCmdInterval); |
|
|
|
RequestSensorsTurnOn_21(taskClientMsg); |
|
|
|
} |
|
|
|
Monitor.Exit(taskClientMsg.Init_0_21); |
|
|
|
}); |
|
|
|
break; |
|
|
|
|
|
|
|
case 0x01: |
|
|
@ -206,6 +217,7 @@ public class TcpManager |
|
|
|
|
|
|
|
case 0x17: |
|
|
|
Console.WriteLine("主机返回传感器列表:{0}", clientMsg.ClientAddr); |
|
|
|
clientMsg.Init_2_17 = true; |
|
|
|
for (int j = 0; j < mm.Data.Length; j++) |
|
|
|
{ |
|
|
|
sensorAddr = Convert.ToByte((mm.Data[j] + mm.Data[++j] * 256)); |
|
|
@ -240,12 +252,36 @@ public class TcpManager |
|
|
|
|
|
|
|
case 0x31: |
|
|
|
Console.WriteLine("传感器全部启动响应:{0}", clientMsg.ClientAddr); |
|
|
|
clientMsg.Init_0_21 = true; |
|
|
|
if (clientMsg.Init_1_24) break; |
|
|
|
if (!Monitor.TryEnter(clientMsg.Init_1_24, 500)) break; |
|
|
|
Task.Run(() => |
|
|
|
{ |
|
|
|
while (!taskClientMsg.Init_1_24) |
|
|
|
{ |
|
|
|
Thread.Sleep(SendCmdInterval); |
|
|
|
RequestHostAutoUploadState_24(taskClientMsg); |
|
|
|
} |
|
|
|
Monitor.Exit(taskClientMsg.Init_1_24); |
|
|
|
}); |
|
|
|
|
|
|
|
SetDataResponse(mm, 0x21); |
|
|
|
break; |
|
|
|
|
|
|
|
case 0x34: |
|
|
|
Console.WriteLine("主机启动自动上传响应:{0}", clientMsg.ClientAddr); |
|
|
|
clientMsg.Init_1_24 = true; |
|
|
|
if (clientMsg.Init_2_17) break; |
|
|
|
if (!Monitor.TryEnter(clientMsg.Init_2_17, 500)) break; |
|
|
|
Task.Run(() => |
|
|
|
{ |
|
|
|
while (!taskClientMsg.Init_2_17) |
|
|
|
{ |
|
|
|
Thread.Sleep(SendCmdInterval); |
|
|
|
RequestSensorList_07(taskClientMsg); |
|
|
|
} |
|
|
|
Monitor.Exit(taskClientMsg.Init_2_17); |
|
|
|
}); |
|
|
|
|
|
|
|
SetDataResponse(mm, 0x24); |
|
|
|
break; |
|
|
@ -434,7 +470,7 @@ public class TcpManager |
|
|
|
{ |
|
|
|
Request = msg |
|
|
|
}; |
|
|
|
return SendRequestMust(ref request); |
|
|
|
return SendRequestTry(ref request); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
@ -560,7 +596,7 @@ public class TcpManager |
|
|
|
{ |
|
|
|
Request = msg |
|
|
|
}; |
|
|
|
return SendRequestMust(ref request); |
|
|
|
return SendRequestTry(ref request); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
@ -643,13 +679,13 @@ public class TcpManager |
|
|
|
private byte FrameInc |
|
|
|
{ get { return (byte)(++Frame % byte.MaxValue); } } |
|
|
|
|
|
|
|
private int SendTryTime { get; set; } = 5; |
|
|
|
private int SendTryTime { get; set; } = 3; |
|
|
|
|
|
|
|
private int SendTryInterval { get; set; } = 200; |
|
|
|
private int SendTryInterval { get; set; } = 300; |
|
|
|
|
|
|
|
private int ReqWaitTime { get; set; } = 3 * 10; |
|
|
|
private int ReqWaitTime { get; set; } = 3; |
|
|
|
|
|
|
|
private int ReqWaitInterval { get; set; } = 100; |
|
|
|
private int ReqWaitInterval { get; set; } = 2000; |
|
|
|
|
|
|
|
private DataMessage GetSendMessageHead(int deviceId, ClientMessage client, byte funNum, byte dataLen) |
|
|
|
{ |
|
|
@ -686,37 +722,29 @@ public class TcpManager |
|
|
|
{ |
|
|
|
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); |
|
|
|
} |
|
|
|
bool send = SendMessage(request.Request.ReceiveIp, request.Request.Encode()); |
|
|
|
RemoveDataRequest(request); |
|
|
|
return respond; |
|
|
|
return send; |
|
|
|
} |
|
|
|
|
|
|
|
private bool SendRequestMust(ref DataRequest request) |
|
|
|
{ |
|
|
|
if (request.Request == null) return false; |
|
|
|
request.Request.FrameNum = FrameInc; |
|
|
|
SetDataRequest(request); |
|
|
|
bool send, respond = false; |
|
|
|
do |
|
|
|
{ |
|
|
|
send = SendMessage(request.Request.ReceiveIp, request.Request.Encode()); |
|
|
|
if (!send) continue; |
|
|
|
SetDataRequest(request); |
|
|
|
if (!send) { Thread.Sleep(SendTryInterval); continue; } |
|
|
|
for (int i = 0; i < ReqWaitTime; i++) |
|
|
|
{ |
|
|
|
respond = IsResponse(request); |
|
|
|
if (respond) break; |
|
|
|
Thread.Sleep(ReqWaitInterval); |
|
|
|
} |
|
|
|
} while (!send && !respond); |
|
|
|
} while (!send || !respond); |
|
|
|
RemoveDataRequest(request); |
|
|
|
return true; |
|
|
|
} |
|
|
@ -739,27 +767,26 @@ public class TcpManager |
|
|
|
|
|
|
|
private void SetDataRequest(DataRequest request) |
|
|
|
{ |
|
|
|
string key = $"{request.Request.FunctionNum}-{request.Request.FrameNum}"; |
|
|
|
string key = $"{request.Request.FunctionNum:X}-{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; |
|
|
|
string key = $"{funcNum:X}-{msg.FrameNum}"; |
|
|
|
if (DataRequestDict.TryGetValue(key, out DataRequest? item)) item.Responce = msg; |
|
|
|
} |
|
|
|
|
|
|
|
private void RemoveDataRequest(DataRequest request) |
|
|
|
{ |
|
|
|
string key = $"{request.Request.FunctionNum}-{request.Request.FrameNum}"; |
|
|
|
string key = $"{request.Request.FunctionNum:X}-{request.Request.FrameNum}"; |
|
|
|
DataRequestDict.Remove(key); |
|
|
|
} |
|
|
|
|
|
|
|
private bool IsResponse(DataRequest request) |
|
|
|
{ |
|
|
|
string key = $"{request.Request.FunctionNum}-{request.Request.FrameNum}"; |
|
|
|
DataRequest? item = DataRequestDict[key]; |
|
|
|
string key = $"{request.Request.FunctionNum:X}-{request.Request.FrameNum}"; |
|
|
|
DataRequestDict.TryGetValue(key, out DataRequest? item); |
|
|
|
return item != null && item.Responce != null; |
|
|
|
} |
|
|
|
|
|
|
|