Files
SpectraElement/HardwareWeb/Controllers/CameraController.cs
T
2026-11-13 09:51:38 +08:00

57 lines
1.6 KiB
C#

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
});
}
}
}