using Microsoft.Extensions.Options; using ShowerMonitor.Models; using ShowerMonitor.Models.Enums; using ShowerMonitorManager.Interface; using ShowerMonitorManager.Models; namespace ShowerMonitorManager; public sealed class ShowerDetectionService( IHumidityRateCalculator rateCalculator, IOptions options) : IShowerDetectionService { private readonly ShowerMonitorOptions _options = options.Value; private readonly Lock _lock = new(); private ShowerState _currentState = ShowerState.Idle; private ShowerSession? _currentSession; private ShowerSession? _lastCompletedSession; private int _todaySessionCount; private DateTimeOffset? _suspectedSince; private double _lastRate; 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) { if (rate >= _options.SpikeRateThreshold && differential >= _options.MinDifferentialHumidity) { _suspectedSince = timestamp; _currentSession = new ShowerSession { StartTime = timestamp, PreEventHumidity = rateCalculator.PreviousReading ?? humidity, PeakHumidity = humidity, PeakTime = timestamp }; return ShowerState.Suspected; } return ShowerState.Idle; } 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; } if (rate < 0 && differential < _options.MinDifferentialHumidity) { CompleteSessionCleanup(); return ShowerState.Idle; } return ShowerState.Suspected; } 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); if (_currentSession is not null && _currentSession.GetDuration(timestamp) >= maxDuration) { return TransitionToDeclining(); } if (rate < 0 && differential < _options.MinDifferentialHumidity) { 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); if (_currentSession is not null && _currentSession.GetDuration(timestamp) >= maxDuration) { CompleteSession(timestamp); return ShowerState.Completed; } return ShowerState.Declining; } private 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; } }