|
|
|
|
using HighWayIot.Log4net;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
|
|
|
|
namespace HighWayIot.TouchSocket
|
|
|
|
|
{
|
|
|
|
|
public class RFIDTcpClient
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private static LogHelper logHelper = LogHelper.Instance;
|
|
|
|
|
|
|
|
|
|
public bool State
|
|
|
|
|
{
|
|
|
|
|
get => client.Online;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
global::TouchSocket.Sockets.TcpClient client = new global::TouchSocket.Sockets.TcpClient();
|
|
|
|
|
|
|
|
|
|
public bool ClientStart(string clientIP, string clientPort, string serverIP, string serverPort)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.Connecting = (client, e) =>
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info($"正在连接 {client.IP} 客户端");
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
|
};//有客户端正在连接
|
|
|
|
|
client.Connected = (client, e) =>
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info($"成功连接 {client.IP} 客户端");
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
|
};//有客户端成功连接
|
|
|
|
|
client.Disconnecting = (client, e) =>
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info($"正在断开连接 {client.IP} 客户端");
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
|
};//有客户端正在断开连接,只有当主动断开时才有效。
|
|
|
|
|
client.Disconnected = (client, e) =>
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info($"断开连接 {client.IP} 客户端");
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
|
};//有客户端断开连接
|
|
|
|
|
client.Received = (client, e) =>
|
|
|
|
|
{
|
|
|
|
|
////从客户端收到信息
|
|
|
|
|
//var mes = Encoding.ASCII.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len
|
|
|
|
|
//logHelper.Info($"已从{client.IP}:{client.Port}接收到信息:{mes}");
|
|
|
|
|
|
|
|
|
|
BufferMemory(Encoding.ASCII.GetString(BufferAnalysis.SplitByteArray(e.ByteBlock.Buffer, 0, e.ByteBlock.Len)), client.IP);
|
|
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
client.Setup(new TouchSocketConfig()//载入配置
|
|
|
|
|
.SetRemoteIPHost($"{clientIP}:{clientPort}")
|
|
|
|
|
.ConfigureContainer(a =>//容器的配置顺序应该在最前面
|
|
|
|
|
{
|
|
|
|
|
a.AddConsoleLogger();//添加一个控制台日志注入(注意:在maui中控制台日志不可用)
|
|
|
|
|
})
|
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
|
{
|
|
|
|
|
//a.Add();//此处可以添加插件
|
|
|
|
|
}));
|
|
|
|
|
client.Connect($"{serverIP}:{serverPort}");//启动
|
|
|
|
|
logHelper.Info("监听服务启动成功");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("监听服务启动失败! 错误代码" + ex.ToString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ClientStop()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.Close();
|
|
|
|
|
logHelper.Info("监听服务关闭成功!");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("监听服务关闭失败! 错误代码" + ex.ToString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ClientDispose()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.Dispose();
|
|
|
|
|
logHelper.Info("监听服务释放成功!");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("监听服务释放失败! 错误代码" + ex.ToString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BufferMemory(string json, string ip)
|
|
|
|
|
{
|
|
|
|
|
JObject obj = JsonConvert.DeserializeObject<JObject>(json);
|
|
|
|
|
if (obj["status"] != null)
|
|
|
|
|
{
|
|
|
|
|
string str = obj["status"].Value<string>();
|
|
|
|
|
if (str == "HEART")
|
|
|
|
|
{
|
|
|
|
|
BufferAnalysis.HeartbeatSocket(ip);
|
|
|
|
|
}
|
|
|
|
|
else if (str == "WARNNING")
|
|
|
|
|
{
|
|
|
|
|
BufferAnalysis.RFIDStatusSocket(ip);
|
|
|
|
|
}
|
|
|
|
|
else if (str == "GR" || str == "NR")
|
|
|
|
|
{
|
|
|
|
|
BufferAnalysis.RFIDCodeSocket(obj, ip);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info($"{str}为未知类型!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logHelper.Info($"输入格式错误!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|