using HardwareControl.Core; using HardwareControl.Core.HardwareServiceImpl.HardwareService; using HardwareControl.Core.Spectrometer; using HardwareWeb.Models; using Microsoft.AspNetCore.Mvc; namespace HardwareWeb.Controllers { /// /// aris 光谱仪 Web 接口。 /// /// 采集方式为同步单帧(SDK 的 AcquireFrame):POST /acquire 会阻塞到一帧采集完成, /// 通常几十毫秒到数秒(取决于曝光时间),前端应设置足够的请求超时。 /// 所有光谱仪操作在服务层串行执行,并发请求会排队。 /// [ApiController] [Route("api/spectrometer")] public class SpectrometerController : ControllerBase { private readonly IArisSpectrometerService _spectrometer; private readonly ILogger _logger; public SpectrometerController( IArisSpectrometerService spectrometer, ILogger logger) { _spectrometer = spectrometer; _logger = logger; } /// 连接光谱仪并读取设备列表。重复调用是幂等的。 [HttpPost("connect")] [ProducesResponseType(typeof(SpectrometerStatusPayload), StatusCodes.Status200OK)] public async Task Connect(CancellationToken ct) { try { await _spectrometer.ConnectAsync(ct); return Ok(SpectrometerStatusPayload.From(_spectrometer)); } catch (HardwareException ex) { return HardwareError(ex); } } /// 断开光谱仪,释放原生上下文。 [HttpPost("disconnect")] [ProducesResponseType(typeof(SpectrometerStatusPayload), StatusCodes.Status200OK)] public async Task Disconnect(CancellationToken ct) { try { await _spectrometer.DisconnectAsync(ct); return Ok(SpectrometerStatusPayload.From(_spectrometer)); } catch (HardwareException ex) { return HardwareError(ex); } } /// 读取当前连接状态、设备列表和累计采集帧数,不访问硬件。 [HttpGet("status")] [ProducesResponseType(typeof(SpectrometerStatusPayload), StatusCodes.Status200OK)] public IActionResult GetStatus() => Ok(SpectrometerStatusPayload.From(_spectrometer)); /// 读取波长数组,用于单独绘制光谱坐标轴。 [HttpGet("wavelengths")] [ProducesResponseType(typeof(WavelengthsPayload), StatusCodes.Status200OK)] public async Task GetWavelengths( [FromQuery] int? deviceIndex, CancellationToken ct) { try { var wavelengths = await _spectrometer.GetWavelengthsAsync(deviceIndex, ct); var index = deviceIndex ?? _spectrometer.SelectedDevice?.Index ?? 0; return Ok(new WavelengthsPayload(index, wavelengths.Length, wavelengths)); } catch (HardwareException ex) { return HardwareError(ex); } } /// 查询设备运行时状态(一次 USB 往返)。 [HttpGet("devices/{deviceIndex:int}/status")] [ProducesResponseType(typeof(SpectrometerDeviceStatusPayload), StatusCodes.Status200OK)] public async Task GetDeviceStatus( int deviceIndex, CancellationToken ct) { try { var status = await _spectrometer.GetDeviceStatusAsync(deviceIndex, ct); return Ok(SpectrometerDeviceStatusPayload.From(status)); } catch (HardwareException ex) { return HardwareError(ex); } } /// /// 同步采集一帧光谱。请求体可省略,此时使用 appsettings.json 中的默认参数。 /// [HttpPost("acquire")] [ProducesResponseType(typeof(SpectrumFramePayload), StatusCodes.Status200OK)] public async Task Acquire( [FromBody] SpectrumAcquisitionRequest? request, CancellationToken ct) { try { var frame = await _spectrometer.AcquireFrameAsync(request, ct); return Ok(SpectrumFramePayload.From(frame)); } catch (HardwareException ex) { return HardwareError(ex); } } /// /// 多台光谱仪同步采集一帧:一次武装所有已连接设备,收齐后按波长自动拼接。 /// 部分设备超时会返回已收到的数据,IsComplete=false。 /// [HttpPost("acquire-synchronized")] [ProducesResponseType(typeof(SynchronizedSpectrumPayload), StatusCodes.Status200OK)] public async Task AcquireSynchronized( [FromBody] SynchronizedSpectrumRequest? request, CancellationToken ct) { try { var result = await _spectrometer.AcquireSynchronizedAsync(request, ct); return Ok(SynchronizedSpectrumPayload.From(result)); //return Ok(null); } 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.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, "光谱仪接口失败:{Kind},{Message}", ex.Kind, ex.Message); } else { _logger.LogWarning( "光谱仪接口失败:{Kind},{Message}", ex.Kind, ex.Message); } return new ObjectResult(new { error = ex.Kind.ToString(), message = ex.Message }) { StatusCode = statusCode }; } } }