forked from amkovkov/GranuSightSoftware2
125 lines
6.3 KiB
C#
125 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);
|
|
}
|
|
);
|
|
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>();
|
|
}
|