feat(events): add SignalR real-time event broadcasting to web UI

This commit is contained in:
Arnaud Dartois committed 2026-07-19 15:24:52 +00:00
1 parent 0d2f0574c9
commit c5728fa810
5 files changed
+121 -9

No files matched your search

@@ -4,6 +4,7 @@ using Heimdall.Application.Services;
using Heimdall.Domain.Entities;
using Heimdall.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Heimdall.Infrastructure.Services;
@@ -11,13 +12,16 @@ namespace Heimdall.Infrastructure.Services;
public sealed class CameraEventService : ICameraEventService
{
private readonly IDbContextFactory<AppDbContext> _dbFactory;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<CameraEventService> _logger;
public CameraEventService(
IDbContextFactory<AppDbContext> dbFactory,
IServiceProvider serviceProvider,
ILogger<CameraEventService> logger)
{
_dbFactory = dbFactory;
_serviceProvider = serviceProvider;
_logger = logger;
}
@@ -40,6 +44,21 @@ public sealed class CameraEventService : ICameraEventService
"Camera event {EventId}: {Type} (confidence={Conf}) for camera {CameraId}",
ev.Id, ev.Type, ev.Confidence, ev.CameraId);
// Fire-and-forget broadcast to connected SignalR clients
_ = Task.Run(async () =>
{
try
{
using var scope = _serviceProvider.CreateScope();
var broadcast = scope.ServiceProvider.GetRequiredService<IEventBroadcastService>();
await broadcast.NotifyEventCreated(MapResponse(ev));
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to broadcast event {EventId}", ev.Id);
}
}, CancellationToken.None);
return ServiceResult<CameraEventResponse>.Success(MapResponse(ev));
}