97 lines
3.4 KiB
C#
97 lines
3.4 KiB
C#
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>
|
||
/// 获取轮廓识别的等待超时时间,
|
||
/// 取自系统参数 camera_Identification_outtime(秒)。
|
||
/// </summary>
|
||
[HttpGet("analyze-timeout")]
|
||
[ProducesResponseType(typeof(AnalyzeTimeoutPayload), StatusCodes.Status200OK)]
|
||
public IActionResult AnalyzeTimeout()
|
||
{
|
||
var timeoutSeconds = _service.GetIdentificationTimeoutSeconds();
|
||
_logger.LogInformation(
|
||
"前端轮廓识别等待超时:{TimeoutSeconds} 秒" +
|
||
"(系统参数 camera_Identification_outtime)",
|
||
timeoutSeconds);
|
||
|
||
return Ok(new AnalyzeTimeoutPayload(timeoutSeconds));
|
||
}
|
||
|
||
/// <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
|
||
};
|
||
}
|
||
}
|
||
}
|