1111
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using HardwareControl.Core;
|
||||
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
|
||||
using HardwareWeb.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HardwareWeb.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// 靶样轮廓识别与采样点规划接口。
|
||||
/// 流程:采集基准画面 -> 采集靶样画面 -> 分析并返回处理后的图像。
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/sample")]
|
||||
public class SampleAnalysisController : ControllerBase
|
||||
{
|
||||
private readonly ISampleAnalysisService _service;
|
||||
private readonly ILogger<SampleAnalysisController> _logger;
|
||||
|
||||
public SampleAnalysisController(
|
||||
ISampleAnalysisService service,
|
||||
ILogger<SampleAnalysisController> logger)
|
||||
{
|
||||
_service = service;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>连续抓取多帧并取中值作为基准背景画面。</summary>
|
||||
[HttpPost("baseline")]
|
||||
[ProducesResponseType(typeof(SampleBaselinePayload), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> CaptureBaseline(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(SampleBaselinePayload.From(
|
||||
await _service.CaptureBaselineAsync(ct)));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>抓取当前最新画面作为靶样画面。</summary>
|
||||
[HttpPost("target")]
|
||||
[ProducesResponseType(typeof(SampleTargetPayload), StatusCodes.Status200OK)]
|
||||
public IActionResult CaptureTarget()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(SampleTargetPayload.From(_service.CaptureTarget()));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>提取靶样轮廓、绘制采样点并返回处理后的 JPEG 图像。</summary>
|
||||
[HttpPost("analyze")]
|
||||
[ProducesResponseType(typeof(SampleAnalysisPayload), StatusCodes.Status200OK)]
|
||||
public IActionResult Analyze()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(SampleAnalysisPayload.From(_service.Analyze()));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "靶样轮廓分析失败");
|
||||
return StatusCode(
|
||||
StatusCodes.Status500InternalServerError,
|
||||
new { error = "AnalyzeFailed", message = "靶样轮廓分析失败,请重试" });
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
_ => 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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user