Files
GSS2Rework/GSS2.Core/DependencyInjectionHelper.cs
T
amkovkov 3cc4d21947 feat: massive rework
1) GSS2.Core:
- strip prefixes in GSS2.Core.Analysis.AmineContent namespace class names
- add IEnumerable<double>.Variance extension
- remake analysis + update database so not needed to store all every file, calibration data also stored in database, also currently capturing images stored in system temporary directory and rewriting every time
2) GSS.UI.Core: AsyncImageRecordViewModel make zoom and pans public
3) GSS:
- remove image-storage-clear command
- remove ResultsView and ResultsViewModel, results presented in analysis
- rework calibration views
- add analysis views
- add text and number fields editors view
- add confirmation view on danger buttons
2026-05-13 12:56:32 +03:00

137 lines
7.1 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.Calibration;
using GSS2.Core.Analysis.AmineContent.Database.Results;
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);
}
);
public static IServiceCollection AddComputeResourcesService(this IServiceCollection services) => services
.AddSingleton<ComputeResourcesService>();
[Obsolete]
public static IServiceCollection AddUsbService(this IServiceCollection services) => services
.AddSingleton<UsbService>();
public static IServiceCollection AddImageStorageService(this IServiceCollection services, DirectoryInfo rootDirectory) => services
.AddSingleton<ImageStorageService>(
serviceProvider =>
{
var logger = serviceProvider.GetService<ILogger<ImageStorageService>>();
if (logger is null)
throw new Exception("Cannot get logger");
return new ImageStorageService(logger, rootDirectory);
}
);
public static IServiceCollection AddAmineContentCalibrationContext(this IServiceCollection services, IConfigurationManager configuration) => services
.AddDbContext<CalibrationContext>(
options =>
{
var databasePath = configuration.GetConnectionString("AmineContentCalibrationDatabasePath");
options.UseSqlite($"Data Source={databasePath}");
},
ServiceLifetime.Singleton
);
public static IServiceCollection AddAmineContentResultsContext(this IServiceCollection services, IConfigurationManager configuration) => services
.AddDbContext<ResultsContext>(
options =>
{
var databasePath = configuration.GetConnectionString("AmineContentResultsDatabasePath");
options.UseSqlite($"Data Source={databasePath}");
},
ServiceLifetime.Singleton
);
public static IServiceCollection AddAmineContentImageCapturerService(this IServiceCollection services) => services
.AddSingleton<ImageCapturerService>();
public static IServiceCollection AddAmineContentAnalyzerService(this IServiceCollection services) => services
.AddSingleton<AnalyzerService>();
}