111 lines
4.8 KiB
C#
111 lines
4.8 KiB
C#
using Heimdall.Application.DTOs;
|
|||
|
|
using Heimdall.Application.Services;
|
||
|
|
using Heimdall.Domain.Enums;
|
||
|
|
using Heimdall.Infrastructure.Security;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.AspNetCore.RateLimiting;
|
||
|
|
|
||
|
|
namespace Heimdall.Api.Controllers;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Camera detection events — motion, person, vehicle, face recognition, etc.
|
||
|
|
/// Agents POST events as they detect them; users GET filtered views.
|
||
|
|
/// </summary>
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/events")]
|
||
|
|
[EnableRateLimiting("Api")]
|
||
|
|
public class EventsController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly ICameraEventService _eventService;
|
||
|
|
|
||
|
|
public EventsController(ICameraEventService eventService) => _eventService = eventService;
|
||
|
|
|
||
|
|
/// <summary>Record a new detection event (called by agents).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "ApiKey", Policy = AppPermissions.CameraWrite)]
|
||
|
|
[HttpPost]
|
||
|
|
[ProducesResponseType(typeof(CameraEventResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status400BadRequest)]
|
||
|
|
public async Task<IActionResult> Create([FromBody] CreateCameraEventRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _eventService.CreateAsync(request, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 400, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Get a single event by ID.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraRead)]
|
||
|
|
[HttpGet("{id:guid}")]
|
||
|
|
[ProducesResponseType(typeof(CameraEventResponse), StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> GetById(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _eventService.GetByIdAsync(id, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok(result.Data)
|
||
|
|
: StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Query events with filters. Supports pagination.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraRead)]
|
||
|
|
[HttpGet]
|
||
|
|
[ProducesResponseType(typeof(PagedResult<CameraEventResponse>), StatusCodes.Status200OK)]
|
||
|
|
public async Task<IActionResult> Query(
|
||
|
|
[FromQuery] Guid? cameraId,
|
||
|
|
[FromQuery] Guid? siteId,
|
||
|
|
[FromQuery] string? type,
|
||
|
|
[FromQuery] DateTime? from,
|
||
|
|
[FromQuery] DateTime? to,
|
||
|
|
[FromQuery] double? minConfidence,
|
||
|
|
[FromQuery] int page = 1,
|
||
|
|
[FromQuery] int pageSize = 50,
|
||
|
|
CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
EventType? parsedType = null;
|
||
|
|
if (type is not null && Enum.TryParse<EventType>(type, true, out var t))
|
||
|
|
parsedType = t;
|
||
|
|
|
||
|
|
var filter = new CameraEventFilterRequest(cameraId, siteId, parsedType, from, to, minConfidence, page, pageSize);
|
||
|
|
var result = await _eventService.QueryAsync(filter, ct);
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Get recent events for a camera.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraRead)]
|
||
|
|
[HttpGet("camera/{cameraId:guid}")]
|
||
|
|
[ProducesResponseType(typeof(List<CameraEventResponse>), StatusCodes.Status200OK)]
|
||
|
|
public async Task<IActionResult> GetRecentByCamera(Guid cameraId, [FromQuery] int count = 20, CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
var result = await _eventService.GetRecentByCameraAsync(cameraId, count, ct);
|
||
|
|
return Ok(result.Data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Attach a snapshot URL to an event (called by agents after upload).</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "ApiKey", Policy = AppPermissions.CameraWrite)]
|
||
|
|
[HttpPost("{id:guid}/snapshot")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> AttachSnapshot(Guid id, [FromBody] AttachSnapshotRequest request, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _eventService.AttachSnapshotAsync(id, request.SnapshotUrl, request.ThumbUrl, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok()
|
||
|
|
: StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Delete an event.</summary>
|
||
|
|
[Authorize(AuthenticationSchemes = "JwtBearer", Policy = AppPermissions.CameraWrite)]
|
||
|
|
[HttpDelete("{id:guid}")]
|
||
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status404NotFound)]
|
||
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var result = await _eventService.DeleteAsync(id, ct);
|
||
|
|
return result.IsSuccess
|
||
|
|
? Ok()
|
||
|
|
: StatusCode(result.StatusCode ?? 404, new { error = result.Error });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|