|
@ -91,6 +91,7 @@ public class TcpManager |
|
|
{ |
|
|
{ |
|
|
foreach (var clientMsg in DeviceDict.Values) |
|
|
foreach (var clientMsg in DeviceDict.Values) |
|
|
{ |
|
|
{ |
|
|
|
|
|
if (!clientMsg.IsOnline) continue; |
|
|
SendHostHeart_01(clientMsg); |
|
|
SendHostHeart_01(clientMsg); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -144,22 +145,20 @@ public class TcpManager |
|
|
|
|
|
|
|
|
#endregion Events
|
|
|
#endregion Events
|
|
|
|
|
|
|
|
|
#region Analysisbo
|
|
|
#region Analysis
|
|
|
|
|
|
|
|
|
private void AnalysisClientMessage(ref ClientMessage clientMsg) |
|
|
private void AnalysisClientMessage(ref ClientMessage clientMsg) |
|
|
{ |
|
|
{ |
|
|
//AA AA 01 C0 A8 01 05 28 23 C0 A8 01 64 88 13 7C 04 02 04 00 2B
|
|
|
|
|
|
//AA AA 01 C0 A8 01 64 88 13 C0 A8 01 05 28 23 24 14 04 0A 00 02 00 03
|
|
|
|
|
|
List<byte[]> msglist = clientMsg.GetMessageList(); |
|
|
List<byte[]> msglist = clientMsg.GetMessageList(); |
|
|
if (msglist == null || msglist.Count == 0) return; |
|
|
if (msglist == null || msglist.Count == 0) return; |
|
|
for (int i = 0; i < msglist.Count; i++) |
|
|
for (int i = 0; i < msglist.Count; i++) |
|
|
{ |
|
|
{ |
|
|
byte[] msg = msglist[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("Receive from {0}:{1} => {2}, {3}", clientMsg.Ip, clientMsg.Port, DataMessage.ToHexString(msg), vaild); |
|
|
if (!vaild) continue; |
|
|
if (!vaild) continue; |
|
|
DataMessage mm = new(); |
|
|
DataMessage mm = new(); |
|
|
mm.Decode(msglist[i]); |
|
|
mm.Decode(msglist[i]); |
|
|
Console.WriteLine("Receive from {0}:{1} => {2}, {3}", clientMsg.Ip, clientMsg.Port, DataMessage.ToHexString(msglist[i]), JsonUtil.ToJson(mm)); |
|
|
|
|
|
switch (mm.FunctionNum) |
|
|
switch (mm.FunctionNum) |
|
|
{ |
|
|
{ |
|
|
case 0x00: |
|
|
case 0x00: |
|
@ -168,26 +167,31 @@ public class TcpManager |
|
|
{ |
|
|
{ |
|
|
clientMsg.Host = new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString() }; |
|
|
clientMsg.Host = new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString() }; |
|
|
UpdateDevice(clientMsg.Ip, clientMsg.Host); |
|
|
UpdateDevice(clientMsg.Ip, clientMsg.Host); |
|
|
ProcessDeviceStateEvent(ref clientMsg, 1); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
ProcessDeviceStateEvent(ref clientMsg, 1); |
|
|
ResponseHostLogin_10(clientMsg, mm); |
|
|
ResponseHostLogin_10(clientMsg, mm); |
|
|
ClientMessage clientMsg2 = clientMsg; |
|
|
ClientMessage taskClientMsg = clientMsg; |
|
|
if (clientMsg2.SensorsEmpty && clientMsg2.SensorsLock.TryEnterWriteLock(1000)) |
|
|
if (taskClientMsg.SensorsEmpty && taskClientMsg.SensorsLock.TryEnterWriteLock(1000)) |
|
|
{ |
|
|
{ |
|
|
Task.Run(() => |
|
|
Task.Run(() => |
|
|
{ |
|
|
{ |
|
|
while (clientMsg2.SensorsEmpty) |
|
|
while (taskClientMsg.SensorsEmpty) |
|
|
{ |
|
|
{ |
|
|
RequestSensorList_07(clientMsg2); |
|
|
RequestSensorList_07(taskClientMsg); |
|
|
Thread.Sleep(1000); |
|
|
Thread.Sleep(1000); |
|
|
} |
|
|
} |
|
|
clientMsg2.SensorsLock.ExitWriteLock(); |
|
|
taskClientMsg.SensorsLock.ExitWriteLock(); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 0x01: |
|
|
case 0x01: |
|
|
Console.WriteLine("心跳:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("心跳:{0}", clientMsg.Ip); |
|
|
|
|
|
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); |
|
|
ProcessDeviceStateEvent(ref clientMsg, 1); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
@ -202,16 +206,11 @@ public class TcpManager |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case 0x14: |
|
|
case 0x14: |
|
|
Console.WriteLine("传感器状态响应:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("传感器轮询状态响应:{0}", clientMsg.Ip); |
|
|
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 online = state % 2; |
|
|
int online = state % 2; |
|
|
int alarm = state / 2 % 2; |
|
|
int alarm = state / 2 % 2; |
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, online, alarm); |
|
|
ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, online, alarm); |
|
|
SetDataRequest(mm, 0x04); |
|
|
SetDataRequest(mm, 0x04); |
|
|
break; |
|
|
break; |
|
@ -223,11 +222,6 @@ public class TcpManager |
|
|
|
|
|
|
|
|
case 0x17: |
|
|
case 0x17: |
|
|
Console.WriteLine("返回传感器列表:{0}", clientMsg.Ip); |
|
|
Console.WriteLine("返回传感器列表:{0}", clientMsg.Ip); |
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
if (clientMsg.SensorsEmpty) |
|
|
if (clientMsg.SensorsEmpty) |
|
|
{ |
|
|
{ |
|
|
for (int j = 2; j < mm.Data.Length; j++) |
|
|
for (int j = 2; j < mm.Data.Length; j++) |
|
@ -236,10 +230,16 @@ public class TcpManager |
|
|
SensorState sensor = new(mm.DeviceId, sensorAddr, 0, 0); |
|
|
SensorState sensor = new(mm.DeviceId, sensorAddr, 0, 0); |
|
|
clientMsg.SensorDict[sensorAddr] = sensor; |
|
|
clientMsg.SensorDict[sensorAddr] = sensor; |
|
|
} |
|
|
} |
|
|
RequestSensorState_04(clientMsg); |
|
|
RequestSensorsStateActively_08(clientMsg); |
|
|
} |
|
|
} |
|
|
SetDataRequest(mm, 0x07); |
|
|
SetDataRequest(mm, 0x07); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case 0x18: |
|
|
|
|
|
Console.WriteLine("传感器主动状态响应:{0}", clientMsg.Ip); |
|
|
|
|
|
|
|
|
|
|
|
SetDataRequest(mm, 0x08); |
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -296,7 +296,7 @@ public class TcpManager |
|
|
OnVibrateTcpAlarm?.Invoke(alarmMsg); |
|
|
OnVibrateTcpAlarm?.Invoke(alarmMsg); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#endregion Analysisbo
|
|
|
#endregion Analysis
|
|
|
|
|
|
|
|
|
#region Send
|
|
|
#region Send
|
|
|
|
|
|
|
|
@ -337,29 +337,40 @@ public class TcpManager |
|
|
|
|
|
|
|
|
public bool SendHostHeart_01(ClientMessage client) |
|
|
public bool SendHostHeart_01(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (client.Host == null) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x01, 1); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x01, 1); |
|
|
msg.Data = new byte[] { 0xFF }; |
|
|
msg.Data = new byte[] { 0xFF }; |
|
|
return SendNoRequest(msg); |
|
|
return SendNoRequest(msg); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool RequestSensorChange_02(ClientMessage client) |
|
|
public bool RequestSensorChange_02(ClientMessage client, int newSensorAddr) |
|
|
{ |
|
|
{ |
|
|
if (client.Host == null || client.Host.Id < 0) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x02, 4); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x02, 4); |
|
|
int channel = 10; |
|
|
msg.Data = new byte[] { (byte)(newSensorAddr % 256), (byte)(newSensorAddr / 256), (100 % 256), (100 / 256) }; |
|
|
msg.Data = new byte[] { (byte)(channel % 256), (byte)(channel / 256), (100 % 256), (100 / 256) }; |
|
|
|
|
|
DataRequest request = new() |
|
|
DataRequest request = new() |
|
|
{ |
|
|
{ |
|
|
Request = msg |
|
|
Request = msg |
|
|
}; |
|
|
}; |
|
|
SendRequest(ref request); |
|
|
return SendRequest(ref request); |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public bool RequestSensorState_04(ClientMessage client, int sensorAddr) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!client.IsOnline) return false; |
|
|
|
|
|
if (!client.SensorDict.TryGetValue(sensorAddr, out _)) return false; |
|
|
|
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x04, 2); |
|
|
|
|
|
msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; |
|
|
|
|
|
DataRequest request = new() |
|
|
|
|
|
{ |
|
|
|
|
|
Request = msg |
|
|
|
|
|
}; |
|
|
|
|
|
return SendRequest(ref request); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool RequestSensorState_04(ClientMessage client) |
|
|
public bool RequestSensorsState_04(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (client.Host == null || client.Host.Id < 0) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
if (client.SensorsEmpty) 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) |
|
@ -377,21 +388,20 @@ public class TcpManager |
|
|
|
|
|
|
|
|
public bool RequestSensorReset_05(ClientMessage client, int sensorAddr) |
|
|
public bool RequestSensorReset_05(ClientMessage client, int sensorAddr) |
|
|
{ |
|
|
{ |
|
|
if (client.Host == null || client.Host.Id < 0) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
if (client.SensorsEmpty) return false; |
|
|
if (!client.SensorDict.TryGetValue(sensorAddr, out _)) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x05, 2); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x05, 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 |
|
|
}; |
|
|
}; |
|
|
SendRequest(ref request); |
|
|
return SendRequest(ref request); |
|
|
return true; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public bool RequestSensorList_07(ClientMessage client) |
|
|
public bool RequestSensorList_07(ClientMessage client) |
|
|
{ |
|
|
{ |
|
|
if (client.Host == null || client.Host.Id < 0) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x07, 0); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x07, 0); |
|
|
DataRequest request = new() |
|
|
DataRequest request = new() |
|
|
{ |
|
|
{ |
|
@ -400,9 +410,40 @@ public class TcpManager |
|
|
return SendRequest(ref request); |
|
|
return SendRequest(ref request); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public bool RequestSensorStateActively_08(ClientMessage client, int sensorAddr) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!client.IsOnline) return false; |
|
|
|
|
|
if (!client.SensorDict.TryGetValue(sensorAddr, out _)) return false; |
|
|
|
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x08, 2); |
|
|
|
|
|
msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; |
|
|
|
|
|
DataRequest request = new() |
|
|
|
|
|
{ |
|
|
|
|
|
Request = msg |
|
|
|
|
|
}; |
|
|
|
|
|
return SendRequest(ref request); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public bool RequestSensorsStateActively_08(ClientMessage client) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!client.IsOnline) return false; |
|
|
|
|
|
if (client.SensorsEmpty) return false; |
|
|
|
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x08, 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() |
|
|
|
|
|
{ |
|
|
|
|
|
Request = msg |
|
|
|
|
|
}; |
|
|
|
|
|
SendRequest(ref request); |
|
|
|
|
|
} |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public bool ResponseHostLogin_10(ClientMessage client, DataMessage mm) |
|
|
public bool ResponseHostLogin_10(ClientMessage client, DataMessage mm) |
|
|
{ |
|
|
{ |
|
|
if (client.Host == null) return false; |
|
|
if (!client.IsOnline) return false; |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x10, 0); |
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x10, 0); |
|
|
msg.FrameNum = mm.FrameNum; |
|
|
msg.FrameNum = mm.FrameNum; |
|
|
return SendResponse(msg); |
|
|
return SendResponse(msg); |
|
|