2024-10-29 16:28:14 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Enums;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Extensions;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Models;
|
2024-09-30 17:10:43 +08:00
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
|
using TouchSocket.Sockets;
|
2024-10-28 16:23:39 +08:00
|
|
|
|
using JiShe.CollectBus.RabbitMQ.Senders;
|
2024-10-29 16:28:14 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-09-30 17:10:43 +08:00
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.Core.Plugins
|
|
|
|
|
|
{
|
2024-10-26 22:27:19 +08:00
|
|
|
|
public partial class TcpServiceReceivedPlugin : PluginBase
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
2024-10-28 16:23:39 +08:00
|
|
|
|
private readonly INSender _nSender;
|
2024-10-29 16:28:14 +08:00
|
|
|
|
private readonly ILogger<TcpServiceReceivedPlugin> _logger;
|
2024-10-26 22:27:19 +08:00
|
|
|
|
|
2024-10-29 16:28:14 +08:00
|
|
|
|
|
|
|
|
|
|
public TcpServiceReceivedPlugin(INSender nSender, ILogger<TcpServiceReceivedPlugin> logger)
|
2024-10-26 22:27:19 +08:00
|
|
|
|
{
|
2024-10-28 16:23:39 +08:00
|
|
|
|
_nSender = nSender;
|
2024-10-29 16:28:14 +08:00
|
|
|
|
_logger = logger;
|
2024-10-26 22:27:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-28 16:23:39 +08:00
|
|
|
|
|
2024-10-22 20:57:26 +08:00
|
|
|
|
[GeneratorPlugin(typeof(ITcpReceivedPlugin))]
|
|
|
|
|
|
public async Task OnTcpReceived(ITcpSessionClient client, ReceivedDataEventArgs e)
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
2024-10-25 19:11:43 +08:00
|
|
|
|
////TODO: 电表主站到集中器的协议都是376.1协议,集中器下发到电表协议分为645-07和modbus
|
|
|
|
|
|
////TODO: 水表主站到集中器的协议分为118和645-97协议
|
|
|
|
|
|
////TODO: 连接成功时获取档案信息,根据档案信息匹配协议正则获取协议服务进行监听发送
|
2024-09-30 17:10:43 +08:00
|
|
|
|
|
2024-10-25 19:11:43 +08:00
|
|
|
|
//const string protocolType = "StandardProtocol";
|
2024-10-22 09:28:58 +08:00
|
|
|
|
|
2024-10-26 22:27:19 +08:00
|
|
|
|
//var protocolPlugin = _serviceProvider.GetKeyedService<IProtocolPlugin>(protocolType);
|
2024-10-25 19:11:43 +08:00
|
|
|
|
//var protocolPluginInfo = await protocolPlugin.GetAsync();
|
|
|
|
|
|
//client.Logger.Info($"{protocolPluginInfo.Name},{protocolPluginInfo.RegularExpression}");
|
|
|
|
|
|
////从客户端收到信息
|
|
|
|
|
|
//var messageHexString = Convert.ToHexString(e.ByteBlock.Span);
|
|
|
|
|
|
//client.Logger.Info($"[TCP] 已从{client.GetIPPort()}接收到信息:{messageHexString}");
|
|
|
|
|
|
|
|
|
|
|
|
//await protocolPlugin.ReceivedAsync(client,e);
|
2024-10-29 16:28:14 +08:00
|
|
|
|
|
|
|
|
|
|
var messageHexString = Convert.ToHexString(e.ByteBlock.Span);
|
|
|
|
|
|
var hexStringList = messageHexString.StringToPairs();
|
|
|
|
|
|
var aFn = (int?)hexStringList.GetAnalyzeValue(CommandChunkEnum.AFN);
|
|
|
|
|
|
var fn = (int?)hexStringList.GetAnalyzeValue(CommandChunkEnum.FN);
|
|
|
|
|
|
|
|
|
|
|
|
if (aFn.HasValue && fn.HasValue)
|
2024-10-25 19:11:43 +08:00
|
|
|
|
{
|
2024-10-29 16:28:14 +08:00
|
|
|
|
if ((AFN)aFn == AFN.链路接口检测)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (fn)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 1://登录
|
|
|
|
|
|
await _nSender.SendToReceivedLoginAsync(new MessageReceivedLoginEvent(client.Id,client.IP, client.Port, messageHexString,""));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2://退出登录
|
|
|
|
|
|
await _nSender.SendToReceivedLoginAsync(new MessageReceivedLoginEvent(client.Id, client.IP, client.Port, messageHexString, ""));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3://心跳
|
|
|
|
|
|
await _nSender.SendToReceivedHeartbeatAsync(new MessageReceivedHeartbeatEvent(client.Id, client.IP, client.Port, messageHexString, ""));
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await _nSender.SendToReceivedAsync(new MessageReceivedEvent(client.Id, client.IP, client.Port, messageHexString, ""));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError($"指令初步解析失败,指令内容:{messageHexString}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-30 17:10:43 +08:00
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 20:57:26 +08:00
|
|
|
|
[GeneratorPlugin(typeof(ITcpConnectingPlugin))]
|
2024-10-29 16:28:14 +08:00
|
|
|
|
public async Task OnTcpConnecting(ITcpSessionClient client,ConnectingEventArgs e)
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
2024-10-29 16:28:14 +08:00
|
|
|
|
_logger.LogInformation($"[TCP] ID:{client.Id} IP:{client.GetIPPort()}正在连接中...");
|
2024-09-30 17:10:43 +08:00
|
|
|
|
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 20:57:26 +08:00
|
|
|
|
[GeneratorPlugin(typeof(ITcpConnectedPlugin))]
|
2024-10-29 16:28:14 +08:00
|
|
|
|
public async Task OnTcpConnected(ITcpSessionClient client,ConnectedEventArgs e)
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
2024-10-29 16:28:14 +08:00
|
|
|
|
_logger.LogInformation($"[TCP] ID:{client.Id} IP:{client.GetIPPort()}已连接");
|
2024-09-30 17:10:43 +08:00
|
|
|
|
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 20:57:26 +08:00
|
|
|
|
[GeneratorPlugin(typeof(ITcpClosedPlugin))]
|
|
|
|
|
|
public async Task OnTcpClosed(ITcpSessionClient client, ClosedEventArgs e)
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
2024-10-29 16:28:14 +08:00
|
|
|
|
_logger.LogInformation($"[TCP] ID:{client.Id} IP:{client.GetIPPort()}已关闭连接");
|
2024-09-30 17:10:43 +08:00
|
|
|
|
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|