diff --git a/protocols/JiShe.CollectBus.Protocol.Contracts/Abstracts/ProtocolPlugin.cs b/protocols/JiShe.CollectBus.Protocol.Contracts/Abstracts/ProtocolPlugin.cs index eaa081b..a323787 100644 --- a/protocols/JiShe.CollectBus.Protocol.Contracts/Abstracts/ProtocolPlugin.cs +++ b/protocols/JiShe.CollectBus.Protocol.Contracts/Abstracts/ProtocolPlugin.cs @@ -1,4 +1,6 @@ -using JiShe.CollectBus.Common.Extensions; +using System; +using System.Reflection; +using JiShe.CollectBus.Common.Extensions; using JiShe.CollectBus.IotSystems.Protocols; using JiShe.CollectBus.Protocol.Contracts.Interfaces; using JiShe.CollectBus.Protocol.Contracts.Models; diff --git a/protocols/JiShe.CollectBus.Protocol.Test/JiSheCollectBusTestProtocolModule.cs b/protocols/JiShe.CollectBus.Protocol.Test/JiSheCollectBusTestProtocolModule.cs index cdd5253..314c3ab 100644 --- a/protocols/JiShe.CollectBus.Protocol.Test/JiSheCollectBusTestProtocolModule.cs +++ b/protocols/JiShe.CollectBus.Protocol.Test/JiSheCollectBusTestProtocolModule.cs @@ -9,11 +9,13 @@ namespace JiShe.CollectBus.Protocol.Test { public override void ConfigureServices(ServiceConfigurationContext context) { + Console.WriteLine("TestProtocolPlugin ConfigureServices"); context.Services.AddKeyedSingleton(nameof(TestProtocolPlugin)); } public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { + Console.WriteLine("TestProtocolPlugin Initialization"); var protocol = context.ServiceProvider.GetRequiredKeyedService(nameof(TestProtocolPlugin)); await protocol.LoadAsync(); } diff --git a/protocols/JiShe.CollectBus.Protocol/AnalysisData/AFN_00H/AFN0_F1_Analysis.cs b/protocols/JiShe.CollectBus.Protocol/AnalysisData/AFN_00H/AFN0_F1_Analysis.cs index 3ff0882..2223868 100644 --- a/protocols/JiShe.CollectBus.Protocol/AnalysisData/AFN_00H/AFN0_F1_Analysis.cs +++ b/protocols/JiShe.CollectBus.Protocol/AnalysisData/AFN_00H/AFN0_F1_Analysis.cs @@ -9,13 +9,14 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; namespace JiShe.CollectBus.Protocol.AnalysisData.AFN_00H { /// /// 全部确认:对收到报文中的全部数据单元标识进行确认 /// - public class AFN0_F1_Analysis: IAnalysisStrategy + public class AFN0_F1_Analysis : IAnalysisStrategy { private readonly ILogger _logger; @@ -45,4 +46,37 @@ namespace JiShe.CollectBus.Protocol.AnalysisData.AFN_00H } } } + + public class AFN0_F2_Analysis : IAnalysisStrategy + { + private readonly ILogger _logger; + + public AFN0_F2_Analysis(ILogger logger) + { + _logger = logger; + } + + public Task ExecuteAsync(TB3761 tB3761) + { + try + { + ArgumentNullException.ThrowIfNull(nameof(tB3761)); + AFN0_F1_AnalysisDto dto = new AFN0_F1_AnalysisDto + { + Code = tB3761.A?.Code, + AFN = tB3761.AFN_FC?.AFN ?? 0, + Fn = tB3761.DT?.Fn ?? 0, + Pn = tB3761.DA?.Pn ?? 0 + }; + return Task.FromResult(dto); + } + catch (Exception ex) + { + _logger.LogError(ex, $"00_1解析失败:{tB3761.A?.Code}-{tB3761.DT?.Fn ?? 0}-{tB3761?.BaseHexMessage?.HexMessageString},{ex.Message}"); + return null; + } + } + } + + } diff --git a/protocols/JiShe.CollectBus.Protocol/JiSheCollectBusProtocolModule.cs b/protocols/JiShe.CollectBus.Protocol/JiSheCollectBusProtocolModule.cs index 842ad39..8009165 100644 --- a/protocols/JiShe.CollectBus.Protocol/JiSheCollectBusProtocolModule.cs +++ b/protocols/JiShe.CollectBus.Protocol/JiSheCollectBusProtocolModule.cs @@ -3,10 +3,13 @@ using JiShe.CollectBus.Protocol.AnalysisData; 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.Dto; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; using Serilog.Core; +using System; using System.Reflection; using TouchSocket.Core; using Volo.Abp; @@ -18,16 +21,30 @@ namespace JiShe.CollectBus.Protocol { public override void ConfigureServices(ServiceConfigurationContext context) { + Console.WriteLine("StandardProtocolPlugin ConfigureServices"); context.Services.AddKeyedSingleton(nameof(StandardProtocolPlugin)); - RegisterProtocolAnalysis(context.Services); + //RegisterProtocolAnalysis(context.Services); + LoadAnalysisStrategy(context.Services); } public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { + Console.WriteLine("StandardProtocolPlugin Initialization"); var standardProtocol = context.ServiceProvider.GetRequiredKeyedService(nameof(StandardProtocolPlugin)); await standardProtocol.LoadAsync(); } + public void LoadAnalysisStrategy(IServiceCollection services) + { + var assembly = Assembly.GetExecutingAssembly(); + var analysisStrategyTypes = assembly.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IAnalysisStrategy<,>))); + foreach (var analysisStrategyType in analysisStrategyTypes) + { + var service = analysisStrategyType.GetInterfaces().First(); + services.AddKeyedSingleton(service, analysisStrategyType.Name,analysisStrategyType); + } + } + public void RegisterProtocolAnalysis(IServiceCollection services) { // 扫描并注册所有策略 diff --git a/services/JiShe.CollectBus.Application/Samples/TestAppService.cs b/services/JiShe.CollectBus.Application/Samples/TestAppService.cs index 8241adb..b054779 100644 --- a/services/JiShe.CollectBus.Application/Samples/TestAppService.cs +++ b/services/JiShe.CollectBus.Application/Samples/TestAppService.cs @@ -26,6 +26,8 @@ using System.Linq; using Cassandra; using JiShe.CollectBus.Interceptors; using JiShe.CollectBus.IotSystems.Protocols; +using TouchSocket.Core; +using Volo.Abp.Modularity; namespace JiShe.CollectBus.Samples; @@ -36,16 +38,19 @@ public class TestAppService : CollectBusAppService private readonly ICassandraRepository _messageReceivedCassandraRepository; private readonly ICassandraProvider _cassandraProvider; private readonly IProtocolService _protocolService; + private readonly IServiceProvider _serviceProvider; + public TestAppService( ILogger logger, ICassandraRepository messageReceivedCassandraRepository, - ICassandraProvider cassandraProvider, IProtocolService protocolService) + ICassandraProvider cassandraProvider, IProtocolService protocolService,IServiceProvider serviceProvider) { _logger = logger; _messageReceivedCassandraRepository = messageReceivedCassandraRepository; _cassandraProvider = cassandraProvider; _protocolService = protocolService; + _serviceProvider = serviceProvider; } public async Task AddMessageOfCassandra() { @@ -137,4 +142,10 @@ public class TestAppService : CollectBusAppService var protocol = await _protocolService.FirstOrDefaultByDeviceAsync(deviceCode, isSpecial); return protocol; } + + // 重新加载插件方法 + public async Task ReloadPluginsAsync() + { + //_serviceProvider.GetService<>() + } } diff --git a/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs b/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs index 07453d6..f498a31 100644 --- a/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs +++ b/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs @@ -25,6 +25,7 @@ using JiShe.CollectBus.IoTDB.Provider; using JiShe.CollectBus.Protocol.Dto; using System.Collections; using JiShe.CollectBus.Interfaces; +using System.CodeDom.Compiler; namespace JiShe.CollectBus.Subscribers { @@ -206,8 +207,8 @@ namespace JiShe.CollectBus.Subscribers //var analysisStrategy = _serviceProvider.GetKeyedService($"AFN0_F1_Analysis"); //var data = await analysisStrategy.ExecuteAsync>(tB3761); - var executor = _serviceProvider.GetRequiredService(); - AFN0_F1_AnalysisDto aFN0_F1_AnalysisDto= await executor.ExecuteAsync("AFN0_F1_Analysis", tB3761); + var result = _serviceProvider.GetRequiredKeyedService>("AFN0_F1_Analysis"); + //AFN0_F1_AnalysisDto aFN0_F1_AnalysisDto = await executor.ExecuteAsync("AFN0_F1_Analysis", tB3761); } return SubscribeAck.Success(); diff --git a/web/JiShe.CollectBus.Host/JiShe.CollectBus.Protocol.Test.dll b/web/JiShe.CollectBus.Host/JiShe.CollectBus.Protocol.Test.dll new file mode 100644 index 0000000..01d6842 Binary files /dev/null and b/web/JiShe.CollectBus.Host/JiShe.CollectBus.Protocol.Test.dll differ diff --git a/web/JiShe.CollectBus.Host/JiShe.CollectBus.Protocol.dll b/web/JiShe.CollectBus.Host/JiShe.CollectBus.Protocol.dll new file mode 100644 index 0000000..96cf07b Binary files /dev/null and b/web/JiShe.CollectBus.Host/JiShe.CollectBus.Protocol.dll differ diff --git a/web/JiShe.CollectBus.Host/appsettings.json b/web/JiShe.CollectBus.Host/appsettings.json index c98e05d..e5fe35d 100644 --- a/web/JiShe.CollectBus.Host/appsettings.json +++ b/web/JiShe.CollectBus.Host/appsettings.json @@ -13,7 +13,8 @@ "DotNetCore.CAP": "Warning", "Serilog.AspNetCore": "Information", "Microsoft.EntityFrameworkCore": "Warning", - "Microsoft.AspNetCore": "Warning" + "Microsoft.AspNetCore": "Warning", + "Microsoft.AspNetCore.Diagnostics.HealthChecks": "Warning" } }, "WriteTo": [ @@ -79,7 +80,7 @@ "SaslPassword": "lixiao1980", "KafkaReplicationFactor": 3, "NumPartitions": 30, - "ServerTagName": "JiSheCollectBus30", + "ServerTagName": "JiSheCollectBus100", "FirstCollectionTime": "2025-04-22 16:07:00" }, "IoTDBOptions": {