f919d6b25b
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
239 lines
6.4 KiB
C#
239 lines
6.4 KiB
C#
#region
|
|
|
|
using Microsoft.Extensions.Options;
|
|
using ShowerMonitor.Models;
|
|
using ShowerMonitor.Models.Enums;
|
|
using ShowerMonitorManager.Interface;
|
|
using ShowerMonitorManager.Models;
|
|
|
|
#endregion
|
|
|
|
namespace ShowerMonitorManager;
|
|
|
|
public sealed class ShowerDetectionService(
|
|
IHumidityRateCalculator rateCalculator,
|
|
IOptions<ShowerMonitorOptions> options)
|
|
: IShowerDetectionService
|
|
{
|
|
private readonly Lock _lock = new Lock();
|
|
private readonly ShowerMonitorOptions _options = options.Value;
|
|
private ShowerSession? _currentSession;
|
|
|
|
private ShowerState _currentState = ShowerState.Idle;
|
|
private ShowerSession? _lastCompletedSession;
|
|
private double _lastRate;
|
|
private DateTimeOffset? _suspectedSince;
|
|
private int _todaySessionCount;
|
|
|
|
public ShowerState CurrentState
|
|
{
|
|
get
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _currentState;
|
|
}
|
|
}
|
|
}
|
|
|
|
public ShowerSession? CurrentSession
|
|
{
|
|
get
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _currentSession;
|
|
}
|
|
}
|
|
}
|
|
|
|
public ShowerSession? LastCompletedSession
|
|
{
|
|
get
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _lastCompletedSession;
|
|
}
|
|
}
|
|
}
|
|
|
|
public int TodaySessionCount
|
|
{
|
|
get
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _todaySessionCount;
|
|
}
|
|
}
|
|
}
|
|
|
|
public double LastRate
|
|
{
|
|
get
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _lastRate;
|
|
}
|
|
}
|
|
}
|
|
|
|
public ShowerState Evaluate(double bathroomHumidity, double mainHomeHumidity, DateTimeOffset timestamp)
|
|
{
|
|
rateCalculator.RecordReading(bathroomHumidity, timestamp);
|
|
var rate = rateCalculator.GetRatePerMinute();
|
|
var differential = bathroomHumidity - mainHomeHumidity;
|
|
|
|
lock (_lock)
|
|
{
|
|
_lastRate = rate;
|
|
|
|
_currentState = _currentState switch
|
|
{
|
|
ShowerState.Idle => EvaluateIdle(rate, differential, bathroomHumidity, timestamp),
|
|
ShowerState.Suspected => EvaluateSuspected(rate, differential, bathroomHumidity, timestamp),
|
|
ShowerState.Active => EvaluateActive(rate, differential, bathroomHumidity, timestamp),
|
|
ShowerState.Declining => EvaluateDeclining(differential, timestamp),
|
|
ShowerState.Completed => TransitionToIdle(),
|
|
_ => _currentState
|
|
};
|
|
|
|
return _currentState;
|
|
}
|
|
}
|
|
|
|
public void ResetDailyCounters()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
_todaySessionCount = 0;
|
|
_lastCompletedSession = null;
|
|
}
|
|
}
|
|
|
|
private ShowerState EvaluateIdle(double rate, double differential, double humidity, DateTimeOffset timestamp)
|
|
{
|
|
var showerSuspected = rate >= _options.SpikeRateThreshold && differential >= _options.MinDifferentialHumidity;
|
|
|
|
if (!showerSuspected)
|
|
{
|
|
return ShowerState.Idle;
|
|
}
|
|
|
|
_suspectedSince = timestamp;
|
|
|
|
_currentSession = new ShowerSession
|
|
{
|
|
StartTime = timestamp,
|
|
PreEventHumidity = rateCalculator.PreviousReading ?? humidity,
|
|
PeakHumidity = humidity,
|
|
PeakTime = timestamp
|
|
};
|
|
|
|
return ShowerState.Suspected;
|
|
}
|
|
|
|
private ShowerState EvaluateSuspected(double rate, double differential, double humidity, DateTimeOffset timestamp)
|
|
{
|
|
if (_currentSession is not null && humidity > _currentSession.PeakHumidity)
|
|
{
|
|
_currentSession.PeakHumidity = humidity;
|
|
_currentSession.PeakTime = timestamp;
|
|
}
|
|
|
|
var confirmationWindow = TimeSpan.FromMinutes(_options.ConfirmationWindowMinutes);
|
|
|
|
if (_suspectedSince.HasValue && timestamp - _suspectedSince.Value >= confirmationWindow && humidity >= _options.PeakHumidityThreshold && differential >= _options.MinDifferentialHumidity)
|
|
{
|
|
return ShowerState.Active;
|
|
}
|
|
|
|
var showerAbandoned = rate < 0 && differential < _options.MinDifferentialHumidity;
|
|
|
|
if (!showerAbandoned)
|
|
{
|
|
return ShowerState.Suspected;
|
|
}
|
|
|
|
CompleteSessionCleanup();
|
|
|
|
return ShowerState.Idle;
|
|
}
|
|
|
|
private ShowerState EvaluateActive(double rate, double differential, double humidity, DateTimeOffset timestamp)
|
|
{
|
|
if (_currentSession is not null && humidity > _currentSession.PeakHumidity)
|
|
{
|
|
_currentSession.PeakHumidity = humidity;
|
|
_currentSession.PeakTime = timestamp;
|
|
}
|
|
|
|
var maxDuration = TimeSpan.FromMinutes(_options.MaxShowerDurationMinutes);
|
|
var maxDurationReached = _currentSession is not null && _currentSession.GetDuration(timestamp) >= maxDuration;
|
|
var humidityRecovered = rate < 0 && differential < _options.MinDifferentialHumidity;
|
|
|
|
if (maxDurationReached || humidityRecovered)
|
|
{
|
|
return TransitionToDeclining();
|
|
}
|
|
|
|
return ShowerState.Active;
|
|
}
|
|
|
|
private ShowerState EvaluateDeclining(double differential, DateTimeOffset timestamp)
|
|
{
|
|
if (differential <= _options.RecoveryDifferentialThreshold)
|
|
{
|
|
CompleteSession(timestamp);
|
|
|
|
return ShowerState.Completed;
|
|
}
|
|
|
|
var maxDuration = TimeSpan.FromMinutes(_options.MaxShowerDurationMinutes);
|
|
var maxDurationReached = _currentSession is not null && _currentSession.GetDuration(timestamp) >= maxDuration;
|
|
|
|
if (!maxDurationReached)
|
|
{
|
|
return ShowerState.Declining;
|
|
}
|
|
|
|
CompleteSession(timestamp);
|
|
|
|
return ShowerState.Completed;
|
|
}
|
|
|
|
private static ShowerState TransitionToDeclining()
|
|
{
|
|
return ShowerState.Declining;
|
|
}
|
|
|
|
private ShowerState TransitionToIdle()
|
|
{
|
|
rateCalculator.Reset();
|
|
|
|
return ShowerState.Idle;
|
|
}
|
|
|
|
private void CompleteSession(DateTimeOffset endTime)
|
|
{
|
|
if (_currentSession is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_currentSession.EndTime = endTime;
|
|
_lastCompletedSession = _currentSession;
|
|
_todaySessionCount++;
|
|
|
|
_currentSession = null;
|
|
_suspectedSince = null;
|
|
}
|
|
|
|
private void CompleteSessionCleanup()
|
|
{
|
|
_currentSession = null;
|
|
_suspectedSince = null;
|
|
}
|
|
} |