using System.Windows;
using HC_APTBS.Infrastructure.Logging;
using HC_APTBS.Infrastructure.Pcan;
using HC_APTBS.Services;
using HC_APTBS.Services.Impl;
using HC_APTBS.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using Peak.Can.Basic;
namespace HC_APTBS;
///
/// Application entry-point and DI container host.
///
///
/// Registers all services, ViewModels, and the main window with
/// , then resolves and shows
/// on startup. The container is disposed when the application exits.
///
///
public partial class App : Application
{
private ServiceProvider? _serviceProvider;
protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var services = new ServiceCollection();
ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider();
// Initialise the ViewModel (loads pump IDs, starts refresh timer, connects CAN).
var mainVm = _serviceProvider.GetRequiredService();
await mainVm.InitialiseAsync();
var window = _serviceProvider.GetRequiredService();
window.Show();
}
protected override void OnExit(ExitEventArgs e)
{
_serviceProvider?.Dispose();
base.OnExit(e);
}
// ── Service registration ──────────────────────────────────────────────────
private static void ConfigureServices(IServiceCollection services)
{
// ── Infrastructure ────────────────────────────────────────────────────
services.AddSingleton();
// PcanAdapter requires a channel handle and baudrate read from configuration.
services.AddSingleton(sp =>
{
var cfg = sp.GetRequiredService();
var logger = sp.GetRequiredService();
var channel = cfg.Bench.Channel;
// Default to 500 kbps; the channel can switch at runtime via ICanService.SwitchBaudrate.
return new PcanAdapter(channel, TPCANBaudrate.PCAN_BAUD_500K, logger);
});
services.AddSingleton();
// ── Application services ──────────────────────────────────────────────
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
// ── ViewModels ────────────────────────────────────────────────────────
services.AddSingleton();
// ── Views ─────────────────────────────────────────────────────────────
services.AddSingleton();
}
}