JiShe.CollectBus/JiShe.CollectBus.Core/Plugins/TcpServiceReceivedPlugin.cs
2024-10-11 11:27:57 +08:00

69 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.DependencyInjection;
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace JiShe.CollectBus.Core.Plugins
{
public class TcpServiceReceivedPlugin(IServiceProvider serviceProvider) : PluginBase, ITcpReceivedPlugin,
ITcpConnectingPlugin, ITcpConnectedPlugin, ITcpClosedPlugin
{
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
{
//TODO:电表: 376.1 645-07 modbus 水表: 118 645-97
const string protocolType = "TestProtocol";
switch (protocolType)
{
//case "376":
// //todo登录拿到设备信息根据设备信息使用不同的协议解析服务
// break;
//case "645":
// //todo: 直接拿设备信息,根据设备信息使用不同的协议解析服务
// break;
}
var protocolPlugin = serviceProvider.GetKeyedService<IProtocolPlugin>(protocolType);
client.Logger.Info($"{protocolPlugin?.Get().Name},{protocolPlugin?.Get().RegularExpression}");
//从客户端收到信息
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();
}
}
}