2024-10-11 08:40:59 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-10-08 15:06:08 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
|
2024-09-30 17:10:43 +08:00
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.Core.Plugins
|
|
|
|
|
|
{
|
2024-10-11 08:40:59 +08:00
|
|
|
|
public class TcpServiceReceivedPlugin(IServiceProvider serviceProvider) : PluginBase, ITcpReceivedPlugin,
|
|
|
|
|
|
ITcpConnectingPlugin, ITcpConnectedPlugin, ITcpClosedPlugin
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
|
|
|
|
|
|
{
|
2024-10-11 08:40:59 +08:00
|
|
|
|
//TODO:电表: 376.1 645-07 modbus 水表: 118 645-97
|
2024-09-30 17:10:43 +08:00
|
|
|
|
|
2024-10-08 15:06:08 +08:00
|
|
|
|
var protocolType = "TestProtocol";
|
|
|
|
|
|
IProtocolPlugin? protocolPlugin;
|
2024-09-30 17:10:43 +08:00
|
|
|
|
switch (protocolType)
|
|
|
|
|
|
{
|
2024-10-08 15:06:08 +08:00
|
|
|
|
//case "376":
|
|
|
|
|
|
// //todo:登录拿到设备信息,根据设备信息使用不同的协议解析服务
|
|
|
|
|
|
// break;
|
|
|
|
|
|
//case "645":
|
|
|
|
|
|
// //todo: 直接拿设备信息,根据设备信息使用不同的协议解析服务
|
|
|
|
|
|
// break;
|
2024-09-30 17:10:43 +08:00
|
|
|
|
}
|
2024-10-11 08:40:59 +08:00
|
|
|
|
protocolPlugin = serviceProvider.GetKeyedService<IProtocolPlugin>(protocolType);
|
2024-10-08 15:06:08 +08:00
|
|
|
|
client.Logger.Info($"{protocolPlugin?.Get().Name},{protocolPlugin?.Get().RegularExpression}");
|
2024-09-30 17:10:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//从客户端收到信息
|
|
|
|
|
|
var messageHexString = Convert.ToHexString(e.ByteBlock.Span);
|
|
|
|
|
|
client.Logger.Info($"[TCP] 已从{client.GetIPPort()}接收到信息:{messageHexString}");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task OnTcpConnecting(ITcpSession client, ConnectingEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (client is ITcpSessionClient sessionClient)
|
|
|
|
|
|
{
|
|
|
|
|
|
client.Logger.Info($"[TCP] ID:{sessionClient.Id} IP:{client.GetIPPort()}正在连接中...");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task OnTcpConnected(ITcpSession client, ConnectedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (client is ITcpSessionClient sessionClient)
|
|
|
|
|
|
{
|
|
|
|
|
|
client.Logger.Info($"[TCP] ID:{sessionClient.Id} IP:{client.GetIPPort()}已连接");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task OnTcpClosed(ITcpSession client, ClosedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (client is ITcpSessionClient sessionClient)
|
|
|
|
|
|
{
|
|
|
|
|
|
client.Logger.Info($"[TCP] ID:{sessionClient.Id} IP:{client.GetIPPort()}已关闭连接");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|