添加项目文件。
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using HardwareControl.Core;
|
||||
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HardwareWeb.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/camera")]
|
||||
public class CameraController : ControllerBase
|
||||
{
|
||||
private readonly IUsbCameraService _cameraService;
|
||||
|
||||
public CameraController(IUsbCameraService cameraService)
|
||||
{
|
||||
_cameraService = cameraService;
|
||||
}
|
||||
|
||||
[HttpPost("connect")]
|
||||
public async Task<IActionResult> Connect(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _cameraService.ConnectAsync(ct);
|
||||
return Ok(new { connected = _cameraService.IsConnected });
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return StatusCode(500, new
|
||||
{
|
||||
error = ex.Kind.ToString(),
|
||||
message = ex.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("disconnect")]
|
||||
public async Task<IActionResult> Disconnect(CancellationToken ct)
|
||||
{
|
||||
await _cameraService.DisconnectAsync(ct);
|
||||
return Ok(new { connected = _cameraService.IsConnected });
|
||||
}
|
||||
|
||||
[HttpGet("status")]
|
||||
public IActionResult GetStatus()
|
||||
{
|
||||
return Ok(new
|
||||
{
|
||||
connected = _cameraService.IsConnected,
|
||||
deviceName = _cameraService.DeviceName,
|
||||
format = _cameraService.FrameFormat,
|
||||
framesReceived = _cameraService.FramesReceived,
|
||||
lastFrameAt = _cameraService.LastFrameAt
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
using HardwareControl.Core;
|
||||
using HardwareControl.Core.HardwareServiceImpl.HardwareService;
|
||||
using HardwareControl.Core.Spectrometer;
|
||||
using HardwareWeb.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HardwareWeb.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// aris 光谱仪 Web 接口。
|
||||
///
|
||||
/// 采集方式为同步单帧(SDK 的 AcquireFrame):POST /acquire 会阻塞到一帧采集完成,
|
||||
/// 通常几十毫秒到数秒(取决于曝光时间),前端应设置足够的请求超时。
|
||||
/// 所有光谱仪操作在服务层串行执行,并发请求会排队。
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/spectrometer")]
|
||||
public class SpectrometerController : ControllerBase
|
||||
{
|
||||
private readonly IArisSpectrometerService _spectrometer;
|
||||
private readonly ILogger<SpectrometerController> _logger;
|
||||
|
||||
public SpectrometerController(
|
||||
IArisSpectrometerService spectrometer,
|
||||
ILogger<SpectrometerController> logger)
|
||||
{
|
||||
_spectrometer = spectrometer;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>连接光谱仪并读取设备列表。重复调用是幂等的。</summary>
|
||||
[HttpPost("connect")]
|
||||
[ProducesResponseType(typeof(SpectrometerStatusPayload), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Connect(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _spectrometer.ConnectAsync(ct);
|
||||
return Ok(SpectrometerStatusPayload.From(_spectrometer));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>断开光谱仪,释放原生上下文。</summary>
|
||||
[HttpPost("disconnect")]
|
||||
[ProducesResponseType(typeof(SpectrometerStatusPayload), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Disconnect(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _spectrometer.DisconnectAsync(ct);
|
||||
return Ok(SpectrometerStatusPayload.From(_spectrometer));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>读取当前连接状态、设备列表和累计采集帧数,不访问硬件。</summary>
|
||||
[HttpGet("status")]
|
||||
[ProducesResponseType(typeof(SpectrometerStatusPayload), StatusCodes.Status200OK)]
|
||||
public IActionResult GetStatus() =>
|
||||
Ok(SpectrometerStatusPayload.From(_spectrometer));
|
||||
|
||||
/// <summary>读取波长数组,用于单独绘制光谱坐标轴。</summary>
|
||||
[HttpGet("wavelengths")]
|
||||
[ProducesResponseType(typeof(WavelengthsPayload), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>查询设备运行时状态(一次 USB 往返)。</summary>
|
||||
[HttpGet("devices/{deviceIndex:int}/status")]
|
||||
[ProducesResponseType(typeof(SpectrometerDeviceStatusPayload), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetDeviceStatus(
|
||||
int deviceIndex,
|
||||
CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var status = await _spectrometer.GetDeviceStatusAsync(deviceIndex, ct);
|
||||
return Ok(SpectrometerDeviceStatusPayload.From(status));
|
||||
}
|
||||
catch (HardwareException ex)
|
||||
{
|
||||
return HardwareError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 同步采集一帧光谱。请求体可省略,此时使用 appsettings.json 中的默认参数。
|
||||
/// </summary>
|
||||
[HttpPost("acquire")]
|
||||
[ProducesResponseType(typeof(SpectrumFramePayload), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 多台光谱仪同步采集一帧:一次武装所有已连接设备,收齐后按波长自动拼接。
|
||||
/// 部分设备超时会返回已收到的数据,IsComplete=false。
|
||||
/// </summary>
|
||||
[HttpPost("acquire-synchronized")]
|
||||
[ProducesResponseType(typeof(SynchronizedSpectrumPayload), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> 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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HardwareWeb.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class WeatherForecastController : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet(Name = "GetWeatherForecast")]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user