105 lines
4.1 KiB
C#
105 lines
4.1 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Text.Json;
|
|
using Heimdall.Application.DTOs;
|
|
|
|
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>
|
|
/// keys through the settings pipeline (full reload + live SettingChanged).
|
|
/// A local list from <c>AgentOptions.Cameras</c> serves as a fallback until
|
|
/// the first Central config is received.
|
|
/// </summary>
|
|
public sealed class CameraConfigStore
|
|
{
|
|
private ConcurrentDictionary<Guid, AgentCameraConfig> _central = new();
|
|
private IReadOnlyList<AgentCameraConfig> _localFallback = [];
|
|
private volatile bool _centralReceived;
|
|
|
|
/// <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>
|
|
/// Applies a single <c>camera.<id>.<field></c> setting key received from
|
|
/// the Central. Any such key marks the Central list as authoritative.
|
|
/// </summary>
|
|
public void ApplyCameraSetting(string key, string value)
|
|
{
|
|
// camera.<id>.<field>
|
|
var parts = key.Split('.', 3);
|
|
if (parts.Length != 3 || !Guid.TryParse(parts[1], out var cameraId))
|
|
return;
|
|
|
|
_centralReceived = true;
|
|
|
|
_central.AddOrUpdate(
|
|
cameraId,
|
|
_ => BuildConfig(cameraId, parts[2], value),
|
|
(_, existing) => existing with
|
|
{
|
|
DetectionProfile = parts[2] == "profile" ? ParseProfile(value) : existing.DetectionProfile,
|
|
Name = parts[2] == "name" ? value : existing.Name,
|
|
RtspUrl = parts[2] == "rtsp" ? value : existing.RtspUrl,
|
|
SubRtspUrl = parts[2] == "sub_rtsp" ? value : existing.SubRtspUrl,
|
|
Enabled = parts[2] == "enabled" ? ParseBool(value) : existing.Enabled,
|
|
});
|
|
|
|
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()
|
|
{
|
|
if (_centralReceived)
|
|
return;
|
|
_centralReceived = true;
|
|
RaiseChanged();
|
|
}
|
|
|
|
/// <summary>Atomically replace the authoritative snapshot, including removals.</summary>
|
|
public void ReplaceCentral(IReadOnlyDictionary<string, string> settings)
|
|
{
|
|
var replacement = new CameraConfigStore();
|
|
foreach (var setting in settings.Where(s => s.Key.StartsWith("camera.", StringComparison.OrdinalIgnoreCase)))
|
|
replacement.ApplyCameraSetting(setting.Key, setting.Value);
|
|
Interlocked.Exchange(ref _central, replacement._central);
|
|
_centralReceived = true;
|
|
RaiseChanged();
|
|
}
|
|
|
|
/// <summary>Effective camera assignments: Central list, or local fallback before that.</summary>
|
|
public IReadOnlyList<AgentCameraConfig> Cameras =>
|
|
_centralReceived ? _central.Values.ToArray() : _localFallback;
|
|
|
|
private static AgentCameraConfig BuildConfig(Guid cameraId, string field, string value) => new(
|
|
cameraId,
|
|
field == "name" ? value : string.Empty,
|
|
field == "rtsp" ? value : string.Empty,
|
|
field == "sub_rtsp" ? value : null,
|
|
field == "enabled" ? ParseBool(value) : true,
|
|
field == "profile" ? ParseProfile(value) : null);
|
|
|
|
private static DetectionProfileDto ParseProfile(string value)
|
|
{
|
|
var profile = JsonSerializer.Deserialize<DetectionProfileDto>(value) ?? throw new ArgumentException("Empty detection profile");
|
|
profile.Validate();
|
|
return profile;
|
|
}
|
|
|
|
private static bool ParseBool(string value) =>
|
|
bool.TryParse(value, out var parsed) ? parsed : value is "1" or "true" or "True";
|
|
|
|
private void RaiseChanged() => Changed?.Invoke(this, EventArgs.Empty);
|
|
}
|