Files
SpectraElement/HardwareWeb/Program.cs
T
2026-11-25 13:35:06 +08:00

134 lines
5.5 KiB
C#

using System.Text.Json.Serialization;
using HardwareControl.Core.AxisPlatform;
using HardwareControl.Core.FocusSystem;
using HardwareControl.Core.HardwareServiceImpl;
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
using HardwareControl.Core.SampleAnalysis;
using HardwareControl.Core.Scanning;
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<UsbCameraOptions>(
builder.Configuration.GetSection("UsbCamera"));
// 2. 注册相机服务为单例——整个进程只允许一个实例持有 CaptureDevice
builder.Services.AddSingleton<IUsbCameraService, UsbCameraService>();
// 3. 光谱仪配置从 appsettings.json 的 "ArisSpectrometer" 节读取。
// 光谱仪服务同样必须是单例:aris 上下文和 USB 设备不能被多个实例同时持有。
builder.Services.Configure<ArisSpectrometerOptions>(
builder.Configuration.GetSection(ArisSpectrometerOptions.SectionName));
builder.Services.AddSingleton<IArisSpectrometerService, ArisSpectrometerService>();
// 4. 相机推流参数(帧率、观看者 gating、帧大小上限)
builder.Services.Configure<CameraStreamOptions>(
builder.Configuration.GetSection(CameraStreamOptions.SectionName));
builder.Services.AddSingleton<CameraViewerRegistry>();
// 5. SignalR
builder.Services.AddSignalR(options =>
{
options.EnableDetailedErrors = builder.Environment.IsDevelopment();
});
// 6. 后台桥接服务:消费硬件事件,通过 IHubContext 推送给客户端
builder.Services.AddSingleton<CameraBroadcastChannel>();
builder.Services.AddHostedService<CameraBroadcastService>();
// 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<ISystemConfigService, SystemConfigService>();
builder.Services.AddSingleton<AdminAccessService>();
// 8. 靶样轮廓识别与采样点规划(ONNX 模型分割)
builder.Services.Configure<OnnxSegmentationOptions>(
builder.Configuration.GetSection(OnnxSegmentationOptions.SectionName));
builder.Services.AddSingleton<TargetOutlineAnalyzer>();
builder.Services.AddHostedService<SampleAnalysisWarmupService>();
builder.Services.AddSingleton<ISampleAnalysisService, SampleAnalysisService>();
// 9. 位移台:串口优先取系统参数 translation_com,未配置时回退到 appsettings 的 AxisStage:ComPort
builder.Services.Configure<AxisStageOptions>(
builder.Configuration.GetSection(AxisStageOptions.SectionName));
builder.Services.AddSingleton<IAxisStageService, AxisStageService>();
// 10. 对焦系统:串口优先取系统参数 focus_com,未配置时回退到 appsettings 的 FocusSystem:ComPort
builder.Services.Configure<FocusSystemOptions>(
builder.Configuration.GetSection(FocusSystemOptions.SectionName));
builder.Services.AddSingleton<IFocusSystemService, FocusSystemService>();
// 11. 扫描流程:参数读取、坐标换算、LiteDB 存储、NIST 分析与流程编排
builder.Services.AddSingleton<ScanSettingsProvider>();
builder.Services.AddSingleton(serviceProvider => new ScanDataStore(
builder.Configuration["Scanning:DatabasePath"],
serviceProvider.GetRequiredService<ILogger<ScanDataStore>>()));
builder.Services.AddSingleton(serviceProvider => new SpectrumAnalysisService(
builder.Configuration["Scanning:DataPath"],
serviceProvider.GetRequiredService<ILogger<SpectrumAnalysisService>>()));
builder.Services.AddSingleton<IScanWorkflowService, ScanWorkflowService>();
// 12. 设备连接状态:启动时自动连接,失败可在首页重试
builder.Services.AddSingleton<DeviceConnectionService>();
builder.Services.AddHostedService<DeviceStartupService>();
builder.Services.AddHostedService<StageBroadcastService>();
builder.Services.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
// 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.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
// 入口页禁止缓存,避免前端重新构建后浏览器仍加载旧的 JS 资源。
if (string.Equals(
context.File.Name, "index.html", StringComparison.OrdinalIgnoreCase))
{
context.Context.Response.Headers.CacheControl =
"no-cache, no-store, must-revalidate";
}
},
});
app.MapControllers();
// 前端通过 /cameraHub 接收 CameraFrame / CameraStatus 实时消息
app.MapHub<CameraHub>("/cameraHub");
// 扫描实时通道:位移台位置、扫描进度、实时光谱、完成/失败
app.MapHub<StageHub>("/stageHub");
//这一行也是
app.MapFallbackToFile("index.html");
app.Run();