2025-04-25 14:23:06 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.Ammeters;
|
2025-03-14 14:28:04 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.Devices;
|
2025-04-15 18:58:38 +08:00
|
|
|
|
using JiShe.CollectBus.Kafka.Producer;
|
2025-04-25 14:23:06 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Interfaces;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol3761;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-04-25 14:23:06 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
using Volo.Abp.Caching;
|
|
|
|
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
|
|
using Volo.Abp.Domain.Repositories;
|
|
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.Plugins
|
|
|
|
|
|
{
|
2025-03-27 08:38:19 +08:00
|
|
|
|
public partial class TcpMonitor : PluginBase, ITransientDependency, ITcpReceivedPlugin, ITcpConnectingPlugin, ITcpConnectedPlugin, ITcpClosedPlugin
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-04-15 18:58:38 +08:00
|
|
|
|
private readonly IProducerService _producerService;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
private readonly ILogger<TcpMonitor> _logger;
|
|
|
|
|
|
private readonly IRepository<Device, Guid> _deviceRepository;
|
|
|
|
|
|
private readonly IDistributedCache<AmmeterInfo> _ammeterInfoCache;
|
2025-04-21 23:47:11 +08:00
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2025-04-23 15:10:20 +08:00
|
|
|
|
private readonly IProtocolService _protocolService;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
|
2025-04-23 15:10:20 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
///
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="producerService"></param>
|
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
|
/// <param name="deviceRepository"></param>
|
|
|
|
|
|
/// <param name="ammeterInfoCache"></param>
|
|
|
|
|
|
/// <param name="serviceProvider"></param>
|
2025-04-18 09:50:00 +08:00
|
|
|
|
public TcpMonitor(IProducerService producerService,
|
2025-03-27 08:38:19 +08:00
|
|
|
|
ILogger<TcpMonitor> logger,
|
|
|
|
|
|
IRepository<Device, Guid> deviceRepository,
|
2025-04-23 15:10:20 +08:00
|
|
|
|
IDistributedCache<AmmeterInfo> ammeterInfoCache, IServiceProvider serviceProvider, IProtocolService protocolService)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-04-15 18:58:38 +08:00
|
|
|
|
_producerService = producerService;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_deviceRepository = deviceRepository;
|
|
|
|
|
|
_ammeterInfoCache = ammeterInfoCache;
|
2025-04-21 23:47:11 +08:00
|
|
|
|
_serviceProvider= serviceProvider;
|
2025-04-23 15:10:20 +08:00
|
|
|
|
_protocolService = protocolService;
|
2025-04-21 23:47:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-27 08:38:19 +08:00
|
|
|
|
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
var messageHexString = Convert.ToHexString(e.ByteBlock.Span);
|
2025-04-23 15:10:20 +08:00
|
|
|
|
var protocolPlugin = await _protocolService.GetProtocolServiceAsync("376.1");
|
2025-04-21 23:47:11 +08:00
|
|
|
|
if (protocolPlugin == null)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-04-21 23:47:11 +08:00
|
|
|
|
_logger.LogError("协议不存在!");
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
2025-04-23 15:10:20 +08:00
|
|
|
|
var tcpSessionClient = (ITcpSessionClient)client;
|
2025-04-27 15:30:45 +08:00
|
|
|
|
|
2025-04-21 23:47:11 +08:00
|
|
|
|
TB3761? tB3761 = await protocolPlugin!.AnalyzeAsync<TB3761>(tcpSessionClient, messageHexString);
|
2025-04-22 09:34:59 +08:00
|
|
|
|
if (tB3761 == null)
|
|
|
|
|
|
{
|
2025-04-25 09:28:56 +08:00
|
|
|
|
// TODO: 暂时不处理,后续再处理
|
2025-04-22 09:34:59 +08:00
|
|
|
|
_logger.LogError($"指令初步解析失败,指令内容:{messageHexString}");
|
|
|
|
|
|
}
|
2025-04-25 09:28:56 +08:00
|
|
|
|
await e.InvokeNext();
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-27 15:30:45 +08:00
|
|
|
|
|
2025-03-27 08:38:19 +08:00
|
|
|
|
public async Task OnTcpConnecting(ITcpSession client, ConnectingEventArgs e)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-03-27 08:38:19 +08:00
|
|
|
|
var tcpSessionClient = (ITcpSessionClient)client;
|
2025-04-27 15:30:45 +08:00
|
|
|
|
|
2025-03-27 08:38:19 +08:00
|
|
|
|
_logger.LogInformation($"[TCP] ID:{tcpSessionClient.Id} IP:{client.GetIPPort()}正在连接中...");
|
2024-12-19 16:07:07 +08:00
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-27 08:38:19 +08:00
|
|
|
|
public async Task OnTcpConnected(ITcpSession client, ConnectedEventArgs e)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-03-27 08:38:19 +08:00
|
|
|
|
var tcpSessionClient = (ITcpSessionClient)client;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"[TCP] ID:{tcpSessionClient.Id} IP:{client.GetIPPort()}已连接");
|
2024-12-19 16:07:07 +08:00
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-27 08:38:19 +08:00
|
|
|
|
public async Task OnTcpClosed(ITcpSession client, ClosedEventArgs e)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-04-27 15:30:45 +08:00
|
|
|
|
//todo: 删除Redis缓存
|
2025-03-27 08:38:19 +08:00
|
|
|
|
var tcpSessionClient = (ITcpSessionClient)client;
|
|
|
|
|
|
var entity = await _deviceRepository.FindAsync(a => a.ClientId == tcpSessionClient.Id);
|
2024-12-19 16:07:07 +08:00
|
|
|
|
if (entity != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
entity.UpdateByOnClosed();
|
|
|
|
|
|
await _deviceRepository.UpdateAsync(entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-03-27 08:38:19 +08:00
|
|
|
|
_logger.LogWarning($"[TCP] ID:{tcpSessionClient.Id} IP:{client.GetIPPort()}已关闭连接,但采集程序检索失败");
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await e.InvokeNext();
|
|
|
|
|
|
}
|
2025-03-12 09:58:37 +08:00
|
|
|
|
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|