2026-10-20 02:26:49 +02:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using Heimdall.Application.DTOs;
|
2026-08-11 21:16:28 +02:00
|
|
|
|
|
|
|
|
namespace Heimdall.Agent.Services;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Holds the camera assignments the agent must capture. The Central is the
|
|
|
|
|
/// source of truth: cameras are shipped as flat <c>camera.<id>.name|rtsp|sub_rtsp|enabled</c>
|
2026-10-23 07:30:58 +02:00
|
|
|
/// keys through the settings pipeline as one complete, versioned snapshot.
|
2026-08-11 21:16:28 +02:00
|
|
|
/// A local list from <c>AgentOptions.Cameras</c> serves as a fallback until
|
|
|
|
|
/// the first Central config is received.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class CameraConfigStore
|
|
|
|
|
{
|
2026-10-23 07:30:58 +02:00
|
|
|
private sealed record CameraSnapshot(IReadOnlyDictionary<Guid, AgentCameraConfig> Cameras, string Version);
|
|
|
|
|
private CameraSnapshot? _central;
|
|
|
|
|
private readonly object _updateGate = new();
|
2026-08-11 21:16:28 +02:00
|
|
|
private IReadOnlyList<AgentCameraConfig> _localFallback = [];
|
|
|
|
|
|
|
|
|
|
/// <summary>Raised whenever the effective camera list may have changed.</summary>
|
|
|
|
|
public event EventHandler? Changed;
|
|
|
|
|
|
|
|
|
|
/// <summary>Sets the local fallback list (used until Central config arrives).</summary>
|
|
|
|
|
public void SetLocalFallback(IReadOnlyList<AgentCameraConfig> cameras)
|
|
|
|
|
{
|
|
|
|
|
_localFallback = cameras;
|
|
|
|
|
RaiseChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-10-23 07:30:58 +02:00
|
|
|
/// Legacy single-key setter for local callers. Remote updates must use ReplaceCentral.
|
2026-08-11 21:16:28 +02:00
|
|
|
/// </summary>
|
|
|
|
|
public void ApplyCameraSetting(string key, string value)
|
|
|
|
|
{
|
|
|
|
|
// camera.<id>.<field>
|
|
|
|
|
var parts = key.Split('.', 3);
|
2026-10-23 07:30:58 +02:00
|
|
|
if (!key.StartsWith("camera.", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
return;
|
2026-08-11 21:16:28 +02:00
|
|
|
if (parts.Length != 3 || !Guid.TryParse(parts[1], out var cameraId))
|
|
|
|
|
return;
|
2026-10-23 07:30:58 +02:00
|
|
|
lock (_updateGate)
|
|
|
|
|
{
|
|
|
|
|
var previous = _central?.Cameras ?? new Dictionary<Guid, AgentCameraConfig>();
|
|
|
|
|
var next = new Dictionary<Guid, AgentCameraConfig>(previous);
|
|
|
|
|
next[cameraId] = ApplyField(next.GetValueOrDefault(cameraId) ??
|
|
|
|
|
new AgentCameraConfig(cameraId, string.Empty, string.Empty, null, true), parts[2], value);
|
|
|
|
|
Volatile.Write(ref _central, new CameraSnapshot(next, string.Empty));
|
|
|
|
|
}
|
2026-08-11 21:16:28 +02:00
|
|
|
RaiseChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Marks the Central config as received; the local fallback is no longer
|
|
|
|
|
/// used, even if the Central list is empty.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void MarkCentralReceived()
|
|
|
|
|
{
|
2026-10-23 07:30:58 +02:00
|
|
|
if (Interlocked.CompareExchange(ref _central,
|
|
|
|
|
new CameraSnapshot(new Dictionary<Guid, AgentCameraConfig>(), string.Empty), null) is not null)
|
2026-08-11 21:16:28 +02:00
|
|
|
return;
|
|
|
|
|
RaiseChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-20 02:26:49 +02:00
|
|
|
/// <summary>Atomically replace the authoritative snapshot, including removals.</summary>
|
2026-10-23 07:30:58 +02:00
|
|
|
public void ReplaceCentral(IReadOnlyDictionary<string, string> settings, string? expectedVersion = null)
|
2026-10-20 02:26:49 +02:00
|
|
|
{
|
2026-10-23 07:30:58 +02:00
|
|
|
var version = CameraConfigVersion.Compute(settings);
|
|
|
|
|
if (expectedVersion is not null && !string.Equals(expectedVersion, version, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
throw new ArgumentException("Camera configuration version mismatch", nameof(expectedVersion));
|
|
|
|
|
var replacement = new Dictionary<Guid, AgentCameraConfig>();
|
2026-10-20 02:26:49 +02:00
|
|
|
foreach (var setting in settings.Where(s => s.Key.StartsWith("camera.", StringComparison.OrdinalIgnoreCase)))
|
2026-10-23 07:30:58 +02:00
|
|
|
{
|
|
|
|
|
var parts = setting.Key.Split('.', 3);
|
|
|
|
|
if (parts.Length != 3 || !Guid.TryParse(parts[1], out var id))
|
|
|
|
|
throw new ArgumentException("Invalid camera setting key", nameof(settings));
|
|
|
|
|
replacement[id] = ApplyField(replacement.GetValueOrDefault(id) ??
|
|
|
|
|
new AgentCameraConfig(id, string.Empty, string.Empty, null, true), parts[2], setting.Value);
|
|
|
|
|
}
|
|
|
|
|
lock (_updateGate)
|
|
|
|
|
Volatile.Write(ref _central, new CameraSnapshot(replacement, version));
|
2026-10-20 02:26:49 +02:00
|
|
|
RaiseChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 21:16:28 +02:00
|
|
|
/// <summary>Effective camera assignments: Central list, or local fallback before that.</summary>
|
|
|
|
|
public IReadOnlyList<AgentCameraConfig> Cameras =>
|
2026-10-23 07:30:58 +02:00
|
|
|
Volatile.Read(ref _central)?.Cameras.Values.ToArray() ?? _localFallback;
|
2026-08-11 21:16:28 +02:00
|
|
|
|
2026-10-23 07:30:58 +02:00
|
|
|
public string? CurrentVersion => Volatile.Read(ref _central)?.Version;
|
|
|
|
|
|
|
|
|
|
private static AgentCameraConfig ApplyField(AgentCameraConfig existing, string field, string value) => field switch
|
|
|
|
|
{
|
|
|
|
|
"name" => existing with { Name = value },
|
|
|
|
|
"rtsp" => existing with { RtspUrl = value },
|
|
|
|
|
"sub_rtsp" => existing with { SubRtspUrl = value },
|
|
|
|
|
"enabled" => existing with { Enabled = ParseBool(value) },
|
|
|
|
|
"profile" => existing with { DetectionProfile = ParseProfile(value) },
|
|
|
|
|
_ => throw new ArgumentException($"Unknown camera configuration field: {field}", nameof(field))
|
|
|
|
|
};
|
2026-10-20 02:26:49 +02:00
|
|
|
|
|
|
|
|
private static DetectionProfileDto ParseProfile(string value)
|
|
|
|
|
{
|
|
|
|
|
var profile = JsonSerializer.Deserialize<DetectionProfileDto>(value) ?? throw new ArgumentException("Empty detection profile");
|
|
|
|
|
profile.Validate();
|
|
|
|
|
return profile;
|
|
|
|
|
}
|
2026-08-11 21:16:28 +02:00
|
|
|
|
2026-10-23 07:30:58 +02:00
|
|
|
private static bool ParseBool(string value) => value switch
|
|
|
|
|
{
|
|
|
|
|
"1" => true,
|
|
|
|
|
"0" => false,
|
|
|
|
|
_ when bool.TryParse(value, out var parsed) => parsed,
|
|
|
|
|
_ => throw new ArgumentException("Invalid camera enabled value", nameof(value))
|
|
|
|
|
};
|
2026-08-11 21:16:28 +02:00
|
|
|
|
|
|
|
|
private void RaiseChanged() => Changed?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|