using HardwareControl.Core.HardwareServiceImpl; using HardwareControl.Core.HardwareServiceImpl.HardwareService; using HardwareControl.Core.SampleAnalysis; using HardwareControl.Core.Spectrometer; using HardwareControl.Core.SystemSetting; using HardwareControl.Core.UsbCamera; using HardwareWeb.Hubs; using HardwareWeb.Services; var builder = WebApplication.CreateBuilder(args); // Add services to the container. // 1. 相机配置从 appsettings.json 的 "UsbCamera" 节读取 builder.Services.Configure( builder.Configuration.GetSection("UsbCamera")); // 2. 注册相机服务为单例——整个进程只允许一个实例持有 CaptureDevice builder.Services.AddSingleton(); // 3. 光谱仪配置从 appsettings.json 的 "ArisSpectrometer" 节读取。 // 光谱仪服务同样必须是单例:aris 上下文和 USB 设备不能被多个实例同时持有。 builder.Services.Configure( builder.Configuration.GetSection(ArisSpectrometerOptions.SectionName)); builder.Services.AddSingleton(); // 4. 相机推流参数(帧率、观看者 gating、帧大小上限) builder.Services.Configure( builder.Configuration.GetSection(CameraStreamOptions.SectionName)); builder.Services.AddSingleton(); // 5. SignalR builder.Services.AddSignalR(options => { options.EnableDetailedErrors = builder.Environment.IsDevelopment(); }); // 6. 后台桥接服务:消费硬件事件,通过 IHubContext 推送给客户端 builder.Services.AddSingleton(); builder.Services.AddHostedService(); // 7. 系统参数:LiteDB 本地库 + 管理密码校验 var systemConfigDbPath = builder.Configuration["SystemSetting:DatabasePath"]; if (string.IsNullOrWhiteSpace(systemConfigDbPath)) { systemConfigDbPath = Path.Combine( AppContext.BaseDirectory, "Data", "system-config.db"); } builder.Services.AddSingleton(_ => new SystemConfigRepository(systemConfigDbPath)); builder.Services.AddSingleton(); builder.Services.AddSingleton(); // 8. 靶样轮廓识别与采样点规划(ONNX 模型分割) builder.Services.Configure( builder.Configuration.GetSection(OnnxSegmentationOptions.SectionName)); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseAuthorization(); app.MapControllers(); // 前端通过 /cameraHub 接收 CameraFrame / CameraStatus 实时消息 app.MapHub("/cameraHub"); app.Run();