160 lines
5.6 KiB
C#
160 lines
5.6 KiB
C#
using HardwareControl.Core;
|
|
using HardwareControl.Core.Scanning;
|
|
using HardwareWeb.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace HardwareWeb.Controllers
|
|
{
|
|
/// <summary>
|
|
/// 一次完整测试流程的 Web 接口。
|
|
/// 扫描为后台任务,前端通过 /stageHub 或 /api/scan/current 获取进度。
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/scan")]
|
|
public class ScanController : ControllerBase
|
|
{
|
|
private readonly IScanWorkflowService _workflow;
|
|
private readonly ScanDataStore _store;
|
|
private readonly ILogger<ScanController> _logger;
|
|
|
|
public ScanController(
|
|
IScanWorkflowService workflow,
|
|
ScanDataStore store,
|
|
ILogger<ScanController> logger)
|
|
{
|
|
_workflow = workflow;
|
|
_store = store;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>开始一次扫描。需要先在放置样品页完成基准/靶样采集。</summary>
|
|
[HttpPost("start")]
|
|
[ProducesResponseType(typeof(ScanStartResponse), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> 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);
|
|
}
|
|
}
|
|
|
|
/// <summary>读取当前扫描进度。</summary>
|
|
[HttpGet("current")]
|
|
[ProducesResponseType(typeof(ScanProgressPayload), StatusCodes.Status200OK)]
|
|
public IActionResult Current() =>
|
|
Ok(ScanProgressPayload.From(_workflow.Current));
|
|
|
|
/// <summary>中止当前扫描:停止流程、丢弃数据并让位移台回零。</summary>
|
|
[HttpPost("abort")]
|
|
public async Task<IActionResult> Abort()
|
|
{
|
|
await _workflow.AbortAsync();
|
|
return Ok(new { aborted = true });
|
|
}
|
|
|
|
/// <summary>最近一次已完成的扫描结果。</summary>
|
|
[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)));
|
|
}
|
|
|
|
/// <summary>历史扫描记录列表(按开始时间倒序,不含图片与光谱数据)。</summary>
|
|
[HttpGet("history")]
|
|
[ProducesResponseType(typeof(IReadOnlyList<ScanHistoryItemPayload>), StatusCodes.Status200OK)]
|
|
public IActionResult History([FromQuery] int limit = 100)
|
|
{
|
|
var runs = _store.GetRecentRuns(limit);
|
|
return Ok(runs.Select(ScanHistoryItemPayload.From).ToList());
|
|
}
|
|
|
|
/// <summary>按扫描 ID 读取结果。</summary>
|
|
[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)));
|
|
}
|
|
|
|
/// <summary>删除一条历史扫描记录及其全部采样点。</summary>
|
|
[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
|
|
};
|
|
}
|
|
}
|
|
}
|