Files
HomeAssistant/BoilerMonitor.Manager/WaterReadyService.cs
T
gitea_admin 77458383d1 Add braces to single-statement guards and refactor NordPool price fetching
- Expand inline guard, return and logger blocks across the monitor services, init classes and tests
- Have INordPoolPriceService take a per-date fetch delegate and expose ParseAndAdjust, moving the HA fetch call into NordPoolInit so the service can be registered singleton
- Record the NordPool and boiler service lifetimes in AGENTS.md
- Drop the unused persisted-state argument from the boiler test data helper
2026-12-01 11:59:31 +02:00

167 lines
4.7 KiB
C#

using BoilerMonitor.Models;
using BoilerMonitorManager.Interface;
using Microsoft.Extensions.Logging;
namespace BoilerMonitorManager;
public sealed class WaterReadyService(ILogger<WaterReadyService> 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;
}
}