Browse Source

[feat] 更新震动服务命令

master
fajiao 2 years ago
parent
commit
7ef2c4a433
  1. 2
      JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs
  2. 10
      JiLinApp.Docking/FenceAlarm/Service/UdpServer.cs
  3. 17
      JiLinApp.Docking/VibrateAlarm/Service/AsyncTcpServer.cs
  4. 94
      JiLinApp.Docking/VibrateAlarm/Service/TcpManager.cs

2
JiLinApp.Docking/FenceAlarm/Service/UdpManager.cs

@ -103,7 +103,7 @@ public class UdpManager
//解码 //解码
byte[] msg = AlarmEncode.DecodeMessage(e.Datagram); byte[] msg = AlarmEncode.DecodeMessage(e.Datagram);
bool vaild = msg.Length >= 8 && msg[2] == AlarmEncode.Head[0] && msg[3] == AlarmEncode.Head[1]; 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; if (!vaild) return;
//解析 //解析
DeviceCmd deviceCmd = (DeviceCmd)msg[4]; DeviceCmd deviceCmd = (DeviceCmd)msg[4];

10
JiLinApp.Docking/FenceAlarm/Service/UdpServer.cs

@ -117,16 +117,18 @@ public class UdpServer
{ {
IPEndPoint ipep = new(IPAddress.Parse(ip), port); IPEndPoint ipep = new(IPAddress.Parse(ip), port);
int result = SendUdp.Send(msg, msg.Length, ipep); int result = SendUdp.Send(msg, msg.Length, ipep);
bool flag = result >= msg.Length; bool flag = result == msg.Length;
Console.WriteLine("Send to {0}:{1} => {2}, {3}", ip, port, DataMessage.ToHexString(msg), flag); Console.WriteLine("Send to {0}:{1} => {2}, {3}", ip, port, DataMessage.ToHexString(AlarmEncode.DecodeMessage(msg)), flag);
return flag; return flag;
} }
public bool SendMessage(IPEndPoint ipep, byte[] msg) public bool SendMessage(IPEndPoint ipep, byte[] msg)
{ {
string ip = ipep.Address.ToString();
int port = ipep.Port;
int result = SendUdp.Send(msg, msg.Length, ipep); int result = SendUdp.Send(msg, msg.Length, ipep);
bool flag = result >= msg.Length; bool flag = result == msg.Length;
Console.WriteLine("Send to {0}:{1} => {2}, {3}", ipep.Address.ToString(), ipep.Port, DataMessage.ToHexString(msg), flag); Console.WriteLine("Send to {0}:{1} => {2}, {3}", ip, port, DataMessage.ToHexString(AlarmEncode.DecodeMessage(msg)), flag);
return flag; return flag;
} }

17
JiLinApp.Docking/VibrateAlarm/Service/AsyncTcpServer.cs

@ -332,24 +332,23 @@ public class AsyncTcpServer : IDisposable
/// </summary> /// </summary>
/// <param name="client">客户端</param> /// <param name="client">客户端</param>
/// <param name="datagram">报文</param> /// <param name="datagram">报文</param>
public void SendAsync(TcpClient client, byte[] datagram) public bool SendAsync(TcpClient client, byte[] datagram)
{ {
if (!IsRunning()) return; if (!IsRunning()) return false;
if (client == null || !client.Connected || datagram == null) return; if (client == null || !client.Connected || datagram == null) return false;
try try
{ {
NetworkStream stream = client.GetStream(); NetworkStream stream = client.GetStream();
if (stream.CanWrite) if (!stream.CanWrite) return false;
{ IAsyncResult result = stream.BeginWrite(datagram, 0, datagram.Length, HandleDatagramWritten, client);
stream.BeginWrite(datagram, 0, datagram.Length, HandleDatagramWritten, client); return result.IsCompleted;
}
} }
catch (Exception) catch (Exception)
{ {
string clientKey = client.Client.RemoteEndPoint?.ToString() ?? ""; string clientKey = client.Client.RemoteEndPoint?.ToString() ?? "";
if (clientKey == "") return; if (clientKey != "" && client != null) RaiseClientDisconnected(clientKey, client);
if (client != null) RaiseClientDisconnected(clientKey, client);
} }
return false;
} }
/// <summary> /// <summary>

94
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.Net;
using System.Timers; using System.Timers;
using Timer = System.Timers.Timer; using Timer = System.Timers.Timer;
@ -155,7 +154,7 @@ public class TcpManager
{ {
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); Console.WriteLine("Recv 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]);
@ -209,9 +208,9 @@ public class TcpManager
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 offline = state % 2;
int alarm = state / 2 % 2; int alarm = state / 2 % 2;
ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, online, alarm); ProcessSensorStateEvent(ref clientMsg, mm, sensorAddr, offline, alarm);
SetDataRequest(mm, 0x04); SetDataRequest(mm, 0x04);
break; break;
@ -307,9 +306,9 @@ public class TcpManager
string cmd = DataMessage.ToHexString(bytes); string cmd = DataMessage.ToHexString(bytes);
DataMessage mm = new(); DataMessage mm = new();
mm.Decode(bytes); mm.Decode(bytes);
Console.WriteLine("Send to {0}:{1} => {2}, {3}", ip, clientMsg.Port, cmd, JsonUtil.ToJson(mm)); bool flag = Server.SendAsync(clientMsg.Client, bytes);
Server.SendAsync(clientMsg.Client, bytes); Console.WriteLine("Send to {0}:{1} => {2}, {3}", ip, clientMsg.Port, cmd, flag);
return true; return flag;
} }
public bool SendRequest(ref DataRequest request) public bool SendRequest(ref DataRequest request)
@ -449,6 +448,85 @@ public class TcpManager
return SendResponse(msg); 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 #endregion Send
#region ClientMessage #region ClientMessage

Loading…
Cancel
Save