提交代码
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
using HardwareControl.Core;
|
||||
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HardwareWeb.Controllers
|
||||
{
|
||||
/// <summary>位移台连接与状态查询。</summary>
|
||||
[ApiController]
|
||||
[Route("api/stage")]
|
||||
public class AxisStageController : ControllerBase
|
||||
{
|
||||
private readonly IAxisStageService _stage;
|
||||
private readonly ILogger<AxisStageController> _logger;
|
||||
|
||||
public AxisStageController(
|
||||
IAxisStageService stage,
|
||||
ILogger<AxisStageController> logger)
|
||||
{
|
||||
_stage = stage;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpPost("connect")]
|
||||
public async Task<IActionResult> Connect(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _stage.ConnectAsync(ct);
|
||||
return Ok(new
|
||||
{
|
||||
connected = _stage.IsConnected,
|
||||
port = _stage.PortName
|
||||
});
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>读取位移台当前状态(一次上传帧)。</summary>
|
||||
[HttpGet("status")]
|
||||
public async Task<IActionResult> Status(CancellationToken ct)
|
||||
{
|
||||
if (!_stage.IsConnected)
|
||||
{
|
||||
return Conflict(new
|
||||
{
|
||||
error = "NotConnected",
|
||||
message = "位移台尚未连接"
|
||||
});
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var status = await _stage.ReadStatusAsync(1000, ct);
|
||||
return Ok(new
|
||||
{
|
||||
x = status.XPosition,
|
||||
y = status.YPosition,
|
||||
z = status.ZPosition,
|
||||
moving = status.AnyMoving,
|
||||
xMoving = status.XMoving,
|
||||
yMoving = status.YMoving,
|
||||
zMoving = status.ZMoving,
|
||||
deviceError = status.DeviceError,
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using HardwareWeb.Models;
|
||||
using HardwareWeb.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HardwareWeb.Controllers
|
||||
{
|
||||
/// <summary>硬件设备连接状态与重试。</summary>
|
||||
[ApiController]
|
||||
[Route("api/devices")]
|
||||
public class DeviceController : ControllerBase
|
||||
{
|
||||
private readonly DeviceConnectionService _devices;
|
||||
|
||||
public DeviceController(DeviceConnectionService devices)
|
||||
{
|
||||
_devices = devices;
|
||||
}
|
||||
|
||||
/// <summary>读取位移台/对焦系统/光谱仪/相机的连接状态。</summary>
|
||||
[HttpGet("status")]
|
||||
[ProducesResponseType(typeof(DeviceStatusPayload), StatusCodes.Status200OK)]
|
||||
public IActionResult Status() => Ok(_devices.GetSnapshot());
|
||||
|
||||
/// <summary>重试连接所有未连接的设备。</summary>
|
||||
[HttpPost("retry")]
|
||||
[ProducesResponseType(typeof(DeviceStatusPayload), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Retry(CancellationToken ct) =>
|
||||
Ok(await _devices.RetryFailedAsync(ct));
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,14 +40,15 @@ namespace HardwareWeb.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>抓取当前最新画面作为靶样画面。</summary>
|
||||
/// <summary>关闭红外测距光源后抓取当前最新画面作为靶样画面。</summary>
|
||||
[HttpPost("target")]
|
||||
[ProducesResponseType(typeof(SampleTargetPayload), StatusCodes.Status200OK)]
|
||||
public IActionResult CaptureTarget()
|
||||
public async Task<IActionResult> CaptureTarget(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Ok(SampleTargetPayload.From(_service.CaptureTarget()));
|
||||
return Ok(SampleTargetPayload.From(
|
||||
await _service.CaptureTargetAsync(ct)));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user