|
|
@ -14,7 +14,7 @@ public class TcpManager |
|
|
|
|
|
|
|
private TcpManagerConfig Config { get; set; } |
|
|
|
|
|
|
|
private ConcurrentDictionary<string, ClientMessage> ClientMessageDict { get; } = new(); |
|
|
|
private ConcurrentDictionary<string, ClientMessage> DeviceDict { get; } = new(); |
|
|
|
|
|
|
|
private Timer HeartTimer { get; } = new(); |
|
|
|
|
|
|
@ -22,21 +22,17 @@ public class TcpManager |
|
|
|
|
|
|
|
#region Event
|
|
|
|
|
|
|
|
public delegate void TcpDeviceMessageEvent(TcpAlarmHost host, string msg); |
|
|
|
public delegate void VibrateTcpDeviceStateEvent(ClientMessage device); |
|
|
|
|
|
|
|
public delegate void TcpSensorMessageEvent(TcpAlarmHost host, TcpSensorTable section, string msg); |
|
|
|
public delegate void VibrateTcpSensorStateEvent(SensorState sensor); |
|
|
|
|
|
|
|
public delegate void TcpSensorStateChangeEvent(SensorState state); |
|
|
|
public delegate void VibrateTcpAlarmEvent(TcpAlarmHostMessage msg); |
|
|
|
|
|
|
|
public delegate void TcpSensorAlarmEvent(TcpAlarmHostMessage msg); |
|
|
|
public event VibrateTcpDeviceStateEvent? OnVibrateTcpDeviceState; |
|
|
|
|
|
|
|
public event TcpDeviceMessageEvent? OnTcpDeviceMessage; |
|
|
|
public event VibrateTcpSensorStateEvent? OnVibrateTcpSensorState; |
|
|
|
|
|
|
|
public event TcpSensorMessageEvent? OnTcpSensorMessage; |
|
|
|
|
|
|
|
public event TcpSensorStateChangeEvent? OnTcpSensorStateChange; |
|
|
|
|
|
|
|
public event TcpSensorAlarmEvent? OnTcpSensorAlarm; |
|
|
|
public event VibrateTcpAlarmEvent? OnVibrateTcpAlarm; |
|
|
|
|
|
|
|
#endregion Event
|
|
|
|
|
|
|
@ -51,18 +47,12 @@ public class TcpManager |
|
|
|
public void Start(TcpManagerConfig config) |
|
|
|
{ |
|
|
|
if (IsRunning()) return; |
|
|
|
try |
|
|
|
{ |
|
|
|
Server = new(IPAddress.Any, config.ServerPort); |
|
|
|
Server.ClientConnected += Server_ClientConnected; |
|
|
|
Server.ClientDisconnected += Server_ClientDisconnected; |
|
|
|
Server.DatagramReceived += Server_DatagramReceived; |
|
|
|
Server.Start(); |
|
|
|
} |
|
|
|
catch (Exception) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
Server = new(IPAddress.Any, config.ServerPort); |
|
|
|
Server.ClientConnected += Server_ClientConnected; |
|
|
|
Server.ClientDisconnected += Server_ClientDisconnected; |
|
|
|
Server.DatagramReceived += Server_DatagramReceived; |
|
|
|
Server.Start(); |
|
|
|
|
|
|
|
HeartTimer.Close(); |
|
|
|
HeartTimer.Interval = 1000 * config.DeviceHeartKeep; |
|
|
|
HeartTimer.Elapsed += HeartTimer_Elapsed; |
|
|
@ -84,7 +74,7 @@ public class TcpManager |
|
|
|
Server.ClientDisconnected -= Server_ClientDisconnected; |
|
|
|
Server.DatagramReceived -= Server_DatagramReceived; |
|
|
|
Server = null; |
|
|
|
ClientMessageDict.Clear(); |
|
|
|
DeviceDict.Clear(); |
|
|
|
HeartTimer.Stop(); |
|
|
|
HeartTimer.Elapsed -= HeartTimer_Elapsed; |
|
|
|
} |
|
|
@ -101,20 +91,12 @@ public class TcpManager |
|
|
|
|
|
|
|
private void HeartTimer_Elapsed(object? sender, ElapsedEventArgs e) |
|
|
|
{ |
|
|
|
foreach (var clientMsg in ClientMessageDict.Values) |
|
|
|
foreach (var clientMsg in DeviceDict.Values) |
|
|
|
{ |
|
|
|
SendHostHeart_01(clientMsg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void StateSpanTimer_Elapsed(object? sender, ElapsedEventArgs e) |
|
|
|
{ |
|
|
|
foreach (var clientMsg in ClientMessageDict.Values) |
|
|
|
{ |
|
|
|
RequestSensorState_04(clientMsg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void Server_ClientConnected(object? sender, TcpClientConnectedEventArgs e) |
|
|
|
{ |
|
|
|
string clientKey = e.TcpClient.Client.RemoteEndPoint?.ToString() ?? ""; |
|
|
@ -122,19 +104,18 @@ public class TcpManager |
|
|
|
string clientIp = clientKey.Split(':')[0]; |
|
|
|
string clientPort = clientKey.Split(':')[1]; |
|
|
|
Console.WriteLine("上线:{0}", clientIp); |
|
|
|
if (!TryGetClientMessage(clientIp, out ClientMessage clientMsg)) |
|
|
|
if (!TryGetDevice(clientIp, out ClientMessage clientMsg)) |
|
|
|
{ |
|
|
|
clientMsg = new() |
|
|
|
{ |
|
|
|
Client = e.TcpClient, |
|
|
|
Host = new() { Id = -1, Ip = clientIp, Port = clientPort, Name = "未知设备" } |
|
|
|
Host = new() { Id = -1, Ip = clientIp, Port = clientPort } |
|
|
|
}; |
|
|
|
AddClientMessage(clientIp, clientMsg); |
|
|
|
AddDeivce(clientIp, clientMsg); |
|
|
|
} |
|
|
|
if (OnTcpDeviceMessage != null) |
|
|
|
else |
|
|
|
{ |
|
|
|
TcpAlarmHost host = clientMsg.Host ?? new() { Id = -1, Ip = clientIp, Port = clientPort, Name = "未知设备" }; |
|
|
|
OnTcpDeviceMessage(host, "报警主机上线"); |
|
|
|
ProcessDeviceStateEvent(ref clientMsg, 1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -145,14 +126,9 @@ public class TcpManager |
|
|
|
string clientIp = clientKey.Split(':')[0]; |
|
|
|
string clientPort = clientKey.Split(':')[1]; |
|
|
|
Console.WriteLine("下线:{0}", clientIp); |
|
|
|
if (TryGetClientMessage(clientIp, out ClientMessage clientMsg)) |
|
|
|
if (TryGetDevice(clientIp, out ClientMessage clientMsg)) |
|
|
|
{ |
|
|
|
RemoveClientMessage(clientIp); |
|
|
|
if (OnTcpDeviceMessage != null) |
|
|
|
{ |
|
|
|
TcpAlarmHost host = clientMsg.Host ?? new() { Id = -1, Ip = clientIp, Port = clientPort, Name = "未知设备" }; |
|
|
|
OnTcpDeviceMessage(host, "报警主机下线"); |
|
|
|
} |
|
|
|
ProcessDeviceStateEvent(ref clientMsg, 0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -161,61 +137,60 @@ public class TcpManager |
|
|
|
string clientKey = e.TcpClient.TcpClient.Client.RemoteEndPoint?.ToString() ?? ""; |
|
|
|
if (clientKey == "") return; |
|
|
|
string clientIp = clientKey.Split(':')[0]; |
|
|
|
if (TryGetClientMessage(clientIp, out ClientMessage clientMsg)) |
|
|
|
if (TryGetDevice(clientIp, out ClientMessage clientMsg)) |
|
|
|
{ |
|
|
|
clientMsg.AddData(e.Datagram); |
|
|
|
AnalysisClientMessage(clientMsg); |
|
|
|
AnalysisClientMessage(ref clientMsg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#endregion Events
|
|
|
|
|
|
|
|
#region Analysis
|
|
|
|
#region Analysisbo
|
|
|
|
|
|
|
|
private void AnalysisClientMessage(ClientMessage clientMsg) |
|
|
|
private void AnalysisClientMessage(ref ClientMessage clientMsg) |
|
|
|
{ |
|
|
|
//AA AA 01 C0 A8 01 64 88 13 C0 A8 01 05 28 23 74 00 01 01 18
|
|
|
|
//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(); |
|
|
|
if (msglist == null || msglist.Count == 0) return; |
|
|
|
for (int i = 0; i < msglist.Count; i++) |
|
|
|
{ |
|
|
|
byte[] msg = msglist[i]; |
|
|
|
bool vaild = msg.Length >= 19 && msg[0] == 0xAA && msg[1] == 0xAA; |
|
|
|
if (!vaild) continue; |
|
|
|
DataMessage mm = new(); |
|
|
|
mm.Decode(msglist[i]); |
|
|
|
Console.WriteLine("Receive from {0}:{1} => {2}, {3}", clientMsg.Ip, clientMsg.Port, DataMessage.ToHexString(msglist[i]), JsonUtil.ToJson(mm)); |
|
|
|
TcpAlarmHost host; |
|
|
|
switch (mm.FunctionNum) |
|
|
|
{ |
|
|
|
case 0x00: |
|
|
|
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(), Name = "未知设备" }; |
|
|
|
UpdateClientMessage(clientMsg.Ip, clientMsg.Host); |
|
|
|
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); |
|
|
|
if (clientMsg.SensorListEmpty && clientMsg.SensorListLock.TryEnterWriteLock(1000)) |
|
|
|
ClientMessage clientMsg2 = clientMsg; |
|
|
|
if (clientMsg2.SensorsEmpty && clientMsg2.SensorsLock.TryEnterWriteLock(1000)) |
|
|
|
{ |
|
|
|
Task.Run(() => |
|
|
|
{ |
|
|
|
while (clientMsg.SensorListEmpty) |
|
|
|
while (clientMsg2.SensorsEmpty) |
|
|
|
{ |
|
|
|
RequestSensorList_07(clientMsg); |
|
|
|
RequestSensorList_07(clientMsg2); |
|
|
|
Thread.Sleep(1000); |
|
|
|
} |
|
|
|
clientMsg.SensorListLock.ExitWriteLock(); |
|
|
|
clientMsg2.SensorsLock.ExitWriteLock(); |
|
|
|
}); |
|
|
|
} |
|
|
|
if (OnTcpDeviceMessage != null) |
|
|
|
{ |
|
|
|
host = clientMsg.Host ?? new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString(), Name = "未知设备" }; |
|
|
|
OnTcpDeviceMessage(host, "报警主机登录"); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case 0x01: |
|
|
|
Console.WriteLine("心跳:{0}", clientMsg.Ip); |
|
|
|
ProcessDeviceStateEvent(ref clientMsg, 1); |
|
|
|
break; |
|
|
|
|
|
|
|
case 0x12: |
|
|
@ -236,17 +211,11 @@ public class TcpManager |
|
|
|
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(), Name = "未知设备" }; |
|
|
|
UpdateClientMessage(clientMsg.Ip, clientMsg.Host); |
|
|
|
clientMsg.Host = new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString() }; |
|
|
|
UpdateDevice(clientMsg.Ip, clientMsg.Host); |
|
|
|
} |
|
|
|
ProcessOnlineEvent(clientMsg, mm, sensorAddr, online); |
|
|
|
ProcessAlarmEvent(clientMsg, mm, sensorAddr, alarm); |
|
|
|
UpdateOnLineAlarm(clientMsg, sensorAddr, online, alarm); |
|
|
|
ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, online, alarm); |
|
|
|
SetDataRequest(mm, 0x04); |
|
|
|
if (alarm == 1) |
|
|
|
{ |
|
|
|
RequestSensorReset_05(clientMsg); |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
case 0x15: |
|
|
@ -258,18 +227,17 @@ public class TcpManager |
|
|
|
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(), Name = "设备" }; |
|
|
|
UpdateClientMessage(clientMsg.Ip, clientMsg.Host); |
|
|
|
clientMsg.Host = new() { Id = mm.DeviceId, Ip = mm.SendIp, Port = mm.SendPort.ToString() }; |
|
|
|
UpdateDevice(clientMsg.Ip, clientMsg.Host); |
|
|
|
} |
|
|
|
if (clientMsg.SensorListEmpty) |
|
|
|
if (clientMsg.SensorsEmpty) |
|
|
|
{ |
|
|
|
List<byte> sensorList = new(); |
|
|
|
for (int j = 2; j < mm.Data.Length; j++) |
|
|
|
{ |
|
|
|
sensorList.Add(Convert.ToByte((mm.Data[j] + mm.Data[++j] * 256))); |
|
|
|
sensorAddr = Convert.ToByte((mm.Data[j] + mm.Data[++j] * 256)); |
|
|
|
SensorState sensor = new(mm.DeviceId, sensorAddr, 0, 0); |
|
|
|
clientMsg.SensorDict[sensorAddr] = sensor; |
|
|
|
} |
|
|
|
clientMsg.SensorList = sensorList; |
|
|
|
UpdateClientMessage(clientMsg.Ip, sensorList); |
|
|
|
RequestSensorState_04(clientMsg); |
|
|
|
} |
|
|
|
SetDataRequest(mm, 0x07); |
|
|
@ -278,70 +246,66 @@ public class TcpManager |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void ProcessOnlineEvent(ClientMessage client, DataMessage mm, int channel, int online) |
|
|
|
private void ProcessDeviceStateEvent(ref ClientMessage device, int online) |
|
|
|
{ |
|
|
|
if (client.SectionTable.ContainsKey(channel)) |
|
|
|
if (device.OnlineState != online) |
|
|
|
{ |
|
|
|
SensorState oldState = client.SectionTable[channel] as SensorState; |
|
|
|
if (oldState.online == online) return; |
|
|
|
device.OnlineState = online; |
|
|
|
ReportDeviceState(device); |
|
|
|
} |
|
|
|
TcpAlarmHost host = client.Host; |
|
|
|
TcpSensorTable section = new() { DeviceId = host.Id, Name = "未知传感器", Channel = channel.ToString() }; |
|
|
|
OnTcpSensorMessage?.Invoke(host, section, online == 0 ? "传感器上线" : "传感器离线"); |
|
|
|
} |
|
|
|
|
|
|
|
private void ProcessAlarmEvent(ClientMessage client, DataMessage mm, int channel, int alarm) |
|
|
|
private void ProcessSensorStateEvent(ref ClientMessage client, DataMessage mm, int sensorAddr, int offline, int alarm) |
|
|
|
{ |
|
|
|
if (client.SectionTable.ContainsKey(channel)) |
|
|
|
if (!client.SensorDict.TryGetValue(sensorAddr, out SensorState? sensor)) |
|
|
|
{ |
|
|
|
sensor = new(mm.DeviceId, sensorAddr, offline, alarm); |
|
|
|
client.SensorDict[sensorAddr] = sensor; |
|
|
|
ReportSensorState(sensor); |
|
|
|
} |
|
|
|
else if (sensor.OfflineState != offline || sensor.AlarmState != alarm) |
|
|
|
{ |
|
|
|
SensorState oldState = client.SectionTable[channel] as SensorState; |
|
|
|
if (oldState.alarm == alarm) return; |
|
|
|
sensor.OfflineState = offline; |
|
|
|
sensor.AlarmState = alarm; |
|
|
|
ReportSensorState(sensor); |
|
|
|
} |
|
|
|
TcpAlarmHost host = client.Host; |
|
|
|
if (alarm == 1) |
|
|
|
{ |
|
|
|
ProcessAlarm(host.Id, channel); |
|
|
|
ReportAlarm(sensor.DeviceId, sensorAddr); |
|
|
|
//RequestSensorReset_05(clientMsg, sensorAddr);
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void UpdateOnLineAlarm(ClientMessage client, int channel, int online, int alarm) |
|
|
|
private void ReportDeviceState(ClientMessage device) |
|
|
|
{ |
|
|
|
if (client.SectionTable.ContainsKey(channel)) |
|
|
|
{ |
|
|
|
SensorState oldState = client.SectionTable[channel] as SensorState; |
|
|
|
oldState.online = online; |
|
|
|
oldState.alarm = alarm; |
|
|
|
OnTcpSensorStateChange?.Invoke(oldState); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
TcpAlarmHost host = client.Host; |
|
|
|
SensorState state = new(host.Id, channel, online, alarm); |
|
|
|
client.SectionTable[channel] = state; |
|
|
|
OnTcpSensorStateChange?.Invoke(state); |
|
|
|
} |
|
|
|
OnVibrateTcpDeviceState?.Invoke(device); |
|
|
|
} |
|
|
|
|
|
|
|
private void ReportSensorState(SensorState sensor) |
|
|
|
{ |
|
|
|
OnVibrateTcpSensorState?.Invoke(sensor); |
|
|
|
} |
|
|
|
|
|
|
|
private void ProcessAlarm(int deviceId, int channel) |
|
|
|
private void ReportAlarm(int deviceId, int sensorAddr) |
|
|
|
{ |
|
|
|
TcpAlarmHostMessage alarmMsg = new() |
|
|
|
{ |
|
|
|
AlarmTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), |
|
|
|
CID = "1151", |
|
|
|
DeviceID = deviceId, |
|
|
|
SensorAddr = channel.ToString() |
|
|
|
SensorAddr = sensorAddr.ToString() |
|
|
|
}; |
|
|
|
OnTcpSensorAlarm?.Invoke(alarmMsg); |
|
|
|
OnVibrateTcpAlarm?.Invoke(alarmMsg); |
|
|
|
} |
|
|
|
|
|
|
|
#endregion Analysis
|
|
|
|
#endregion Analysisbo
|
|
|
|
|
|
|
|
#region Send
|
|
|
|
|
|
|
|
public bool SendMessage(string ip, byte[] bytes) |
|
|
|
{ |
|
|
|
if (Server == null || !Server.IsRunning()) return false; |
|
|
|
if (!TryGetClientMessage(ip, out ClientMessage clientMsg)) return false; |
|
|
|
if (!TryGetDevice(ip, out ClientMessage clientMsg)) return false; |
|
|
|
string cmd = DataMessage.ToHexString(bytes); |
|
|
|
DataMessage mm = new(); |
|
|
|
mm.Decode(bytes); |
|
|
@ -398,12 +362,12 @@ public class TcpManager |
|
|
|
public bool RequestSensorState_04(ClientMessage client) |
|
|
|
{ |
|
|
|
if (client.Host == null || client.Host.Id < 0) return false; |
|
|
|
if (client.SensorListEmpty) return false; |
|
|
|
if (client.SensorsEmpty) return false; |
|
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x04, 2); |
|
|
|
foreach (var item in client.SensorList) |
|
|
|
foreach (var item in client.SensorDict.Values) |
|
|
|
{ |
|
|
|
int channel = item; |
|
|
|
msg.Data = new byte[] { (byte)(channel % 256), (byte)(channel / 256) }; |
|
|
|
int sensorAddr = item.Addr; |
|
|
|
msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; |
|
|
|
DataRequest request = new() |
|
|
|
{ |
|
|
|
Request = msg |
|
|
@ -413,21 +377,17 @@ public class TcpManager |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
public bool RequestSensorReset_05(ClientMessage client) |
|
|
|
public bool RequestSensorReset_05(ClientMessage client, int sensorAddr) |
|
|
|
{ |
|
|
|
if (client.Host == null || client.Host.Id < 0) return false; |
|
|
|
if (client.SensorListEmpty) return false; |
|
|
|
if (client.SensorsEmpty) return false; |
|
|
|
DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x05, 2); |
|
|
|
foreach (var item in client.SensorList) |
|
|
|
msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; |
|
|
|
DataRequest request = new() |
|
|
|
{ |
|
|
|
int channel = item; |
|
|
|
msg.Data = new byte[] { (byte)(channel % 256), (byte)(channel / 256) }; |
|
|
|
DataRequest request = new() |
|
|
|
{ |
|
|
|
Request = msg |
|
|
|
}; |
|
|
|
SendRequest(ref request); |
|
|
|
} |
|
|
|
Request = msg |
|
|
|
}; |
|
|
|
SendRequest(ref request); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
@ -454,50 +414,62 @@ public class TcpManager |
|
|
|
|
|
|
|
#region ClientMessage
|
|
|
|
|
|
|
|
private bool ContainsClientMessage(string clientIp) |
|
|
|
public bool ContainsDevice(string clientIp) |
|
|
|
{ |
|
|
|
return DeviceDict.ContainsKey(clientIp); |
|
|
|
} |
|
|
|
|
|
|
|
public ClientMessage GetDevice(string clientIp) |
|
|
|
{ |
|
|
|
return ClientMessageDict.ContainsKey(clientIp); |
|
|
|
return DeviceDict[clientIp]; |
|
|
|
} |
|
|
|
|
|
|
|
private ClientMessage GetClientMessage(string clientIp) |
|
|
|
public bool TryGetDevice(string clientIp, out ClientMessage clientMsg) |
|
|
|
{ |
|
|
|
return ClientMessageDict[clientIp]; |
|
|
|
return DeviceDict.TryGetValue(clientIp, out clientMsg); |
|
|
|
} |
|
|
|
|
|
|
|
private bool TryGetClientMessage(string clientIp, out ClientMessage clientMsg) |
|
|
|
public bool TryGetDevice(int deviceId, out ClientMessage clientMsg) |
|
|
|
{ |
|
|
|
return ClientMessageDict.TryGetValue(clientIp, out clientMsg); |
|
|
|
foreach (var item in DeviceDict.Values) |
|
|
|
{ |
|
|
|
if (item.Host.Id == deviceId) |
|
|
|
{ |
|
|
|
clientMsg = item; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
clientMsg = null; |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
private bool AddClientMessage(string clientIp, ClientMessage clientMsg) |
|
|
|
private bool AddDeivce(string clientIp, ClientMessage clientMsg) |
|
|
|
{ |
|
|
|
if (ContainsClientMessage(clientIp)) return false; |
|
|
|
ClientMessageDict[clientIp] = clientMsg; |
|
|
|
if (ContainsDevice(clientIp)) return false; |
|
|
|
DeviceDict[clientIp] = clientMsg; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
private void SetClientMessage(string clientIp, ClientMessage clientMsg) |
|
|
|
private void SetDevice(string clientIp, ClientMessage clientMsg) |
|
|
|
{ |
|
|
|
ClientMessageDict[clientIp] = clientMsg; |
|
|
|
DeviceDict[clientIp] = clientMsg; |
|
|
|
} |
|
|
|
|
|
|
|
private bool RemoveClientMessage(string clientIp) |
|
|
|
private bool RemoveDevice(string clientIp) |
|
|
|
{ |
|
|
|
return ClientMessageDict.Remove(clientIp, out _); |
|
|
|
return DeviceDict.Remove(clientIp, out _); |
|
|
|
} |
|
|
|
|
|
|
|
private bool UpdateClientMessage(string clientIp, TcpAlarmHost host) |
|
|
|
private bool UpdateDevice(string clientIp, TcpAlarmHost host) |
|
|
|
{ |
|
|
|
if (!ClientMessageDict.ContainsKey(clientIp)) return false; |
|
|
|
ClientMessageDict[clientIp].Host = host; |
|
|
|
if (!DeviceDict.ContainsKey(clientIp)) return false; |
|
|
|
DeviceDict[clientIp].Host = host; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
private bool UpdateClientMessage(string clientIp, List<byte> sensorList) |
|
|
|
public List<ClientMessage> GetDeviceList() |
|
|
|
{ |
|
|
|
if (!ClientMessageDict.ContainsKey(clientIp)) return false; |
|
|
|
ClientMessageDict[clientIp].SensorList = sensorList; |
|
|
|
return true; |
|
|
|
return DeviceDict.Values.ToList(); |
|
|
|
} |
|
|
|
|
|
|
|
#endregion ClientMessage
|
|
|
|