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 _intensityWhite;
|
|
private readonly double _intensityUv365nm;
|
|
private readonly double _intensityUv254nm;
|
|
|
|
public CaptureWorker(ILogger<CaptureWorker> logger, IHostApplicationLifetime applicationLifetime, CameraService cameraService, IlluminatorService illuminatorService, FileInfo outputFile, double intensityWhite, double intensityUv365nm, double intensityUv254nm)
|
|
{
|
|
_applicationLifetime = applicationLifetime;
|
|
_logger = logger;
|
|
_cameraService = cameraService;
|
|
_illuminatorService = illuminatorService;
|
|
_outputFile = outputFile;
|
|
_intensityWhite = intensityWhite;
|
|
_intensityUv365nm = intensityUv365nm;
|
|
_intensityUv254nm = intensityUv254nm;
|
|
|
|
_logger.LogInformation("Initialized");
|
|
_logger.LogInformation("Output File: {}", _outputFile);
|
|
_logger.LogInformation("Intensity White: {}", _intensityWhite);
|
|
_logger.LogInformation("Intensity UV 365 nm: {}", _intensityUv365nm);
|
|
_logger.LogInformation("Intensity UV 254 nm: {}", _intensityUv254nm);
|
|
}
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_logger.LogInformation("Running camera fine tuning");
|
|
_illuminatorService.SetIntensity(_intensityWhite, _intensityUv365nm, _intensityUv254nm);
|
|
_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 occurred 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 occurred while image saving");
|
|
}
|
|
|
|
_applicationLifetime.StopApplication();
|
|
}
|
|
|
|
}
|
|
private static void Capture(ParseResult parseResult)
|
|
{
|
|
var outputFile = parseResult.GetRequiredValue(_captureOutputFile);
|
|
var intensityWhite = parseResult.GetRequiredValue(_captureIntensityWhite);
|
|
var intensityUv365nm = parseResult.GetRequiredValue(_captureIntensityUv365nm);
|
|
var intensityUv254nm = parseResult.GetRequiredValue(_captureIntensityUv254nm);
|
|
|
|
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, intensityWhite, intensityUv365nm, intensityUv254nm);
|
|
});
|
|
|
|
var host = builder.Build();
|
|
host.Services.GetRequiredService<LibCameraLogSink>();
|
|
|
|
host.Run();
|
|
}
|
|
}
|