51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
|
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using YD_TrainingCompetition_Api.Models;
|
|||
|
|
using YD_TrainingCompetition_Api.Services;
|
|||
|
|
|
|||
|
|
namespace YD_TrainingCompetition_Api.Controllers;
|
|||
|
|
|
|||
|
|
[ApiController]
|
|||
|
|
[Route("api/[controller]")]
|
|||
|
|
public sealed class DevicesController : ControllerBase
|
|||
|
|
{
|
|||
|
|
private readonly IDeviceStateStore _store;
|
|||
|
|
private readonly IRealtimeNotifier _notifier;
|
|||
|
|
|
|||
|
|
public DevicesController(IDeviceStateStore store, IRealtimeNotifier notifier)
|
|||
|
|
{
|
|||
|
|
_store = store;
|
|||
|
|
_notifier = notifier;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpGet]
|
|||
|
|
public IActionResult GetAll()
|
|||
|
|
{
|
|||
|
|
return Ok(_store.GetAll());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpGet("{deviceId}")]
|
|||
|
|
public IActionResult GetById(string deviceId)
|
|||
|
|
{
|
|||
|
|
var snapshot = _store.GetByDeviceId(deviceId);
|
|||
|
|
return snapshot is null ? NotFound() : Ok(snapshot);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[HttpPost("ingest")]
|
|||
|
|
public async Task<IActionResult> Ingest([FromBody] DeviceTelemetry telemetry, CancellationToken cancellationToken)
|
|||
|
|
{
|
|||
|
|
if (!_store.TryUpsert(telemetry, out var reason))
|
|||
|
|
{
|
|||
|
|
return BadRequest(new { message = reason });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var snapshot = _store.GetByDeviceId(telemetry.DeviceId);
|
|||
|
|
if (snapshot is not null)
|
|||
|
|
{
|
|||
|
|
await _notifier.BroadcastTelemetryAsync(snapshot, cancellationToken);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return Accepted(new { message = "telemetry accepted" });
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|