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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user