JiShe.CollectBus/JiShe.CollectBus.Core/Plugins/TcpServiceReceivedPlugin.cs

62 lines
2.3 KiB
C#
Raw Normal View History

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-22 09:28:58 +08:00
//TODO: 电表主站到集中器的协议都是376.1协议集中器下发到电表协议分为645-07和modbus
//TODO: 水表主站到集中器的协议分为118和645-97协议
//TODO: 连接成功时获取档案信息,根据档案信息匹配协议正则获取协议服务进行监听发送
2024-09-30 17:10:43 +08:00
2024-10-12 23:42:15 +08:00
const string protocolType = "StandardProtocol";
2024-10-22 09:28:58 +08:00
2024-10-11 11:27:57 +08:00
var 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}");
2024-10-21 16:24:29 +08:00
protocolPlugin?.Received(e);
2024-09-30 17:10:43 +08:00
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();
}
}
}