using HardwareControl.Core; using HardwareControl.Core.Scanning; using HardwareWeb.Models; using Microsoft.AspNetCore.Mvc; namespace HardwareWeb.Controllers { /// /// 一次完整测试流程的 Web 接口。 /// 扫描为后台任务,前端通过 /stageHub 或 /api/scan/current 获取进度。 /// [ApiController] [Route("api/scan")] public class ScanController : ControllerBase { private readonly IScanWorkflowService _workflow; private readonly ScanDataStore _store; private readonly ILogger _logger; public ScanController( IScanWorkflowService workflow, ScanDataStore store, ILogger logger) { _workflow = workflow; _store = store; _logger = logger; } /// 开始一次扫描。需要先在放置样品页完成基准/靶样采集。 [HttpPost("start")] [ProducesResponseType(typeof(ScanStartResponse), StatusCodes.Status200OK)] public async Task Start(CancellationToken ct) { try { var result = await _workflow.StartAsync(ct); _logger.LogInformation( "扫描已启动:{ScanId},采样点 {Count} 个", result.ScanId, result.TotalPoints); return Ok(new ScanStartResponse(result.ScanId, result.TotalPoints)); } catch (HardwareException ex) { return HardwareError(ex); } } /// 读取当前扫描进度。 [HttpGet("current")] [ProducesResponseType(typeof(ScanProgressPayload), StatusCodes.Status200OK)] public IActionResult Current() => Ok(ScanProgressPayload.From(_workflow.Current)); /// 中止当前扫描:停止流程、丢弃数据并让位移台回零。 [HttpPost("abort")] public async Task Abort() { await _workflow.AbortAsync(); return Ok(new { aborted = true }); } /// 最近一次已完成的扫描结果。 [HttpGet("latest")] [ProducesResponseType(typeof(ScanRunPayload), StatusCodes.Status200OK)] public IActionResult Latest() { var run = _store.GetLatestRun(); if (run is null) { return NotFound(new { error = "NotFound", message = "还没有已完成的扫描记录" }); } return Ok(ScanRunPayload.From(run, _store.GetPoints(run.ScanId))); } /// 历史扫描记录列表(按开始时间倒序,不含图片与光谱数据)。 [HttpGet("history")] [ProducesResponseType(typeof(IReadOnlyList), StatusCodes.Status200OK)] public IActionResult History([FromQuery] int limit = 100) { var runs = _store.GetRecentRuns(limit); return Ok(runs.Select(ScanHistoryItemPayload.From).ToList()); } /// 按扫描 ID 读取结果。 [HttpGet("{scanId}")] [ProducesResponseType(typeof(ScanRunPayload), StatusCodes.Status200OK)] public IActionResult Get(string scanId) { var run = _store.GetRun(scanId); if (run is null) { return NotFound(new { error = "NotFound", message = $"扫描记录 {scanId} 不存在" }); } return Ok(ScanRunPayload.From(run, _store.GetPoints(scanId))); } /// 删除一条历史扫描记录及其全部采样点。 [HttpDelete("{scanId}")] public IActionResult Delete(string scanId) { if (!_store.DeleteRun(scanId)) { return NotFound(new { error = "NotFound", message = $"扫描记录 {scanId} 不存在" }); } return Ok(new { deleted = true }); } private IActionResult HardwareError(HardwareException ex) { var statusCode = ex.Kind switch { HardwareErrorKind.InvalidArgument => StatusCodes.Status400BadRequest, HardwareErrorKind.NotConnected => StatusCodes.Status409Conflict, HardwareErrorKind.InvalidState => StatusCodes.Status409Conflict, HardwareErrorKind.Timeout => StatusCodes.Status504GatewayTimeout, HardwareErrorKind.ConnectFailed => StatusCodes.Status503ServiceUnavailable, HardwareErrorKind.CommunicationFailed => StatusCodes.Status503ServiceUnavailable, HardwareErrorKind.NotSupported => StatusCodes.Status501NotImplemented, _ => StatusCodes.Status500InternalServerError, }; if (statusCode >= 500) { _logger.LogError(ex, "扫描接口失败:{Message}", ex.Message); } else { _logger.LogWarning("扫描接口失败:{Message}", ex.Message); } return new ObjectResult(new { error = ex.Kind.ToString(), message = ex.Message }) { StatusCode = statusCode }; } } }