提交代码
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
using HardwareControl.Core;
|
||||
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HardwareWeb.Controllers
|
||||
{
|
||||
/// <summary>对焦系统连接与状态查询。</summary>
|
||||
[ApiController]
|
||||
[Route("api/focus")]
|
||||
public class FocusSystemController : ControllerBase
|
||||
{
|
||||
private readonly IFocusSystemService _focus;
|
||||
private readonly ILogger<FocusSystemController> _logger;
|
||||
|
||||
public FocusSystemController(
|
||||
IFocusSystemService focus,
|
||||
ILogger<FocusSystemController> logger)
|
||||
{
|
||||
_focus = focus;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpPost("connect")]
|
||||
public async Task<IActionResult> Connect(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _focus.ConnectAsync(ct);
|
||||
return Ok(new
|
||||
{
|
||||
connected = _focus.IsConnected,
|
||||
port = _focus.PortName
|
||||
});
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>读取对焦系统信息(位置、错误码、激光在线、电压)。</summary>
|
||||
[HttpGet("status")]
|
||||
public async Task<IActionResult> Status(CancellationToken ct)
|
||||
{
|
||||
if (!_focus.IsConnected)
|
||||
{
|
||||
return Conflict(new
|
||||
{
|
||||
error = "NotConnected",
|
||||
message = "对焦系统尚未连接"
|
||||
});
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var status = await _focus.ReadSystemInfoAsync(1000, ct);
|
||||
return Ok(new
|
||||
{
|
||||
focusPosition = status.FocusPosition,
|
||||
deviceError = status.Error,
|
||||
errorDescription = status.ErrorDescription,
|
||||
location = status.Location,
|
||||
laserOnline = status.LaserOnline,
|
||||
voltage = status.VoltageVolts,
|
||||
isDebugMode = status.IsDebugMode,
|
||||
updatedAt = DateTimeOffset.Now
|
||||
});
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private IActionResult HardwareError(HardwareException ex)
|
||||
{
|
||||
var statusCode = ex.Kind switch
|
||||
{
|
||||
HardwareErrorKind.InvalidArgument => StatusCodes.Status400BadRequest,
|
||||
HardwareErrorKind.NotConnected => StatusCodes.Status409Conflict,
|
||||
HardwareErrorKind.Timeout => StatusCodes.Status504GatewayTimeout,
|
||||
HardwareErrorKind.ConnectFailed => StatusCodes.Status503ServiceUnavailable,
|
||||
_ => 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