diff --git a/protocols/JiShe.CollectBus.Protocol.Contracts/AnalysisStrategyContext.cs b/protocols/JiShe.CollectBus.Protocol.Contracts/AnalysisStrategyContext.cs index 283c3ff..8fe8f70 100644 --- a/protocols/JiShe.CollectBus.Protocol.Contracts/AnalysisStrategyContext.cs +++ b/protocols/JiShe.CollectBus.Protocol.Contracts/AnalysisStrategyContext.cs @@ -1,4 +1,5 @@ using JiShe.CollectBus.Protocol.Contracts.Interfaces; +using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; @@ -8,18 +9,27 @@ using System.Threading.Tasks; namespace JiShe.CollectBus.Protocol.Contracts { - public class AnalysisStrategyContext + public class AnalysisStrategyContext { - private readonly IAnalysisStrategy _analysisStrategy; + private readonly IServiceProvider _provider; - public AnalysisStrategyContext(IAnalysisStrategy analysisStrategy) - { - _analysisStrategy = analysisStrategy; - } + public AnalysisStrategyContext(IServiceProvider provider) => _provider = provider; - public Task ExecuteAnalysisStrategy(TInput input) + /// + /// 执行策略 + /// + /// + /// + /// + /// + /// + public Task ExecuteAsync(string type, TInput input) { - return _analysisStrategy.ExecuteAsync(input); + var factory = _provider.GetRequiredService>(); + var strategy = (IAnalysisStrategy)factory(type, typeof(TInput), typeof(TResult)); + return strategy.ExecuteAsync(input); } } + + } 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 f5ba2af..e1c08d9 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 @@ -14,6 +14,10 @@ namespace JiShe.CollectBus.Protocol.AnalysisData.AFN_00H { public class AFN0_F1_Analysis: IAnalysisStrategy { + public static string StrategyType => nameof(AFN0_F1_Analysis); + public static Type InputType => typeof(TB3761); + public static Type ResultType => typeof(AFN0_F1_AnalysisDto); + private readonly ILogger _logger; public AFN0_F1_Analysis(ILogger logger) diff --git a/protocols/JiShe.CollectBus.Protocol/JiSheCollectBusProtocolModule.cs b/protocols/JiShe.CollectBus.Protocol/JiSheCollectBusProtocolModule.cs index 4ceeb77..989f497 100644 --- a/protocols/JiShe.CollectBus.Protocol/JiSheCollectBusProtocolModule.cs +++ b/protocols/JiShe.CollectBus.Protocol/JiSheCollectBusProtocolModule.cs @@ -1,5 +1,6 @@ using JiShe.CollectBus.Kafka.Internal; using JiShe.CollectBus.Protocol.AnalysisData; +using JiShe.CollectBus.Protocol.Contracts; using JiShe.CollectBus.Protocol.Contracts.Abstracts; using JiShe.CollectBus.Protocol.Contracts.Interfaces; using Microsoft.Extensions.DependencyInjection; @@ -29,6 +30,10 @@ namespace JiShe.CollectBus.Protocol public void RegisterProtocolAnalysis(IServiceCollection services) { + // 扫描并注册所有策略 + var strategyMetadata = new Dictionary<(string, Type, Type), Type>(); + services.AddTransient(); + // 批量注册 var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); if (string.IsNullOrWhiteSpace(assemblyPath)) @@ -44,27 +49,44 @@ namespace JiShe.CollectBus.Protocol .FirstOrDefault(a => a.GetName().FullName == assemblyName.FullName); var assembly = existingAssembly ?? Assembly.LoadFrom(file); // 实现IAnalysisStrategy接口 - //var analysisStrategyTypes = assembly.GetTypes().Where(t =>!t.IsAbstract && !t.IsInterface && t.GetInterfaces().Any(i => - //i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IAnalysisStrategy<,>))).ToList(); - var analysisStrategyTypes = assembly.GetTypes().Where(type => - typeof(IAnalysisStrategy<,>).IsAssignableFrom(type) && - !type.IsAbstract && !type.IsInterface).ToList(); - if (analysisStrategyTypes.Count == 0) + var analysisStrategyTypes = assembly.GetTypes().Where(t => !t.IsAbstract && !t.IsInterface && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IAnalysisStrategy<,>))); + if (analysisStrategyTypes.Count() == 0) continue; foreach (var analysisStrategyType in analysisStrategyTypes) { - // 取所有接口 - //var interfaceTypes = analysisStrategyType.GetInterfaces() - // .Where(i => i.IsGenericType && - // i.GetGenericTypeDefinition() == typeof(IAnalysisStrategy<,>)); - //foreach (var interfaceType in interfaceTypes) - //{ - // services.AddKeyedTransient(analysisStrategyType, nameof(analysisStrategyType)); - //} - services.AddKeyedTransient(analysisStrategyType, nameof(analysisStrategyType)); + // 通过反射获取静态元数据 + var strategyType = analysisStrategyType.Name; + var genericArgs = analysisStrategyType.GetInterface("IAnalysisStrategy`2")!.GetGenericArguments(); + var inputType = genericArgs[0]; + var resultType = genericArgs[1]; + // 注册策略实现 + services.AddTransient(analysisStrategyType); + strategyMetadata[(strategyType, inputType, resultType)] = analysisStrategyType; + + } } - + + // 注册元数据字典 + services.AddSingleton(strategyMetadata); + + // 注册策略解析工厂 + services.AddTransient>(provider => (name, inputType, resultType) => + { + var metadata = provider.GetRequiredService>(); + if (metadata.TryGetValue((name, inputType, resultType), out var strategyType)) + { + return provider.GetRequiredService(strategyType); + } + else + { + var logger= provider.GetRequiredService>(); + logger.LogWarning($"未能找到解析策略:{name}-{inputType}-{resultType}"); + return null; + } + }); + + } } } diff --git a/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs b/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs index 3437f4b..0d97c69 100644 --- a/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs +++ b/services/JiShe.CollectBus.Application/ScheduledMeterReading/BasicScheduledMeterReadingService.cs @@ -248,8 +248,8 @@ namespace JiShe.CollectBus.ScheduledMeterReading timer1.Stop(); _logger.LogError($"读取数据总共花费时间{timer1.ElapsedMilliseconds}毫秒"); - //DeviceGroupBalanceControl.InitializeCache(focusAddressDataLista, _kafkaOptions.NumPartitions); - //return; + DeviceGroupBalanceControl.InitializeCache(focusAddressDataLista, _kafkaOptions.NumPartitions); + return; #else var meterInfos = await GetAmmeterInfoList(gatherCode); #endif diff --git a/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs b/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs index f1df404..cebb2e7 100644 --- a/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs +++ b/services/JiShe.CollectBus.Application/Subscribers/SubscriberAppService.cs @@ -219,7 +219,7 @@ namespace JiShe.CollectBus.Subscribers } - [KafkaSubscribe(ProtocolConst.SubscriberAFN01HReceivedEventNameTemp)] + [KafkaSubscribe(ProtocolConst.SubscriberAFN02HReceivedEventNameTemp)] public async Task ReceivedAFN00Event(MessageReceived receivedMessage) { @@ -241,11 +241,12 @@ namespace JiShe.CollectBus.Subscribers Logger.LogError($"数据解析失败:{receivedMessage.Serialize()}"); return SubscribeAck.Success(); } - //string serverName = $"AFN{tB3761.AFN_FC.AFN}_F{tB3761.DT.Fn}_Analysis"; + string serverName = $"AFN{tB3761.AFN_FC.AFN}_F{tB3761.DT.Fn}_Analysis"; //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); } return SubscribeAck.Success(); diff --git a/web/JiShe.CollectBus.Host/Pages/Monitor.cshtml b/web/JiShe.CollectBus.Host/Pages/Monitor.cshtml index 88209c2..0193df3 100644 --- a/web/JiShe.CollectBus.Host/Pages/Monitor.cshtml +++ b/web/JiShe.CollectBus.Host/Pages/Monitor.cshtml @@ -16,6 +16,7 @@ 后端服务 + diff --git a/web/JiShe.CollectBus.Host/appsettings.json b/web/JiShe.CollectBus.Host/appsettings.json index fca91bb..e146496 100644 --- a/web/JiShe.CollectBus.Host/appsettings.json +++ b/web/JiShe.CollectBus.Host/appsettings.json @@ -84,7 +84,7 @@ "SaslPassword": "lixiao1980", "KafkaReplicationFactor": 3, "NumPartitions": 30, - "ServerTagName": "JiSheCollectBus4", + "ServerTagName": "JiSheCollectBus99", "FirstCollectionTime": "2025-04-22 16:07:00" }, "IoTDBOptions": {