This commit is contained in:
2026-06-28 13:58:35 +03:00
parent 8f45028715
commit e6bf8258bb
114 changed files with 4365 additions and 874 deletions
+73 -37
View File
@@ -8,8 +8,9 @@ public sealed class WaterReadyService(ILogger<WaterReadyService> logger, TimePro
{
private const string SwitchOnState = "on";
private readonly Lock _lock = new();
private DateTimeOffset? _lastTurnedOnAt;
private DateTimeOffset? _waterReadySince;
private bool _hasObservedHeating;
private int _consecutiveBelowThresholdCount;
public bool IsInCooldown
{
@@ -22,6 +23,17 @@ public sealed class WaterReadyService(ILogger<WaterReadyService> logger, TimePro
}
}
public DateTimeOffset? WaterReadySince
{
get
{
lock (_lock)
{
return _waterReadySince;
}
}
}
public bool IsWaterReady(string? switchState, double? powerWatts, double heatingThresholdWatts)
{
lock (_lock)
@@ -43,39 +55,68 @@ public sealed class WaterReadyService(ILogger<WaterReadyService> logger, TimePro
return false;
}
if (IsWithinWarmUpGracePeriod())
lock (_lock)
{
logger.LogDebug(
"Ignoring low power reading ({PowerWatts}W) during warm-up grace period",
powerWatts.Value);
return false;
}
var isReady = powerWatts.Value < heatingThresholdWatts;
if (isReady)
{
lock (_lock)
if (!_hasObservedHeating)
{
_waterReadySince ??= timeProvider.GetUtcNow();
logger.LogDebug(
"Ignoring power reading ({PowerWatts}W) — boiler has not been observed heating yet",
powerWatts.Value);
return false;
}
logger.LogInformation(
"Water detected as ready, cooldown period ({CooldownPeriod}) started",
BoilerMonitorConsts.WaterReadyCooldownPeriod);
}
if (powerWatts.Value >= heatingThresholdWatts)
{
_consecutiveBelowThresholdCount = 0;
return false;
}
return isReady;
_consecutiveBelowThresholdCount++;
if (_consecutiveBelowThresholdCount < BoilerMonitorConsts.ConsecutiveReadingsRequired)
{
logger.LogDebug(
"Below-threshold reading {Count}/{Required} ({PowerWatts}W), waiting for confirmation",
_consecutiveBelowThresholdCount,
BoilerMonitorConsts.ConsecutiveReadingsRequired,
powerWatts.Value);
return false;
}
_waterReadySince ??= timeProvider.GetUtcNow();
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)
{
_lastTurnedOnAt = timeProvider.GetUtcNow();
_hasObservedHeating = false;
_consecutiveBelowThresholdCount = 0;
}
logger.LogDebug("Boiler turn-on recorded, warm-up grace period started ({GracePeriod})", BoilerMonitorConsts.WarmUpGracePeriod);
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()
@@ -83,33 +124,28 @@ public sealed class WaterReadyService(ILogger<WaterReadyService> logger, TimePro
lock (_lock)
{
_waterReadySince = null;
_consecutiveBelowThresholdCount = 0;
}
logger.LogInformation("Water ready cooldown has been reset");
}
private bool IsWithinCooldownPeriod()
{
if (_waterReadySince is null)
{
return false;
}
var elapsed = timeProvider.GetUtcNow() - _waterReadySince.Value;
return elapsed < BoilerMonitorConsts.WaterReadyCooldownPeriod;
}
private bool IsWithinWarmUpGracePeriod()
public void RestoreCooldownState(DateTimeOffset waterReadySince)
{
lock (_lock)
{
if (_lastTurnedOnAt is null)
var todayMidnightUtc = new DateTimeOffset(timeProvider.GetUtcNow().Date, TimeSpan.Zero);
if (waterReadySince < todayMidnightUtc)
{
return false;
logger.LogInformation(
"Persisted cooldown is from a previous day ({Since}), not restoring", waterReadySince);
return;
}
var elapsed = timeProvider.GetUtcNow() - _lastTurnedOnAt.Value;
return elapsed < BoilerMonitorConsts.WarmUpGracePeriod;
_waterReadySince = waterReadySince;
logger.LogInformation("Restored water ready cooldown from {Since}", waterReadySince);
}
}
private bool IsWithinCooldownPeriod() => _waterReadySince is not null;
}