Track manual snapshots and apply versioned camera config
This commit is contained in:
1 parent
f4a8f4a668
commit
8f00f69a5f
30 files changed
+645
-63
No files matched your search
@@ -7,6 +7,7 @@ using Heimdall.Application.Cameras.Queries.GetCamera;
|
||||
using Heimdall.Application.Cameras.Queries.GetCamerasBySite;
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Domain.Entities;
|
||||
using Heimdall.Application.Services;
|
||||
using Heimdall.Domain.Enums;
|
||||
using Heimdall.Infrastructure.Security;
|
||||
@@ -46,11 +47,67 @@ public class CamerasController : ControllerBase
|
||||
if (camera.Status != CameraStatus.Online) return Conflict(new { error = "The camera is not online" });
|
||||
if (camera.AgentId is null) return Conflict(new { error = "No agent is assigned to this camera" });
|
||||
var requestId = Guid.NewGuid();
|
||||
var snapshotRequest = new ManualSnapshotRequest(requestId, id, camera.AgentId.Value, DateTime.UtcNow);
|
||||
_db.ManualSnapshotRequests.Add(snapshotRequest);
|
||||
await _db.SaveChangesWithRetryAsync(ct: ct);
|
||||
if (!await _broadcast.RequestManualSnapshot(camera.AgentId.Value, id, requestId, ct))
|
||||
{
|
||||
snapshotRequest.Fail("L'agent affecté est indisponible.", DateTime.UtcNow);
|
||||
await _db.SaveChangesWithRetryAsync(ct: ct);
|
||||
return Conflict(new { error = "The assigned agent is offline" });
|
||||
}
|
||||
return Accepted(new { requestId });
|
||||
}
|
||||
|
||||
/// <summary>Read the durable outcome of a manual capture request.</summary>
|
||||
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.CameraRead)]
|
||||
[HttpGet("{id:guid}/snapshot/{requestId:guid}")]
|
||||
public async Task<IActionResult> GetSnapshotRequest(Guid id, Guid requestId, CancellationToken ct)
|
||||
{
|
||||
var request = await _db.ManualSnapshotRequests.AsNoTracking()
|
||||
.FirstOrDefaultAsync(r => r.Id == requestId && r.CameraId == id, ct);
|
||||
if (request is null) return NotFound();
|
||||
if (request.Status == ManualSnapshotStatus.Pending && request.ExpiresAt <= DateTime.UtcNow)
|
||||
{
|
||||
await _db.ManualSnapshotRequests
|
||||
.Where(r => r.Id == requestId && r.Status == ManualSnapshotStatus.Pending && r.ExpiresAt <= DateTime.UtcNow)
|
||||
.ExecuteUpdateAsync(setters => setters
|
||||
.SetProperty(r => r.Status, ManualSnapshotStatus.TimedOut)
|
||||
.SetProperty(r => r.FailureReason, "La capture n'a pas été livrée dans le délai attendu.")
|
||||
.SetProperty(r => r.UpdatedAt, DateTime.UtcNow), ct);
|
||||
request = await _db.ManualSnapshotRequests.AsNoTracking()
|
||||
.FirstAsync(r => r.Id == requestId, ct);
|
||||
}
|
||||
return Ok(new { requestId = request.Id, status = request.Status.ToString(),
|
||||
reason = request.FailureReason, updatedAt = request.UpdatedAt });
|
||||
}
|
||||
|
||||
/// <summary>Record a capture failure reported by the agent assigned when the command was issued.</summary>
|
||||
[Authorize(AuthenticationSchemes = "ApiKey", Policy = AppPermissions.CameraWrite)]
|
||||
[HttpPut("{id:guid}/snapshot/{requestId:guid}/failure")]
|
||||
public async Task<IActionResult> FailSnapshotRequest(Guid id, Guid requestId,
|
||||
[FromBody] ManualSnapshotFailure failure, CancellationToken ct)
|
||||
{
|
||||
var exists = await _db.ManualSnapshotRequests.AnyAsync(r => r.Id == requestId && r.CameraId == id, ct);
|
||||
if (!exists) return NotFound();
|
||||
var reason = failure.Code switch
|
||||
{
|
||||
"camera_unavailable" => "La caméra n'est plus affectée ou est désactivée sur l'agent.",
|
||||
"no_frame" => "Aucune image n'a été reçue du flux de la caméra.",
|
||||
"queue_failed" => "L'agent n'a pas pu enregistrer la capture pour livraison.",
|
||||
_ => "La capture a échoué sur l'agent."
|
||||
};
|
||||
await _db.ManualSnapshotRequests
|
||||
.Where(r => r.Id == requestId && r.CameraId == id && r.Status != ManualSnapshotStatus.Completed)
|
||||
.ExecuteUpdateAsync(setters => setters
|
||||
.SetProperty(r => r.Status, ManualSnapshotStatus.Failed)
|
||||
.SetProperty(r => r.FailureReason, reason)
|
||||
.SetProperty(r => r.UpdatedAt, DateTime.UtcNow), ct);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
public sealed record ManualSnapshotFailure(string Code);
|
||||
|
||||
/// <summary>Create a new camera.</summary>
|
||||
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.CameraWrite)]
|
||||
[HttpPost]
|
||||
|
||||
Reference in new issue
Block a user