feat: migrate boiler control implementation to BoilerMonitor namespace and add budget handling logic
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
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 DateTimeOffset? _lastTurnedOnAt;
|
||||
private DateTimeOffset? _waterReadySince;
|
||||
|
||||
public bool IsInCooldown
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return IsWithinCooldownPeriod();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsWaterReady(string? switchState, double? powerWatts, double heatingThresholdWatts)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (IsWithinCooldownPeriod())
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
if (IsWithinWarmUpGracePeriod())
|
||||
{
|
||||
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)
|
||||
{
|
||||
_waterReadySince ??= timeProvider.GetUtcNow();
|
||||
}
|
||||
|
||||
logger.LogInformation(
|
||||
"Water detected as ready, cooldown period ({CooldownPeriod}) started",
|
||||
BoilerMonitorConsts.WaterReadyCooldownPeriod);
|
||||
}
|
||||
|
||||
return isReady;
|
||||
}
|
||||
|
||||
public void NotifyTurnedOn()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_lastTurnedOnAt = timeProvider.GetUtcNow();
|
||||
}
|
||||
|
||||
logger.LogDebug("Boiler turn-on recorded, warm-up grace period started ({GracePeriod})", BoilerMonitorConsts.WarmUpGracePeriod);
|
||||
}
|
||||
|
||||
public void ResetCooldown()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_waterReadySince = null;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_lastTurnedOnAt is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var elapsed = timeProvider.GetUtcNow() - _lastTurnedOnAt.Value;
|
||||
return elapsed < BoilerMonitorConsts.WarmUpGracePeriod;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user