From 8da5fee0a2e2115464466da8d9246c2c5d1fc4d4 Mon Sep 17 00:00:00 2001
From: zenghongyao <873884283@qq.com>
Date: Wed, 23 Apr 2025 15:10:20 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Interfaces/IProtocolService.cs | 8 +++++
.../Services/ProtocolService.cs | 25 +++++++++++++-
.../Plugins/TcpMonitor.cs | 34 ++++++++-----------
.../Subscribers/SubscriberAppService.cs | 5 ++-
4 files changed, 50 insertions(+), 22 deletions(-)
diff --git a/protocols/JiShe.CollectBus.Protocol.Contracts/Interfaces/IProtocolService.cs b/protocols/JiShe.CollectBus.Protocol.Contracts/Interfaces/IProtocolService.cs
index c8ea899..757457a 100644
--- a/protocols/JiShe.CollectBus.Protocol.Contracts/Interfaces/IProtocolService.cs
+++ b/protocols/JiShe.CollectBus.Protocol.Contracts/Interfaces/IProtocolService.cs
@@ -18,5 +18,13 @@ namespace JiShe.CollectBus.Protocol.Contracts.Interfaces
///
///
Task FirstOrDefaultByDeviceAsync(string deviceCode, bool isSpecial = false);
+
+ ///
+ /// 获取协议池服务
+ ///
+ ///
+ ///
+ ///
+ Task GetProtocolServiceAsync(string deviceCode, bool isSpecial = false);
}
}
diff --git a/protocols/JiShe.CollectBus.Protocol.Contracts/Services/ProtocolService.cs b/protocols/JiShe.CollectBus.Protocol.Contracts/Services/ProtocolService.cs
index 3707e6b..0c50f6e 100644
--- a/protocols/JiShe.CollectBus.Protocol.Contracts/Services/ProtocolService.cs
+++ b/protocols/JiShe.CollectBus.Protocol.Contracts/Services/ProtocolService.cs
@@ -18,10 +18,12 @@ namespace JiShe.CollectBus.Protocol.Contracts.Services
public class ProtocolService : IProtocolService, ISingletonDependency
{
private readonly IFreeRedisProvider _freeRedisProvider;
+ private readonly IServiceProvider _serviceProvider;
- public ProtocolService(IFreeRedisProvider freeRedisProvider)
+ public ProtocolService(IFreeRedisProvider freeRedisProvider, IServiceProvider serviceProvider)
{
_freeRedisProvider = freeRedisProvider;
+ _serviceProvider = serviceProvider;
}
///
@@ -42,6 +44,27 @@ namespace JiShe.CollectBus.Protocol.Contracts.Services
return protocolInfo;
}
+ ///
+ /// 获取协议池服务
+ ///
+ ///
+ ///
+ ///
+ public async Task GetProtocolServiceAsync(string deviceCode, bool isSpecial = false)
+ {
+ try
+ {
+ ProtocolInfo protocolInfo= await FirstOrDefaultByDeviceAsync(deviceCode, isSpecial);
+ if(protocolInfo==null)
+ return null;
+ return _serviceProvider.GetKeyedService(protocolInfo.Name);
+ }
+ catch (Exception ex)
+ {
+ return null;
+ }
+ }
+
private static bool ContainsExactPartRegex(string searchPattern, string fullString)
{
// 构建正则表达式 - 匹配以逗号或开头为边界,以逗号或结尾为边界的部分
diff --git a/services/JiShe.CollectBus.Application/Plugins/TcpMonitor.cs b/services/JiShe.CollectBus.Application/Plugins/TcpMonitor.cs
index 5d2ba4b..440239b 100644
--- a/services/JiShe.CollectBus.Application/Plugins/TcpMonitor.cs
+++ b/services/JiShe.CollectBus.Application/Plugins/TcpMonitor.cs
@@ -18,6 +18,7 @@ using JiShe.CollectBus.Protocol.Contracts;
using JiShe.CollectBus.Protocol.Contracts.Abstracts;
using JiShe.CollectBus.Protocol.Contracts.Interfaces;
using JiShe.CollectBus.Protocol.Contracts.Models;
+using JiShe.CollectBus.Protocol.Contracts.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TouchSocket.Core;
@@ -38,25 +39,27 @@ namespace JiShe.CollectBus.Plugins
private readonly IRepository _deviceRepository;
private readonly IDistributedCache _ammeterInfoCache;
private readonly IServiceProvider _serviceProvider;
+ private readonly IProtocolService _protocolService;
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
public TcpMonitor(IProducerService producerService,
ILogger logger,
IRepository deviceRepository,
- IDistributedCache ammeterInfoCache, IServiceProvider serviceProvider)
+ IDistributedCache ammeterInfoCache, IServiceProvider serviceProvider, IProtocolService protocolService)
{
_producerService = producerService;
_logger = logger;
_deviceRepository = deviceRepository;
_ammeterInfoCache = ammeterInfoCache;
_serviceProvider= serviceProvider;
+ _protocolService = protocolService;
}
@@ -64,14 +67,12 @@ namespace JiShe.CollectBus.Plugins
public async Task OnTcpReceived(ITcpSession client, ReceivedDataEventArgs e)
{
var messageHexString = Convert.ToHexString(e.ByteBlock.Span);
-
- var tcpSessionClient = (ITcpSessionClient)client;
- var protocolPlugin = _serviceProvider.GetKeyedService("StandardProtocolPlugin");
+ var protocolPlugin = await _protocolService.GetProtocolServiceAsync("376.1");
if (protocolPlugin == null)
{
_logger.LogError("协议不存在!");
}
-
+ var tcpSessionClient = (ITcpSessionClient)client;
TB3761? tB3761 = await protocolPlugin!.AnalyzeAsync(tcpSessionClient, messageHexString);
if (tB3761 == null)
{
@@ -131,13 +132,6 @@ namespace JiShe.CollectBus.Plugins
///
private async Task OnTcpNormalReceived(ITcpSessionClient tcpSessionClient,string messageHexString, TB3761? tB3761)
{
- //var _protocolPlugin = _serviceProvider.GetKeyedService("StandardProtocolPlugin");
- //if (_protocolPlugin == null)
- //{
- // _logger.LogError("376.1协议插件不存在!");
- //}
-
-
//await _producerBus.Publish(new MessageReceived
//{
// ClientId = client.Id,
diff --git a/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs b/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs
index b11431e..07453d6 100644
--- a/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs
+++ b/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs
@@ -24,6 +24,7 @@ using JiShe.CollectBus.Kafka.Internal;
using JiShe.CollectBus.IoTDB.Provider;
using JiShe.CollectBus.Protocol.Dto;
using System.Collections;
+using JiShe.CollectBus.Interfaces;
namespace JiShe.CollectBus.Subscribers
{
@@ -36,6 +37,7 @@ namespace JiShe.CollectBus.Subscribers
private readonly IRepository _messageReceivedHeartbeatEventRepository;
private readonly IMeterReadingRecordRepository _meterReadingRecordsRepository;
private readonly IIoTDbProvider _dbProvider;
+ private readonly IProtocolService _protocolService;
///
/// Initializes a new instance of the class.
@@ -52,7 +54,7 @@ namespace JiShe.CollectBus.Subscribers
IRepository messageReceivedLoginEventRepository,
IRepository messageReceivedHeartbeatEventRepository,
IIoTDbProvider dbProvider,
- IMeterReadingRecordRepository meterReadingRecordsRepository)
+ IMeterReadingRecordRepository meterReadingRecordsRepository, IProtocolService protocolService)
{
_logger = logger;
_tcpService = tcpService;
@@ -61,6 +63,7 @@ namespace JiShe.CollectBus.Subscribers
_messageReceivedHeartbeatEventRepository = messageReceivedHeartbeatEventRepository;
_meterReadingRecordsRepository = meterReadingRecordsRepository;
_dbProvider = dbProvider;
+ _protocolService = protocolService;
}
[LogIntercept]