using BoilerMonitor.Models; using BoilerMonitorManager.Interface; using Microsoft.Extensions.Logging; namespace BoilerMonitorManager; public sealed class WaterReadyService(ILogger logger, TimeProvider timeProvider) : IWaterReadyService { private const string SwitchOnState = "on"; private readonly Lock _lock = new(); private int _consecutiveBelowThresholdCount; private bool _hasObservedHeating; private DateTimeOffset? _waterReadySince; public bool IsInCooldown { get { lock (_lock) { return IsWithinCooldownPeriod(); } } } public DateTimeOffset? WaterReadySince { get { lock (_lock) { return _waterReadySince; } } } public bool IsWaterReady(string? switchState, double? powerWatts, double heatingThresholdWatts) { lock (_lock) { if (IsWithinCooldownPeriod()) { if (logger.IsEnabled(LogLevel.Debug)) logger.LogDebug("Water ready cooldown active (since {Since}), suppressing turn-on", _waterReadySince); return true; } } if (switchState is null || powerWatts is null) return false; if (!string.Equals(switchState, SwitchOnState, StringComparison.OrdinalIgnoreCase)) return false; lock (_lock) { if (!_hasObservedHeating) { if (logger.IsEnabled(LogLevel.Debug)) logger.LogDebug("Ignoring power reading ({PowerWatts}W) — boiler has not been observed heating yet", powerWatts.Value); return false; } if (powerWatts.Value >= heatingThresholdWatts) { _consecutiveBelowThresholdCount = 0; return false; } _consecutiveBelowThresholdCount++; if (_consecutiveBelowThresholdCount < BoilerMonitorConsts.ConsecutiveReadingsRequired) { if (logger.IsEnabled(LogLevel.Debug)) logger.LogDebug( "Below-threshold reading {Count}/{Required} ({PowerWatts}W), waiting for confirmation", _consecutiveBelowThresholdCount, BoilerMonitorConsts.ConsecutiveReadingsRequired, powerWatts.Value); return false; } _waterReadySince ??= timeProvider.GetUtcNow(); if (logger.IsEnabled(LogLevel.Information)) logger.LogInformation( "Water detected as ready after {Count} consecutive readings, cooldown active until shower or midnight reset", _consecutiveBelowThresholdCount); return true; } } public void NotifyTurnedOn() { lock (_lock) { _hasObservedHeating = false; _consecutiveBelowThresholdCount = 0; } logger.LogDebug( "Boiler turn-on recorded, awaiting first heating observation before water ready detection is enabled"); } public void RecordHeatingObserved() { lock (_lock) { if (_hasObservedHeating) return; _hasObservedHeating = true; } logger.LogDebug("Heating observed, water ready detection is now enabled"); } public void ResetCooldown() { lock (_lock) { _waterReadySince = null; _consecutiveBelowThresholdCount = 0; } logger.LogInformation("Water ready cooldown has been reset"); } public void RestoreCooldownState(DateTimeOffset waterReadySince) { lock (_lock) { var todayMidnightUtc = new DateTimeOffset(timeProvider.GetUtcNow().Date, TimeSpan.Zero); if (waterReadySince < todayMidnightUtc) { if (logger.IsEnabled(LogLevel.Information)) logger.LogInformation("Persisted cooldown is from a previous day ({Since}), not restoring", waterReadySince); return; } _waterReadySince = waterReadySince; if (logger.IsEnabled(LogLevel.Information)) logger.LogInformation("Restored water ready cooldown from {Since}", waterReadySince); } } private bool IsWithinCooldownPeriod() { return _waterReadySince is not null; } }