JiShe.CollectBus/services/JiShe.CollectBus.Application/CollectBusApplicationModule.cs

106 lines
3.7 KiB
C#
Raw Normal View History

using JiShe.CollectBus.Common.Consts;
2025-03-21 14:30:11 +08:00
using JiShe.CollectBus.Common.Extensions;
using JiShe.CollectBus.FreeSql;
using JiShe.CollectBus.Kafka;
using JiShe.CollectBus.Kafka.AdminClient;
2025-04-07 17:35:37 +08:00
using JiShe.CollectBus.Protocol.Contracts;
using JiShe.CollectBus.ScheduledMeterReading;
2025-04-07 17:35:37 +08:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
2025-04-15 17:57:47 +08:00
using System.Threading.Tasks;
2025-04-21 09:54:34 +08:00
using Cassandra.Mapping;
2025-04-15 17:57:47 +08:00
using JiShe.CollectBus.Cassandra;
2025-04-17 20:28:50 +08:00
using JiShe.CollectBus.FreeRedis;
using JiShe.CollectBus.IoTDB;
2025-04-21 09:54:34 +08:00
using JiShe.CollectBus.Mappers;
using Volo.Abp;
using Volo.Abp.Application;
2025-04-15 17:57:47 +08:00
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.BackgroundWorkers;
using Volo.Abp.BackgroundWorkers.Hangfire;
2025-04-09 14:33:20 +08:00
using Volo.Abp.EventBus;
using Volo.Abp.Modularity;
2025-04-17 14:12:02 +08:00
using Microsoft.Extensions.Options;
using JiShe.CollectBus.Kafka.Internal;
2025-04-21 09:54:34 +08:00
using JiShe.CollectBus.Interceptors;
using JiShe.CollectBus.Common.Attributes;
2024-12-19 16:07:07 +08:00
namespace JiShe.CollectBus;
[DependsOn(
typeof(CollectBusDomainModule),
typeof(CollectBusApplicationContractsModule),
typeof(AbpDddApplicationModule),
typeof(AbpAutoMapperModule),
2025-04-15 17:57:47 +08:00
typeof(AbpAutofacModule),
2025-03-14 13:45:29 +08:00
typeof(AbpBackgroundWorkersHangfireModule),
2025-03-17 08:35:19 +08:00
typeof(CollectBusFreeRedisModule),
2025-04-09 14:33:20 +08:00
typeof(CollectBusFreeSqlModule),
typeof(CollectBusKafkaModule),
2025-04-21 10:17:40 +08:00
typeof(CollectBusIoTDbModule),
2025-04-15 17:57:47 +08:00
typeof(CollectBusCassandraModule)
2024-12-19 16:07:07 +08:00
)]
public class CollectBusApplicationModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
2025-04-09 14:33:20 +08:00
var configuration = context.Services.GetConfiguration();
2024-12-19 16:07:07 +08:00
context.Services.AddAutoMapperObjectMapper<CollectBusApplicationModule>();
Configure<AbpAutoMapperOptions>(options =>
{
options.AddMaps<CollectBusApplicationModule>(validate: true);
2025-04-21 09:54:34 +08:00
});
context.Services.AddSingleton(new MappingConfiguration()
.Define(new CollectBusMapping()));
// 注册拦截器
context.Services.OnRegistered(ctx =>
{
var methods = ctx.ImplementationType.GetMethods();
foreach (var method in methods)
{
var attr = method.GetCustomAttribute(typeof(LogInterceptAttribute), true);
if (attr != null)
{
ctx.Interceptors.TryAdd<LogInterceptor>();
}
}
});
2024-12-19 16:07:07 +08:00
}
2025-04-15 17:57:47 +08:00
public override async Task OnApplicationInitializationAsync(
2024-12-19 16:07:07 +08:00
ApplicationInitializationContext context)
{
2024-12-19 16:07:07 +08:00
var assembly = Assembly.GetExecutingAssembly();
var types = assembly.GetTypes().Where(t => typeof(ICollectWorker).IsAssignableFrom(t) && !t.IsInterface).ToList();
foreach (var type in types)
{
2025-04-15 17:57:47 +08:00
await context.AddBackgroundWorkerAsync(type);
2024-12-19 16:07:07 +08:00
}
2025-03-21 11:48:31 +08:00
//默认初始化表计信息
2025-04-11 17:43:19 +08:00
var dbContext = context.ServiceProvider.GetRequiredService<EnergySystemScheduledMeterReadingService>();
2025-04-18 11:31:23 +08:00
await dbContext.InitAmmeterCacheData();
2025-04-15 18:01:30 +08:00
//await dbContext.InitWatermeterCacheData();
2025-04-11 17:43:19 +08:00
//初始化主题信息
var kafkaAdminClient = context.ServiceProvider.GetRequiredService<IAdminClientService>();
2025-04-17 14:12:02 +08:00
var kafkaOptions = context.ServiceProvider.GetRequiredService<IOptions<KafkaOptionConfig>>();
2025-04-11 17:43:19 +08:00
2025-04-21 09:54:34 +08:00
var topics = ProtocolConstExtensions.GetAllTopicNamesByIssued();
2025-04-11 17:43:19 +08:00
topics.AddRange(ProtocolConstExtensions.GetAllTopicNamesByReceived());
foreach (var item in topics)
{
2025-04-17 14:12:02 +08:00
await kafkaAdminClient.CreateTopicAsync(item, kafkaOptions.Value.NumPartitions, kafkaOptions.Value.KafkaReplicationFactor);
2025-04-11 17:43:19 +08:00
}
2024-12-19 16:07:07 +08:00
}
}