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
+11 -37
View File
@@ -1,4 +1,3 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ShowerMonitor.Models;
using ShowerMonitor.Models.Enums;
@@ -7,11 +6,12 @@ using ShowerMonitorManager.Models;
namespace ShowerMonitorManager;
public sealed class ShowerDetectionService : IShowerDetectionService
public sealed class ShowerDetectionService(
IHumidityRateCalculator rateCalculator,
IOptions<ShowerMonitorOptions> options)
: IShowerDetectionService
{
private readonly IHumidityRateCalculator _rateCalculator;
private readonly ILogger<ShowerDetectionService> _logger;
private readonly ShowerMonitorOptions _options;
private readonly ShowerMonitorOptions _options = options.Value;
private readonly Lock _lock = new();
private ShowerState _currentState = ShowerState.Idle;
@@ -20,16 +20,6 @@ public sealed class ShowerDetectionService : IShowerDetectionService
private int _todaySessionCount;
private DateTimeOffset? _suspectedSince;
public ShowerDetectionService(
IHumidityRateCalculator rateCalculator,
ILogger<ShowerDetectionService> logger,
IOptions<ShowerMonitorOptions> options)
{
_rateCalculator = rateCalculator;
_logger = logger;
_options = options.Value;
}
public ShowerState CurrentState
{
get { lock (_lock) { return _currentState; } }
@@ -52,13 +42,12 @@ public sealed class ShowerDetectionService : IShowerDetectionService
public ShowerState Evaluate(double bathroomHumidity, double mainHomeHumidity, DateTimeOffset timestamp)
{
_rateCalculator.RecordReading(bathroomHumidity, timestamp);
var rate = _rateCalculator.GetRatePerMinute();
rateCalculator.RecordReading(bathroomHumidity, timestamp);
var rate = rateCalculator.GetRatePerMinute();
var differential = bathroomHumidity - mainHomeHumidity;
lock (_lock)
{
var previousState = _currentState;
_currentState = _currentState switch
{
ShowerState.Idle => EvaluateIdle(rate, differential, bathroomHumidity, timestamp),
@@ -69,13 +58,6 @@ public sealed class ShowerDetectionService : IShowerDetectionService
_ => _currentState
};
if (_currentState != previousState)
{
_logger.LogInformation(
"Shower state transition: {Previous} -> {Current} (humidity={Humidity}%, differential={Diff}%, rate={Rate}%/min)",
previousState, _currentState, bathroomHumidity, Math.Round(differential, 1), Math.Round(rate, 2));
}
return _currentState;
}
}
@@ -86,7 +68,6 @@ public sealed class ShowerDetectionService : IShowerDetectionService
{
_todaySessionCount = 0;
_lastCompletedSession = null;
_logger.LogInformation("Shower monitor daily counters reset");
}
}
@@ -103,7 +84,6 @@ public sealed class ShowerDetectionService : IShowerDetectionService
PeakTime = timestamp
};
_logger.LogInformation("Shower suspected: rate={Rate}%/min, differential={Diff}%", Math.Round(rate, 2), Math.Round(differential, 1));
return ShowerState.Suspected;
}
@@ -119,17 +99,16 @@ public sealed class ShowerDetectionService : IShowerDetectionService
}
var confirmationWindow = TimeSpan.FromMinutes(_options.ConfirmationWindowMinutes);
if (_suspectedSince.HasValue && (timestamp - _suspectedSince.Value) >= confirmationWindow
&& humidity >= _options.PeakHumidityThreshold
&& differential >= _options.MinDifferentialHumidity)
{
_logger.LogInformation("Shower confirmed: peak={Peak}%, differential={Diff}%", humidity, Math.Round(differential, 1));
return ShowerState.Active;
}
if (rate < 0 && differential < _options.MinDifferentialHumidity)
{
_logger.LogInformation("Shower suspicion cleared: differential dropped below threshold");
CompleteSessionCleanup();
return ShowerState.Idle;
}
@@ -146,9 +125,9 @@ public sealed class ShowerDetectionService : IShowerDetectionService
}
var maxDuration = TimeSpan.FromMinutes(_options.MaxShowerDurationMinutes);
if (_currentSession is not null && _currentSession.Duration >= maxDuration)
{
_logger.LogWarning("Shower exceeded max duration ({MaxDuration}), completing", maxDuration);
return TransitionToDeclining(timestamp);
}
@@ -169,9 +148,9 @@ public sealed class ShowerDetectionService : IShowerDetectionService
}
var maxDuration = TimeSpan.FromMinutes(_options.MaxShowerDurationMinutes);
if (_currentSession is not null && _currentSession.Duration >= maxDuration)
{
_logger.LogWarning("Shower declining phase exceeded max duration, completing");
CompleteSession(timestamp);
return ShowerState.Completed;
}
@@ -181,13 +160,12 @@ public sealed class ShowerDetectionService : IShowerDetectionService
private ShowerState TransitionToDeclining(DateTimeOffset timestamp)
{
_logger.LogInformation("Shower entering decline phase at {Time}", timestamp);
return ShowerState.Declining;
}
private ShowerState TransitionToIdle()
{
_rateCalculator.Reset();
rateCalculator.Reset();
return ShowerState.Idle;
}
@@ -202,10 +180,6 @@ public sealed class ShowerDetectionService : IShowerDetectionService
_lastCompletedSession = _currentSession;
_todaySessionCount++;
_logger.LogInformation(
"Shower session completed: duration={Duration}, peak={Peak}%, sessions today={Count}",
_currentSession.Duration, _currentSession.PeakHumidity, _todaySessionCount);
_currentSession = null;
_suspectedSince = null;
}