forked from amkovkov/GranuSightSoftware2
123 lines
4.8 KiB
C#
123 lines
4.8 KiB
C#
using System.CommandLine;
|
|
|
|
using GSS2.Core;
|
|
using GSS2.Core.Hardware;
|
|
using GSS2.Core.Logging;
|
|
|
|
using Microsoft.Extensions.Logging.Console;
|
|
|
|
using OpenCvSharp;
|
|
|
|
using ConsoleFormatter = GSS2.Core.Logging.ConsoleFormatter;
|
|
|
|
namespace GSS2.Test;
|
|
|
|
public partial class Program
|
|
{
|
|
private class CaptureWorker : BackgroundService
|
|
{
|
|
private readonly ILogger<CaptureWorker> _logger;
|
|
private readonly IHostApplicationLifetime _applicationLifetime;
|
|
private readonly CameraService _cameraService;
|
|
private readonly IlluminatorService _illuminatorService;
|
|
private readonly FileInfo _outputFile;
|
|
private readonly double _intencityWhite;
|
|
private readonly double _intencityUv365nm;
|
|
private readonly double _intencityUv254nm;
|
|
|
|
public CaptureWorker(ILogger<CaptureWorker> logger, IHostApplicationLifetime applicationLifetime, CameraService cameraService, IlluminatorService illuminatorService, FileInfo outputFile, double intencityWhite, double intencityUv365nm, double intencityUv254nm)
|
|
{
|
|
_applicationLifetime = applicationLifetime;
|
|
_logger = logger;
|
|
_cameraService = cameraService;
|
|
_illuminatorService = illuminatorService;
|
|
_outputFile = outputFile;
|
|
_intencityWhite = intencityWhite;
|
|
_intencityUv365nm = intencityUv365nm;
|
|
_intencityUv254nm = intencityUv254nm;
|
|
|
|
_logger.LogInformation("Initialized");
|
|
_logger.LogInformation("Output File: {}", _outputFile);
|
|
_logger.LogInformation("Intencity White: {}", _intencityWhite);
|
|
_logger.LogInformation("Intencity UV 365 nm: {}", _intencityUv365nm);
|
|
_logger.LogInformation("Intencity UV 254 nm: {}", _intencityUv254nm);
|
|
}
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_logger.LogInformation("Running camera fine tuning");
|
|
_illuminatorService.SetIntensity(_intencityWhite, _intencityUv365nm, _intencityUv254nm);
|
|
_illuminatorService.TurnOn();
|
|
|
|
Mat? image;
|
|
try
|
|
{
|
|
image = await _cameraService.CaptureImage(stoppingToken, 5);
|
|
|
|
if (image is null)
|
|
{
|
|
_logger.LogCritical("Cannot get image");
|
|
_applicationLifetime.StopApplication();
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogCritical(ex, "Exception occured while image capturing");
|
|
_applicationLifetime.StopApplication();
|
|
return;
|
|
}
|
|
finally
|
|
{
|
|
_illuminatorService.TurnOff();
|
|
}
|
|
|
|
try
|
|
{
|
|
image.ImWrite(_outputFile.FullName);
|
|
_logger.LogInformation("Image saved: {}", _outputFile.FullName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogCritical(ex, "Exception occured while image saving");
|
|
}
|
|
|
|
_applicationLifetime.StopApplication();
|
|
}
|
|
|
|
}
|
|
private static void Capture(ParseResult parseResult)
|
|
{
|
|
var outputFile = parseResult.GetRequiredValue(_captureOutputFile);
|
|
var intencityWhite = parseResult.GetRequiredValue(_captureIntencityWhite);
|
|
var intencityUv365nm = parseResult.GetRequiredValue(_captureIntencityUv365nm);
|
|
var intencityUv254nm = parseResult.GetRequiredValue(_captureIntencityUv254nm);
|
|
|
|
var builder = Host.CreateApplicationBuilder();
|
|
|
|
builder.Logging.ClearProviders();
|
|
builder.Logging.AddConsole(options => options.FormatterName = nameof(ConsoleFormatter));
|
|
builder.Logging.AddConsoleFormatter<ConsoleFormatter, ConsoleFormatterOptions>();
|
|
builder.Logging.AddProvider(new FileLoggerProvider("full.log", true));
|
|
builder.Logging.AddProvider(new FileLoggerProvider("last_run.log", false));
|
|
builder.Services.AddSingleton<LibCameraLogSink>();
|
|
|
|
builder.Services.AddIlluminatorService("Hardware:Illuminator");
|
|
builder.Services.AddCameraService("Hardware:Camera", true);
|
|
|
|
builder.Services.AddHostedService<CaptureWorker>(services =>
|
|
{
|
|
var logger = services.GetRequiredService<ILogger<CaptureWorker>>();
|
|
var applicationLifetime = services.GetRequiredService<IHostApplicationLifetime>();
|
|
var cameraService = services.GetRequiredService<CameraService>();
|
|
var illuminatorService = services.GetRequiredService<IlluminatorService>();
|
|
return new CaptureWorker(logger, applicationLifetime, cameraService, illuminatorService, outputFile, intencityWhite, intencityUv365nm, intencityUv254nm);
|
|
});
|
|
|
|
var host = builder.Build();
|
|
host.Services.GetRequiredService<LibCameraLogSink>();
|
|
|
|
host.Run();
|
|
}
|
|
}
|