diff --git a/JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs b/JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs index 6267006..5f2af5f 100644 --- a/JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs +++ b/JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs @@ -103,7 +103,7 @@ public class UdpManager //解码 byte[] msg = AlarmEncode.DecodeMessage(e.Datagram); bool vaild = msg.Length >= 8 && msg[2] == AlarmEncode.Head[0] && msg[3] == AlarmEncode.Head[1]; - Console.WriteLine("Receive from {0}:{1} => {2}, {3}", ipep.Address.ToString(), ipep.Port, DataMessage.ToHexString(msg), vaild); + Console.WriteLine("Recv from {0}:{1} => {2}, {3}", ipep.Address.ToString(), ipep.Port, DataMessage.ToHexString(msg), vaild); if (!vaild) return; //解析 DeviceCmd deviceCmd = (DeviceCmd)msg[4]; diff --git a/JiLinApp.Docking/FenceAlarm/Service/UdpServer.cs b/JiLinApp.Docking/FenceAlarm/Service/UdpServer.cs index 16da808..24a2e0b 100644 --- a/JiLinApp.Docking/FenceAlarm/Service/UdpServer.cs +++ b/JiLinApp.Docking/FenceAlarm/Service/UdpServer.cs @@ -117,16 +117,18 @@ public class UdpServer { IPEndPoint ipep = new(IPAddress.Parse(ip), port); int result = SendUdp.Send(msg, msg.Length, ipep); - bool flag = result >= msg.Length; - Console.WriteLine("Send to {0}:{1} => {2}, {3}", ip, port, DataMessage.ToHexString(msg), flag); + bool flag = result == msg.Length; + Console.WriteLine("Send to {0}:{1} => {2}, {3}", ip, port, DataMessage.ToHexString(AlarmEncode.DecodeMessage(msg)), flag); return flag; } public bool SendMessage(IPEndPoint ipep, byte[] msg) { + string ip = ipep.Address.ToString(); + int port = ipep.Port; int result = SendUdp.Send(msg, msg.Length, ipep); - bool flag = result >= msg.Length; - Console.WriteLine("Send to {0}:{1} => {2}, {3}", ipep.Address.ToString(), ipep.Port, DataMessage.ToHexString(msg), flag); + bool flag = result == msg.Length; + Console.WriteLine("Send to {0}:{1} => {2}, {3}", ip, port, DataMessage.ToHexString(AlarmEncode.DecodeMessage(msg)), flag); return flag; } diff --git a/JiLinApp.Docking/VibrateAlarm/Service/AsyncTcpServer.cs b/JiLinApp.Docking/VibrateAlarm/Service/AsyncTcpServer.cs index ebe89fe..09391a1 100644 --- a/JiLinApp.Docking/VibrateAlarm/Service/AsyncTcpServer.cs +++ b/JiLinApp.Docking/VibrateAlarm/Service/AsyncTcpServer.cs @@ -332,24 +332,23 @@ public class AsyncTcpServer : IDisposable /// /// 客户端 /// 报文 - public void SendAsync(TcpClient client, byte[] datagram) + public bool SendAsync(TcpClient client, byte[] datagram) { - if (!IsRunning()) return; - if (client == null || !client.Connected || datagram == null) return; + if (!IsRunning()) return false; + if (client == null || !client.Connected || datagram == null) return false; try { NetworkStream stream = client.GetStream(); - if (stream.CanWrite) - { - stream.BeginWrite(datagram, 0, datagram.Length, HandleDatagramWritten, client); - } + if (!stream.CanWrite) return false; + IAsyncResult result = stream.BeginWrite(datagram, 0, datagram.Length, HandleDatagramWritten, client); + return result.IsCompleted; } catch (Exception) { string clientKey = client.Client.RemoteEndPoint?.ToString() ?? ""; - if (clientKey == "") return; - if (client != null) RaiseClientDisconnected(clientKey, client); + if (clientKey != "" && client != null) RaiseClientDisconnected(clientKey, client); } + return false; } /// diff --git a/JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs b/JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs index 5dc29ba..d908225 100644 --- a/JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs +++ b/JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs @@ -1,5 +1,4 @@ -using EC.Util.Common; -using System.Collections.Concurrent; +using System.Collections.Concurrent; using System.Net; using System.Timers; using Timer = System.Timers.Timer; @@ -155,7 +154,7 @@ public class TcpManager { byte[] msg = msglist[i]; 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); + Console.WriteLine("Recv from {0}:{1} => {2}, {3}", clientMsg.Ip, clientMsg.Port, DataMessage.ToHexString(msg), vaild); if (!vaild) continue; DataMessage mm = new(); mm.Decode(msglist[i]); @@ -209,9 +208,9 @@ public class TcpManager Console.WriteLine("传感器轮询状态响应:{0}", clientMsg.Ip); int sensorAddr = mm.Data[0] + mm.Data[1] * 256; int state = mm.Data[2] + mm.Data[3] * 256; - int online = state % 2; + int offline = state % 2; int alarm = state / 2 % 2; - ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, online, alarm); + ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, offline, alarm); SetDataRequest(mm, 0x04); break; @@ -307,9 +306,9 @@ public class TcpManager string cmd = DataMessage.ToHexString(bytes); DataMessage mm = new(); mm.Decode(bytes); - Console.WriteLine("Send to {0}:{1} => {2}, {3}", ip, clientMsg.Port, cmd, JsonUtil.ToJson(mm)); - Server.SendAsync(clientMsg.Client, bytes); - return true; + 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) @@ -449,6 +448,85 @@ public class TcpManager return SendResponse(msg); } + public bool RequestSensorTurnOff_09(ClientMessage client, int sensorAddr) + { + if (!client.IsOnline) return false; + if (client.SensorsEmpty) return false; + 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) }; + DataRequest request = new() + { + Request = msg + }; + SendRequest(ref request); + } + return true; + } + + public bool RequestSensorsTurnOff_09(ClientMessage client) + { + if (!client.IsOnline) return false; + if (client.SensorsEmpty) return false; + 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) }; + DataRequest request = new() + { + Request = msg + }; + SendRequest(ref request); + } + return true; + } + + public bool RequestSensorTurnOn_20(ClientMessage client, int sensorAddr) + { + if (!client.IsOnline) return false; + if (client.SensorsEmpty) return false; + DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x20, 2); + msg.Data = new byte[] { (byte)(sensorAddr % 256), (byte)(sensorAddr / 256) }; + DataRequest request = new() + { + Request = msg + }; + return SendRequest(ref request); + } + + public bool RequestSensorsTurnOn_20(ClientMessage client) + { + if (!client.IsOnline) return false; + if (client.SensorsEmpty) return false; + 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() + { + Request = msg + }; + SendRequest(ref request); + } + return true; + } + + public bool RequestSensorsTurnOn_21(ClientMessage client) + { + if (!client.IsOnline) return false; + if (client.SensorsEmpty) return false; + DataMessage msg = GetSendMessageHead(client.Host.Id, client, 0x21, 0); + DataRequest request = new() + { + Request = msg + }; + return SendRequest(ref request); + } + #endregion Send #region ClientMessage