This commit is contained in:
2026-06-28 14:22:13 +03:00
parent e6bf8258bb
commit 7f280bea7b
10 changed files with 54 additions and 17 deletions
@@ -19,6 +19,7 @@ public sealed class ShowerDetectionService(
private ShowerSession? _lastCompletedSession;
private int _todaySessionCount;
private DateTimeOffset? _suspectedSince;
private double _lastRate;
public ShowerState CurrentState
{
@@ -40,6 +41,11 @@ public sealed class ShowerDetectionService(
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);
@@ -48,6 +54,7 @@ public sealed class ShowerDetectionService(
lock (_lock)
{
_lastRate = rate;
_currentState = _currentState switch
{
ShowerState.Idle => EvaluateIdle(rate, differential, bathroomHumidity, timestamp),
@@ -79,7 +86,7 @@ public sealed class ShowerDetectionService(
_currentSession = new ShowerSession
{
StartTime = timestamp,
PreEventHumidity = humidity - (rate > 0 ? rate : 0),
PreEventHumidity = rateCalculator.PreviousReading ?? humidity,
PeakHumidity = humidity,
PeakTime = timestamp
};
@@ -125,15 +132,15 @@ public sealed class ShowerDetectionService(
}
var maxDuration = TimeSpan.FromMinutes(_options.MaxShowerDurationMinutes);
if (_currentSession is not null && _currentSession.Duration >= maxDuration)
if (_currentSession is not null && _currentSession.GetDuration(timestamp) >= maxDuration)
{
return TransitionToDeclining(timestamp);
return TransitionToDeclining();
}
if (rate < 0 && differential < _options.PeakHumidityThreshold - _options.RecoveryDifferentialThreshold)
if (rate < 0 && differential < _options.MinDifferentialHumidity)
{
return TransitionToDeclining(timestamp);
return TransitionToDeclining();
}
return ShowerState.Active;
@@ -148,8 +155,8 @@ public sealed class ShowerDetectionService(
}
var maxDuration = TimeSpan.FromMinutes(_options.MaxShowerDurationMinutes);
if (_currentSession is not null && _currentSession.Duration >= maxDuration)
if (_currentSession is not null && _currentSession.GetDuration(timestamp) >= maxDuration)
{
CompleteSession(timestamp);
return ShowerState.Completed;
@@ -158,7 +165,7 @@ public sealed class ShowerDetectionService(
return ShowerState.Declining;
}
private ShowerState TransitionToDeclining(DateTimeOffset timestamp)
private ShowerState TransitionToDeclining()
{
return ShowerState.Declining;
}