Files
GSS2Rework/GSS2.Core/DependencyInjectionHelper.cs
T
amkovkov 72ae26eee7 comprehensive update
1) remove unused components: camera view (due it causes segmentation faults), compute resources and all related, illuminator controller (due in useless without camera view), image storage service, temperature and humidity service
2) database schema - reduce tables references, features storing in records themselves in compressed form, add records creation and editing date and time, add separator comment column
3) analysis - rework of pipeline and ui, now database storing only raw data and all display values calculated from it
4) lights service - add reconnection if disconnected
5) add width and height command line arguments
6) fix some typos and other issues
2026-06-29 15:13:29 +03:00

126 lines
6.3 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Protocols.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using GSS2.Core.Logging;
using GSS2.Core.Hardware;
using GSS2.Core.Analysis.AmineContent.Database;
using GSS2.Core.Analysis.AmineContent;
using ConsoleFormatter = GSS2.Core.Logging.ConsoleFormatter;
namespace GSS2.Core;
public static class DependencyInjectionHelper
{
public static IConfigurationBuilder AddAppSettingsJson(this IConfigurationBuilder configuration) =>
configuration.AddJsonFile(Path.Join(Path.GetDirectoryName(Environment.ProcessPath), "appsettings.json"));
public static ILoggingBuilder ConfigureLogging(this ILoggingBuilder logging)
{
logging.ClearProviders();
logging.AddConsole(options => options.FormatterName = nameof(ConsoleFormatter));
logging.AddConsoleFormatter<ConsoleFormatter, ConsoleFormatterOptions>();
logging.AddProvider(new FileLoggerProvider("full.log", true));
logging.AddProvider(new FileLoggerProvider("last_run.log", false));
return logging;
}
// public static IServiceCollection AddTemperatureHumidityService(this IServiceCollection services, string section) => services
// .AddSingleton(
// serviceProvider =>
// {
// var configuration = serviceProvider.GetService<IConfiguration>();
// if (configuration is null)
// throw new Exception("Cannot get configuration");
// var serviceConfiguration = configuration.GetSection(section).Get<TemperatureHumidityService.TemperatureHumidityServiceConfiguration>();
// if (serviceConfiguration is null)
// throw new InvalidConfigurationException($"Cannot get configuration {section}");
// var logger = serviceProvider.GetService<ILogger<TemperatureHumidityService>>();
// if (logger is null)
// throw new Exception("Cannot get logger");
// return new TemperatureHumidityService(logger, serviceConfiguration);
// }
// );
public static IServiceCollection AddLightsService(this IServiceCollection services, string section) => services
.AddSingleton(
serviceProvider =>
{
var configuration = serviceProvider.GetService<IConfiguration>();
if (configuration is null)
throw new Exception("Cannot get configuration");
var serviceConfiguration = configuration.GetSection(section).Get<LightsService.LightsServiceConfiguration>();
if (serviceConfiguration is null)
throw new InvalidConfigurationException($"Cannot get configuration {section}");
var logger = serviceProvider.GetService<ILogger<LightsService>>();
if (logger is null)
throw new Exception("Cannot get logger");
return new LightsService(logger, serviceConfiguration);
}
);
public static IServiceCollection AddIlluminatorService(this IServiceCollection services, string section) => services
.AddSingleton(
serviceProvider =>
{
var configuration = serviceProvider.GetService<IConfiguration>();
if (configuration is null)
throw new Exception("Cannot get configuration");
var serviceConfiguration = configuration.GetSection(section).Get<IlluminatorService.IlluminatorServiceConfiguration>();
if (serviceConfiguration is null)
throw new InvalidConfigurationException($"Cannot get configuration {section}");
var logger = serviceProvider.GetService<ILogger<IlluminatorService>>();
if (logger is null)
throw new Exception("Cannot get logger");
return new IlluminatorService(logger, serviceConfiguration);
}
);
public static IServiceCollection AddCameraService(this IServiceCollection services, string section, bool autoInitialize = false) => services
.AddSingleton(
serviceProvider =>
{
var configuration = serviceProvider.GetService<IConfiguration>();
if (configuration is null)
throw new Exception("Cannot get configuration");
var serviceConfiguration = configuration.GetSection(section).Get<CameraService.CameraServiceConfiguration>();
if (serviceConfiguration is null)
throw new InvalidConfigurationException($"Cannot get configuration {section}");
var logger = serviceProvider.GetService<ILogger<CameraService>>();
if (logger is null)
throw new Exception("Cannot get logger");
return new CameraService(logger, serviceConfiguration, autoInitialize);
}
);
[Obsolete]
public static IServiceCollection AddUsbService(this IServiceCollection services) => services
.AddSingleton<UsbService>();
public static IServiceCollection AddAmineContentContext(this IServiceCollection services, IConfigurationManager configuration) => services
.AddDbContext<Context>(
options =>
{
var databasePath = configuration.GetConnectionString("AmineContentDatabasePath");
options.UseSqlite($"Data Source={databasePath}");
},
ServiceLifetime.Singleton
);
public static IServiceCollection AddAmineContentImageCapturerService(this IServiceCollection services, bool mock = false)
{
if (!mock)
return services.AddSingleton<IImageCapturerService, ImageCapturerService>();
return services.AddSingleton<IImageCapturerService, ImageCapturerMockService>(serviceProvider =>
{
var logger = serviceProvider.GetService<ILogger<ImageCapturerMockService>>();
if (logger is null)
throw new Exception("Cannot get logger");
return new ImageCapturerMockService("./", logger);
});
}
public static IServiceCollection AddAmineContentAnalyzerService(this IServiceCollection services) => services
.AddSingleton<AnalyzerService>();
}