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
This commit is contained in:
@@ -38,6 +38,10 @@ public sealed class ExhaustFanControlService(
|
||||
case ShowerState.Idle:
|
||||
EvaluatePostShowerFanOff(timestamp);
|
||||
break;
|
||||
|
||||
default:
|
||||
// Unknown state: leave the fan untouched.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ namespace ShowerMonitorManager.Interface;
|
||||
|
||||
public interface IShowerDetectionService
|
||||
{
|
||||
ShowerState CurrentState { get; }
|
||||
ShowerSession? CurrentSession { get; }
|
||||
ShowerSession? LastCompletedSession { get; }
|
||||
int TodaySessionCount { get; }
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace ShowerMonitorManager.Interface;
|
||||
|
||||
public interface IShowerMonitorInit
|
||||
{
|
||||
Task InitializeAsync();
|
||||
Task CleanupEntitiesAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -6,11 +6,8 @@ namespace ShowerMonitorManager.Models;
|
||||
public sealed class ShowerMonitorUpdateContext
|
||||
{
|
||||
public required ShowerState State { get; init; }
|
||||
public required double BathroomHumidity { get; init; }
|
||||
public required double MainHomeHumidity { get; init; }
|
||||
public required double Differential { get; init; }
|
||||
public required double HumidityRate { get; init; }
|
||||
public required int TodaySessionCount { get; init; }
|
||||
public ShowerSession? LastCompletedSession { get; init; }
|
||||
public ShowerSession? CurrentSession { get; init; }
|
||||
}
|
||||
|
||||
@@ -80,21 +80,24 @@ public sealed class ShowerDetectionService(
|
||||
|
||||
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
|
||||
};
|
||||
var showerSuspected = rate >= _options.SpikeRateThreshold
|
||||
&& differential >= _options.MinDifferentialHumidity;
|
||||
|
||||
return ShowerState.Suspected;
|
||||
if (!showerSuspected)
|
||||
{
|
||||
return ShowerState.Idle;
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -106,21 +109,23 @@ public sealed class ShowerDetectionService(
|
||||
}
|
||||
|
||||
var confirmationWindow = TimeSpan.FromMinutes(_options.ConfirmationWindowMinutes);
|
||||
|
||||
if (_suspectedSince.HasValue && (timestamp - _suspectedSince.Value) >= confirmationWindow
|
||||
|
||||
if (_suspectedSince.HasValue && timestamp - _suspectedSince.Value >= confirmationWindow
|
||||
&& humidity >= _options.PeakHumidityThreshold
|
||||
&& differential >= _options.MinDifferentialHumidity)
|
||||
{
|
||||
return ShowerState.Active;
|
||||
}
|
||||
|
||||
if (rate < 0 && differential < _options.MinDifferentialHumidity)
|
||||
var showerAbandoned = rate < 0 && differential < _options.MinDifferentialHumidity;
|
||||
if (!showerAbandoned)
|
||||
{
|
||||
CompleteSessionCleanup();
|
||||
return ShowerState.Idle;
|
||||
return ShowerState.Suspected;
|
||||
}
|
||||
|
||||
return ShowerState.Suspected;
|
||||
CompleteSessionCleanup();
|
||||
|
||||
return ShowerState.Idle;
|
||||
}
|
||||
|
||||
private ShowerState EvaluateActive(double rate, double differential, double humidity, DateTimeOffset timestamp)
|
||||
@@ -132,13 +137,11 @@ public sealed class ShowerDetectionService(
|
||||
}
|
||||
|
||||
var maxDuration = TimeSpan.FromMinutes(_options.MaxShowerDurationMinutes);
|
||||
var maxDurationReached = _currentSession is not null
|
||||
&& _currentSession.GetDuration(timestamp) >= maxDuration;
|
||||
var humidityRecovered = rate < 0 && differential < _options.MinDifferentialHumidity;
|
||||
|
||||
if (_currentSession is not null && _currentSession.GetDuration(timestamp) >= maxDuration)
|
||||
{
|
||||
return TransitionToDeclining();
|
||||
}
|
||||
|
||||
if (rate < 0 && differential < _options.MinDifferentialHumidity)
|
||||
if (maxDurationReached || humidityRecovered)
|
||||
{
|
||||
return TransitionToDeclining();
|
||||
}
|
||||
@@ -151,23 +154,25 @@ public sealed class ShowerDetectionService(
|
||||
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 (_currentSession is not null && _currentSession.GetDuration(timestamp) >= maxDuration)
|
||||
if (!maxDurationReached)
|
||||
{
|
||||
CompleteSession(timestamp);
|
||||
|
||||
return ShowerState.Completed;
|
||||
return ShowerState.Declining;
|
||||
}
|
||||
|
||||
return ShowerState.Declining;
|
||||
CompleteSession(timestamp);
|
||||
|
||||
return ShowerState.Completed;
|
||||
}
|
||||
|
||||
private ShowerState TransitionToDeclining()
|
||||
private static ShowerState TransitionToDeclining()
|
||||
{
|
||||
return ShowerState.Declining;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ using static ShowerMonitor.Models.ShowerMonitorConsts;
|
||||
namespace ShowerMonitorManager;
|
||||
|
||||
[NetDaemonApp]
|
||||
public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
public sealed class ShowerMonitorInit
|
||||
{
|
||||
private readonly ILogger<ShowerMonitorInit> _logger;
|
||||
private readonly IHaContext _haContext;
|
||||
@@ -63,7 +63,7 @@ public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
}
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
private async Task InitializeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -88,11 +88,6 @@ public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _entityService.CleanupEntitiesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private void SchedulePeriodicPolling()
|
||||
{
|
||||
var pollInterval = TimeSpan.FromSeconds(_options.PollIntervalSeconds);
|
||||
@@ -162,7 +157,11 @@ public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
|
||||
if (!bathroomHumidity.HasValue || !mainHomeHumidity.HasValue)
|
||||
{
|
||||
_logger.LogDebug("Humidity sensor data unavailable (bathroom={Bathroom}, home={Home})", bathroomHumidity, mainHomeHumidity);
|
||||
if (_logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
_logger.LogDebug("Humidity sensor data unavailable (bathroom={Bathroom}, home={Home})", bathroomHumidity, mainHomeHumidity);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -175,19 +174,19 @@ public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
var context = new ShowerMonitorUpdateContext
|
||||
{
|
||||
State = state,
|
||||
BathroomHumidity = bathroomHumidity.Value,
|
||||
MainHomeHumidity = mainHomeHumidity.Value,
|
||||
Differential = differential,
|
||||
HumidityRate = _detectionService.LastRate,
|
||||
TodaySessionCount = _detectionService.TodaySessionCount,
|
||||
LastCompletedSession = _detectionService.LastCompletedSession,
|
||||
CurrentSession = _detectionService.CurrentSession
|
||||
LastCompletedSession = _detectionService.LastCompletedSession
|
||||
};
|
||||
|
||||
await _entityService.UpdateEntitiesAsync(context);
|
||||
|
||||
_logger.LogDebug("Poll: bathroom={Bath}%, home={Home}%, diff={Diff}%, state={State}",
|
||||
bathroomHumidity.Value, mainHomeHumidity.Value, Math.Round(differential, 1), state);
|
||||
if (_logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
_logger.LogDebug("Poll: bathroom={Bath}%, home={Home}%, diff={Diff}%, state={State}",
|
||||
bathroomHumidity.Value, mainHomeHumidity.Value, Math.Round(differential, 1), state);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user