Files
HomeAssistant/ShowerMonitor.Manager/ShowerDetectionService.cs
T
gitea_admin 41ac93733e Clear Qodana findings in monitor runtime code
- Guard logger calls with IsEnabled so value-type arguments are not boxed
  when the level is disabled
- Make helper members static, narrow write-only properties to init or
  private get, and delete members no call site reaches
- Flatten nested conditionals and convert if/else chains into switch and
  ternary form
- Drop the unused *MonitorInit interfaces; NetDaemon instantiates the
  concrete app classes
- Drop the unused IConfiguration parameter from AddNordPool and
  AddPowerMonitor, and the unused IEntityManager.CreateEntitiesAsync
- Remove the ignored parameters of the boiler run-time tracker stub
- Pin the NordPool DTO price mapping explicitly so the getter use is
  visible to static analysis
- Fail Qodana on any finding and record the registration changes in
  AGENTS.md
2026-11-29 21:42:47 +02:00

208 lines
6.1 KiB
C#

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<ShowerMonitorOptions> 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)
{
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;
}
}