2024-12-19 16:07:07 +08:00
|
|
|
|
using System;
|
2025-04-22 16:48:53 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-04-14 16:41:41 +08:00
|
|
|
|
using System.Linq;
|
2025-03-27 08:38:19 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2025-03-27 08:38:19 +08:00
|
|
|
|
using DeviceDetectorNET.Parser.Device;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using JiShe.CollectBus.Ammeters;
|
2025-04-17 14:39:14 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Consts;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Enums;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Extensions;
|
2025-04-15 18:58:38 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Helpers;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using JiShe.CollectBus.Enums;
|
|
|
|
|
|
using JiShe.CollectBus.Interceptors;
|
2025-03-14 14:28:04 +08:00
|
|
|
|
using JiShe.CollectBus.IotSystems.Devices;
|
|
|
|
|
|
using JiShe.CollectBus.IotSystems.MessageReceiveds;
|
2025-04-15 18:58:38 +08:00
|
|
|
|
using JiShe.CollectBus.Kafka.Producer;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts;
|
2025-04-21 23:47:11 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Abstracts;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
|
|
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Models;
|
2025-04-23 15:10:20 +08:00
|
|
|
|
using JiShe.CollectBus.Protocol.Contracts.Services;
|
2025-04-21 23:47:11 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
using Volo.Abp.Caching;
|
|
|
|
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
|
|
using Volo.Abp.Domain.Entities;
|
|
|
|
|
|
using Volo.Abp.Domain.Repositories;
|
2025-04-21 23:47:11 +08:00
|
|
|
|
using static System.Formats.Asn1.AsnWriter;
|
2025-04-08 23:15:46 +08:00
|
|
|
|
using static FreeSql.Internal.GlobalFilter;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError($"指令初步解析失败,指令内容:{messageHexString}");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await OnTcpNormalReceived(tcpSessionClient, messageHexString, tB3761);
|
|
|
|
|
|
}
|
|
|
|
|
|
await e.InvokeNext();
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-27 08:38:19 +08:00
|
|
|
|
//[GeneratorPlugin(typeof(ITcpConnectingPlugin))]
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
_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
|
|
|
|
//[GeneratorPlugin(typeof(ITcpConnectedPlugin))]
|
|
|
|
|
|
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
|
|
|
|
//[GeneratorPlugin(typeof(ITcpClosedPlugin))]//ITcpSessionClient
|
|
|
|
|
|
public async Task OnTcpClosed(ITcpSession client, ClosedEventArgs e)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
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
|
|
|
|
|
2025-04-08 23:15:46 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 正常帧处理,将不同的AFN进行分发
|
|
|
|
|
|
/// </summary>
|
2025-04-22 09:34:59 +08:00
|
|
|
|
/// <param name="tcpSessionClient"></param>
|
2025-04-08 23:15:46 +08:00
|
|
|
|
/// <param name="messageHexString"></param>
|
2025-04-22 09:34:59 +08:00
|
|
|
|
/// <param name="tB3761"></param>
|
2025-04-08 23:15:46 +08:00
|
|
|
|
/// <returns></returns>
|
2025-04-22 09:34:59 +08:00
|
|
|
|
private async Task OnTcpNormalReceived(ITcpSessionClient tcpSessionClient,string messageHexString, TB3761? tB3761)
|
2024-12-19 16:07:07 +08:00
|
|
|
|
{
|
2025-04-07 21:50:50 +08:00
|
|
|
|
//await _producerBus.Publish(new MessageReceived
|
2025-04-01 22:50:34 +08:00
|
|
|
|
//{
|
|
|
|
|
|
// ClientId = client.Id,
|
|
|
|
|
|
// ClientIp = client.IP,
|
|
|
|
|
|
// ClientPort = client.Port,
|
|
|
|
|
|
// MessageHexString = messageHexString,
|
|
|
|
|
|
// DeviceNo = deviceNo,
|
|
|
|
|
|
// MessageId = NewId.NextGuid().ToString()
|
|
|
|
|
|
//});
|
2025-04-07 21:50:50 +08:00
|
|
|
|
|
2025-04-16 09:54:21 +08:00
|
|
|
|
|
2025-04-10 17:06:53 +08:00
|
|
|
|
//string topicName = string.Format(ProtocolConst.AFNTopicNameFormat, aFn);
|
2025-04-10 23:31:43 +08:00
|
|
|
|
//todo 如何确定时标?目前集中器的采集频率,都是固定,数据上报的时候,根据当前时间,往后推测出应当采集的时间点作为时标。但是如果由于网络问题,数据一直没上报的情况改怎么计算?
|
2025-04-16 09:54:21 +08:00
|
|
|
|
//await _producerBus.PublishAsync(ProtocolConst.SubscriberReceivedEventName, new MessageReceived
|
|
|
|
|
|
//{
|
|
|
|
|
|
// ClientId = client.Id,
|
|
|
|
|
|
// ClientIp = client.IP,
|
|
|
|
|
|
// ClientPort = client.Port,
|
|
|
|
|
|
// MessageHexString = messageHexString,
|
|
|
|
|
|
// DeviceNo = deviceNo,
|
|
|
|
|
|
// MessageId = NewId.NextGuid().ToString()
|
|
|
|
|
|
//});
|
2025-04-21 23:47:11 +08:00
|
|
|
|
|
2025-04-22 16:48:53 +08:00
|
|
|
|
if(tB3761?.AFN_FC?.AFN==null || tB3761.DT?.Fn==null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError("376.1协议解析AFN失败");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 登录心跳已做了处理,故需要忽略登录和心跳帧
|
|
|
|
|
|
//if(tB3761.DT?.Fn == (int)FN.登录 || tB3761.DT?.Fn == (int)FN.心跳)
|
|
|
|
|
|
// return;
|
|
|
|
|
|
|
2025-04-21 23:47:11 +08:00
|
|
|
|
//TODO:根据AFN进行分流推送到kafka
|
2025-04-22 16:48:53 +08:00
|
|
|
|
string topicName = string.Format(ProtocolConst.AFNTopicNameFormat, tB3761?.AFN_FC?.AFN.ToString().PadLeft(2,'0'));
|
|
|
|
|
|
|
|
|
|
|
|
List<string> topics = ProtocolConstExtensions.GetAllTopicNamesByReceived();
|
|
|
|
|
|
|
|
|
|
|
|
if(topics.Contains(topicName))
|
|
|
|
|
|
await _producerService.ProduceAsync(topicName, new MessageReceived
|
|
|
|
|
|
{
|
|
|
|
|
|
ClientId = tcpSessionClient.Id,
|
|
|
|
|
|
ClientIp = tcpSessionClient.IP,
|
|
|
|
|
|
ClientPort = tcpSessionClient.Port,
|
|
|
|
|
|
MessageHexString = messageHexString,
|
|
|
|
|
|
DeviceNo = tB3761?.A?.Code!,
|
|
|
|
|
|
MessageId = Guid.NewGuid().ToString()
|
|
|
|
|
|
});
|
|
|
|
|
|
else
|
2025-04-22 09:34:59 +08:00
|
|
|
|
{
|
2025-04-22 16:48:53 +08:00
|
|
|
|
_logger.LogError($"不支持的上报kafka主题:{topicName}");
|
|
|
|
|
|
await _producerService.ProduceAsync(ProtocolConst.SubscriberReceivedEventName, new MessageReceived
|
|
|
|
|
|
{
|
|
|
|
|
|
ClientId = tcpSessionClient.Id,
|
|
|
|
|
|
ClientIp = tcpSessionClient.IP,
|
|
|
|
|
|
ClientPort = tcpSessionClient.Port,
|
|
|
|
|
|
MessageHexString = messageHexString,
|
|
|
|
|
|
DeviceNo = tB3761?.A?.Code!,
|
|
|
|
|
|
MessageId = Guid.NewGuid().ToString()
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|