using System.Drawing; using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; using GSS2.Core; using GSS2.Core.Analysis.AmineContent.Database; using GSS2.Core.Hardware; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging.Console; namespace GSS2.Test; public partial class App : Application { private readonly IHost _host = BuildHostApp(); public override void Initialize() { _host.Start(); AvaloniaXamlLoader.Load(this); } private static IHost BuildHostApp() { var builder = Host.CreateApplicationBuilder(); builder.Logging.ClearProviders(); builder.Logging.AddConsole(options => options.FormatterName = nameof(ConsoleFormatter)); builder.Logging.AddConsoleFormatter(); builder.Logging.AddProvider(new FileLoggerProvider("app.log")); builder.Services.AddSingleton(); builder.Services.AddAmineContentCalibrationContext(builder.Configuration); builder.Services.AddAmineContentResultsContext(builder.Configuration); builder.Services.AddLightsService("Hardware:Lights"); builder.Services.AddTemperatureHumidityService("Hardware:TemperatureHumidity"); builder.Services.AddIlluminatorService("Hardware:Illuminator"); builder.Services.AddCameraService("Hardware:Camera", true); var host = builder.Build(); host.Services.GetRequiredService(); using (var scope = host.Services.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService(); context.Database.Migrate(); } using (var scope = host.Services.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService(); context.Database.Migrate(); } return host; } public override void OnFrameworkInitializationCompleted() { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { var lightsService = _host.Services.GetRequiredService(); _ = lightsService.Run(CancellationToken.None); _ = lightsService.SnowfallAsync(0.3, 1, Color.Aqua); var cameraService = _host.Services.GetRequiredService(); var illuminatorService = _host.Services.GetRequiredService(); desktop.MainWindow = new MainWindow(cameraService, illuminatorService, lightsService); desktop.MainWindow.Closed += async (_, _) => { await lightsService.DisconnectAsync(); await _host.StopAsync(); _host.Dispose(); }; } base.OnFrameworkInitializationCompleted(); } }