forked from amkovkov/GranuSightSoftware2
134 lines
7.0 KiB
C#
134 lines
7.0 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>();
|
|
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<AmineContentCalibrationContext>(
|
|
options =>
|
|
{
|
|
var databasePath = configuration.GetConnectionString("AmineContentCalibrationDatabasePath");
|
|
options.UseSqlite($"Data Source={databasePath}");
|
|
},
|
|
ServiceLifetime.Singleton
|
|
);
|
|
public static IServiceCollection AddAmineContentResultsContext(this IServiceCollection services, IConfigurationManager configuration) => services
|
|
.AddDbContext<AmineContentResultsContext>(
|
|
options =>
|
|
{
|
|
var databasePath = configuration.GetConnectionString("AmineContentResultsDatabasePath");
|
|
options.UseSqlite($"Data Source={databasePath}");
|
|
},
|
|
ServiceLifetime.Singleton
|
|
);
|
|
|
|
public static IServiceCollection AddAmineContentImageCapturerService(this IServiceCollection services) => services
|
|
.AddSingleton<AmineContentImageCapturerService>();
|
|
|
|
public static IServiceCollection AddAmineContentAnalyzerService(this IServiceCollection services) => services
|
|
.AddSingleton<AmineContentAnalyzerService>();
|
|
}
|