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:
@@ -1,6 +0,0 @@
|
||||
namespace LeakMonitorManager.Interface;
|
||||
|
||||
public interface ILeakMonitorInit
|
||||
{
|
||||
Task InitializeAsync();
|
||||
}
|
||||
@@ -10,7 +10,7 @@ using LeakMonitorManager.Models;
|
||||
namespace LeakMonitorManager;
|
||||
|
||||
[NetDaemonApp]
|
||||
public sealed class LeakMonitorInit : ILeakMonitorInit
|
||||
public sealed class LeakMonitorInit
|
||||
{
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILeakNotificationService _notificationService;
|
||||
@@ -31,7 +31,7 @@ public sealed class LeakMonitorInit : ILeakMonitorInit
|
||||
_ = InitializeAsync();
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
private Task InitializeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -47,7 +47,10 @@ public sealed class LeakMonitorInit : ILeakMonitorInit
|
||||
SubscribeToLeakSensor(sensorEntityId);
|
||||
}
|
||||
|
||||
_logger.LogInformation("LeakMonitor initialized, monitoring {Count} sensor(s)", _options.SensorEntityIds.Length);
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("LeakMonitor initialized, monitoring {Count} sensor(s)", _options.SensorEntityIds.Length);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -74,7 +77,10 @@ public sealed class LeakMonitorInit : ILeakMonitorInit
|
||||
}
|
||||
});
|
||||
|
||||
_logger.LogInformation("Subscribed to leak sensor {SensorId}", sensorEntityId);
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Subscribed to leak sensor {SensorId}", sensorEntityId);
|
||||
}
|
||||
}
|
||||
|
||||
internal void HandleStateChange(string sensorEntityId, StateChange change)
|
||||
@@ -89,8 +95,11 @@ public sealed class LeakMonitorInit : ILeakMonitorInit
|
||||
}
|
||||
else if (string.Equals(newState, LeakMonitorConsts.WaterClearedState, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_logger.LogInformation("Water leak cleared by {SensorId}", sensorEntityId);
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Water leak cleared by {SensorId}", sensorEntityId);
|
||||
}
|
||||
|
||||
_notificationService.DismissLeakAlert(sensorEntityId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace LeakMonitorManager;
|
||||
|
||||
public static class LeakMonitorServiceRegistration
|
||||
{
|
||||
public static IServiceCollection AddLeakMonitor(this IServiceCollection services, IConfiguration configuration)
|
||||
public static void AddLeakMonitor(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var options = configuration
|
||||
.GetSection(LeakMonitorOptions.SectionName)
|
||||
@@ -16,11 +16,9 @@ public static class LeakMonitorServiceRegistration
|
||||
|
||||
if (!options.Enabled)
|
||||
{
|
||||
return services;
|
||||
return;
|
||||
}
|
||||
|
||||
services.AddNetDaemonApp<LeakMonitorInit>().AddScoped<ILeakNotificationService, LeakNotificationService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,37 +8,30 @@ using LeakMonitorManager.Models;
|
||||
|
||||
namespace LeakMonitorManager;
|
||||
|
||||
public sealed class LeakNotificationService : ILeakNotificationService
|
||||
public sealed class LeakNotificationService(
|
||||
IHaContext haContext,
|
||||
ILogger<LeakNotificationService> logger,
|
||||
IOptions<LeakMonitorOptions> options,
|
||||
TimeProvider timeProvider) : ILeakNotificationService
|
||||
{
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILogger<LeakNotificationService> _logger;
|
||||
private readonly LeakMonitorOptions _options;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private readonly LeakMonitorOptions _options = options.Value;
|
||||
private readonly ConcurrentDictionary<string, DateTimeOffset> _lastNotificationTimes = new();
|
||||
|
||||
public LeakNotificationService(
|
||||
IHaContext haContext,
|
||||
ILogger<LeakNotificationService> logger,
|
||||
IOptions<LeakMonitorOptions> options,
|
||||
TimeProvider timeProvider)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
_timeProvider = timeProvider;
|
||||
}
|
||||
|
||||
public void SendLeakAlert(string sensorEntityId)
|
||||
{
|
||||
if (IsWithinCooldown(sensorEntityId))
|
||||
{
|
||||
_logger.LogDebug(
|
||||
"Skipping leak notification for {SensorId} -- within cooldown period",
|
||||
sensorEntityId);
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug(
|
||||
"Skipping leak notification for {SensorId} -- within cooldown period",
|
||||
sensorEntityId);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_lastNotificationTimes[sensorEntityId] = _timeProvider.GetUtcNow();
|
||||
_lastNotificationTimes[sensorEntityId] = timeProvider.GetUtcNow();
|
||||
|
||||
var notificationId = BuildNotificationId(sensorEntityId);
|
||||
var message = $"Water detected by {sensorEntityId}! Check immediately.";
|
||||
@@ -46,24 +39,27 @@ public sealed class LeakNotificationService : ILeakNotificationService
|
||||
SendMobilePushNotification(message);
|
||||
CreatePersistentNotification(notificationId, message);
|
||||
|
||||
_logger.LogWarning("Leak alert sent for sensor {SensorId}", sensorEntityId);
|
||||
logger.LogWarning("Leak alert sent for sensor {SensorId}", sensorEntityId);
|
||||
}
|
||||
|
||||
public void DismissLeakAlert(string sensorEntityId)
|
||||
{
|
||||
var notificationId = BuildNotificationId(sensorEntityId);
|
||||
|
||||
_haContext.CallService(
|
||||
haContext.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationDismissService,
|
||||
data: new { notification_id = notificationId });
|
||||
|
||||
_logger.LogInformation("Leak alert dismissed for sensor {SensorId}", sensorEntityId);
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation("Leak alert dismissed for sensor {SensorId}", sensorEntityId);
|
||||
}
|
||||
}
|
||||
|
||||
private void SendMobilePushNotification(string message)
|
||||
{
|
||||
_haContext.CallService(
|
||||
haContext.CallService(
|
||||
LeakMonitorConsts.NotifyDomain,
|
||||
LeakMonitorConsts.MobileAppService,
|
||||
data: new
|
||||
@@ -86,7 +82,7 @@ public sealed class LeakNotificationService : ILeakNotificationService
|
||||
|
||||
private void CreatePersistentNotification(string notificationId, string message)
|
||||
{
|
||||
_haContext.CallService(
|
||||
haContext.CallService(
|
||||
LeakMonitorConsts.PersistentNotificationDomain,
|
||||
LeakMonitorConsts.PersistentNotificationCreateService,
|
||||
data: new
|
||||
@@ -105,7 +101,7 @@ public sealed class LeakNotificationService : ILeakNotificationService
|
||||
}
|
||||
|
||||
var cooldown = TimeSpan.FromMinutes(_options.NotificationCooldownMinutes);
|
||||
return _timeProvider.GetUtcNow() - lastTime < cooldown;
|
||||
return timeProvider.GetUtcNow() - lastTime < cooldown;
|
||||
}
|
||||
|
||||
internal static string BuildNotificationId(string sensorEntityId)
|
||||
|
||||
@@ -6,9 +6,9 @@ public sealed class LeakMonitorOptions
|
||||
{
|
||||
public const string SectionName = "LeakMonitor";
|
||||
|
||||
public bool Enabled { get; set; }
|
||||
public bool Enabled { get; init; }
|
||||
|
||||
public string[] SensorEntityIds { get; set; } = [];
|
||||
public string[] SensorEntityIds { get; init; } = [];
|
||||
|
||||
public int NotificationCooldownMinutes { get; set; } = LeakMonitorConsts.DefaultNotificationCooldownMinutes;
|
||||
public int NotificationCooldownMinutes { get; init; } = LeakMonitorConsts.DefaultNotificationCooldownMinutes;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user