f919d6b25b
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
118 lines
4.1 KiB
C#
118 lines
4.1 KiB
C#
#region
|
|
|
|
using System.Collections.Concurrent;
|
|
using LeakMonitor.Models;
|
|
using LeakMonitorManager.Interface;
|
|
using LeakMonitorManager.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using NetDaemon.HassModel;
|
|
|
|
#endregion
|
|
|
|
namespace LeakMonitorManager;
|
|
|
|
public sealed class LeakNotificationService(
|
|
IHaContext haContext,
|
|
ILogger<LeakNotificationService> logger,
|
|
IOptions<LeakMonitorOptions> options,
|
|
TimeProvider timeProvider) : ILeakNotificationService
|
|
{
|
|
private readonly ConcurrentDictionary<string, DateTimeOffset> _lastNotificationTimes = new ConcurrentDictionary<string, DateTimeOffset>();
|
|
private readonly LeakMonitorOptions _options = options.Value;
|
|
|
|
public void SendLeakAlert(string sensorEntityId)
|
|
{
|
|
if (IsWithinCooldown(sensorEntityId))
|
|
{
|
|
if (logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
logger.LogDebug(
|
|
"Skipping leak notification for {SensorId} -- within cooldown period",
|
|
sensorEntityId);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
_lastNotificationTimes[sensorEntityId] = timeProvider.GetUtcNow();
|
|
|
|
var notificationId = BuildNotificationId(sensorEntityId);
|
|
var message = $"Water detected by {sensorEntityId}! Check immediately.";
|
|
|
|
SendMobilePushNotification(message);
|
|
CreatePersistentNotification(notificationId, message);
|
|
|
|
logger.LogWarning("Leak alert sent for sensor {SensorId}", sensorEntityId);
|
|
}
|
|
|
|
public void DismissLeakAlert(string sensorEntityId)
|
|
{
|
|
var notificationId = BuildNotificationId(sensorEntityId);
|
|
|
|
haContext.CallService(
|
|
LeakMonitorConsts.PersistentNotificationDomain,
|
|
LeakMonitorConsts.PersistentNotificationDismissService,
|
|
data: new { notification_id = notificationId });
|
|
|
|
if (logger.IsEnabled(LogLevel.Information))
|
|
{
|
|
logger.LogInformation("Leak alert dismissed for sensor {SensorId}", sensorEntityId);
|
|
}
|
|
}
|
|
|
|
private void SendMobilePushNotification(string message)
|
|
{
|
|
haContext.CallService(
|
|
LeakMonitorConsts.NotifyDomain,
|
|
LeakMonitorConsts.MobileAppService,
|
|
data: new
|
|
{
|
|
message,
|
|
title = LeakMonitorConsts.NotificationTitle,
|
|
data = new
|
|
{
|
|
push = new
|
|
{
|
|
sound = new
|
|
{
|
|
critical = 1,
|
|
volume = 1.0
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void CreatePersistentNotification(string notificationId, string message)
|
|
{
|
|
haContext.CallService(
|
|
LeakMonitorConsts.PersistentNotificationDomain,
|
|
LeakMonitorConsts.PersistentNotificationCreateService,
|
|
data: new
|
|
{
|
|
title = LeakMonitorConsts.NotificationTitle,
|
|
message,
|
|
notification_id = notificationId
|
|
});
|
|
}
|
|
|
|
private bool IsWithinCooldown(string sensorEntityId)
|
|
{
|
|
if (!_lastNotificationTimes.TryGetValue(sensorEntityId, out var lastTime))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var cooldown = TimeSpan.FromMinutes(_options.NotificationCooldownMinutes);
|
|
|
|
return timeProvider.GetUtcNow() - lastTime < cooldown;
|
|
}
|
|
|
|
internal static string BuildNotificationId(string sensorEntityId)
|
|
{
|
|
var sanitized = sensorEntityId.Replace('.', '_');
|
|
|
|
return $"{LeakMonitorConsts.NotificationIdPrefix}{sanitized}";
|
|
}
|
|
} |