using System.Text.Json; using System.Text.Json.Serialization; using AttendanceHub.Sync.Workers; namespace AttendanceHub.Sync.Configuration; public static class Phase1ConfigLoader { private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true, ReadCommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true, Converters = { new JsonStringEnumConverter() } }; public static Phase1DeviceConfig Load(string path) { var json = File.ReadAllText(path); var raw = JsonSerializer.Deserialize(json, JsonOptions) ?? throw new InvalidOperationException($"Empty config: {path}"); return new Phase1DeviceConfig( new DeviceEndpoint( raw.Device.DeviceId, raw.Device.DisplayName, raw.Device.Ip, raw.Device.Port, raw.Device.MachineNumber, raw.Device.CommKey), new SdkConfig(raw.Sdk.CommTimeoutSeconds, raw.Sdk.ExpectedSdkVersion), new SessionConfig(raw.Session.OperationTimeoutMs, raw.Session.ConnectTimeoutMs), new RealtimeConfig(raw.Realtime.EventMask, raw.Realtime.ChannelCapacity), new ReadConfig(raw.Read.Mode, raw.Read.LookbackMinutes, raw.Read.MaxRecordsPerRead, raw.Read.FailureRetryLimit, raw.Read.FailureRetryBackoffMs, raw.Read.TryTimeWindow, raw.Read.WindowedIntervalMs, raw.Read.FullReconcileMinutes, raw.Read.OverlapMinutes), new SupervisorConfig(raw.Supervisor.ReconnectDelayMs, raw.Supervisor.HealthIntervalMs, raw.Supervisor.ReadIntervalMs, raw.Supervisor.MaxReconnectDelayMs, raw.Supervisor.PingFailThreshold, raw.Supervisor.ReadRecreateThreshold, raw.Supervisor.LiveStaleMinutes)); } private sealed class Phase1ConfigFile { public DeviceJson Device { get; set; } = new(); public SdkJson Sdk { get; set; } = new(); public SessionJson Session { get; set; } = new(); public RealtimeJson Realtime { get; set; } = new(); public ReadJson Read { get; set; } = new(); public SupervisorJson Supervisor { get; set; } = new(); } private sealed class DeviceJson { public string DeviceId { get; set; } = "phase1-test-01"; public string DisplayName { get; set; } = ""; public string Ip { get; set; } = ""; public int Port { get; set; } = 4370; public int MachineNumber { get; set; } = 1; public int CommKey { get; set; } } private sealed class SdkJson { public int CommTimeoutSeconds { get; set; } = 5; public string ExpectedSdkVersion { get; set; } = "6.3.1.45"; } private sealed class SessionJson { public int OperationTimeoutMs { get; set; } = 30000; public int ConnectTimeoutMs { get; set; } = 15000; } private sealed class RealtimeJson { public int EventMask { get; set; } = 1; public int ChannelCapacity { get; set; } = 256; } private sealed class ReadJson { public string Mode { get; set; } = "Auto"; public int LookbackMinutes { get; set; } = 1440; public int MaxRecordsPerRead { get; set; } = 1_000_000; // drain cap only — full-buffer reads are unbounded by load size /// Limited retries per read cycle after a READ_FAILURE (0 = no in-cycle retry). public int FailureRetryLimit { get; set; } = 3; /// Backoff between failure retries so a failing device is not hammered. public int FailureRetryBackoffMs { get; set; } = 15000; /// Attempt the device-side windowed load first (runtime-validated, fails safe). public bool TryTimeWindow { get; set; } = true; /// Cycle interval while the windowed load is active. public int WindowedIntervalMs { get; set; } = 15000; /// Periodic full-buffer pass (completeness safety net + window validator). public int FullReconcileMinutes { get; set; } = 10; /// Overlap subtracted from the watermark when building a window. public int OverlapMinutes { get; set; } = 10; } private sealed class SupervisorJson { public int ReconnectDelayMs { get; set; } = 5000; public int HealthIntervalMs { get; set; } = 30000; public int ReadIntervalMs { get; set; } = 60000; /// Upper bound of the exponential reconnect backoff. public int MaxReconnectDelayMs { get; set; } = 60000; /// Consecutive failed liveness probes before a session is dropped. public int PingFailThreshold { get; set; } = 2; /// Consecutive failed read cycles before the READ session is recreated. public int ReadRecreateThreshold { get; set; } = 6; /// Minutes of LIVE silence (with READ traffic) before LIVE_STALE_ALARM. public int LiveStaleMinutes { get; set; } = 15; } }