114 lines
3.6 KiB
C#
114 lines
3.6 KiB
C#
using System.Collections.Concurrent;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using NetDaemon.HassModel;
|
|
using LeakMonitor.Models;
|
|
using LeakMonitorManager.Interface;
|
|
using LeakMonitorManager.Models;
|
|
|
|
namespace LeakMonitorManager;
|
|
|
|
public sealed class LeakNotificationService : ILeakNotificationService
|
|
{
|
|
private readonly IHaContext _haContext;
|
|
private readonly ILogger<LeakNotificationService> _logger;
|
|
private readonly LeakMonitorOptions _options;
|
|
private readonly ConcurrentDictionary<string, DateTimeOffset> _lastNotificationTimes = new();
|
|
|
|
public LeakNotificationService(
|
|
IHaContext haContext,
|
|
ILogger<LeakNotificationService> logger,
|
|
IOptions<LeakMonitorOptions> options)
|
|
{
|
|
_haContext = haContext;
|
|
_logger = logger;
|
|
_options = options.Value;
|
|
}
|
|
|
|
public void SendLeakAlert(string sensorEntityId)
|
|
{
|
|
if (IsWithinCooldown(sensorEntityId))
|
|
{
|
|
_logger.LogDebug(
|
|
"Skipping leak notification for {SensorId} -- within cooldown period",
|
|
sensorEntityId);
|
|
return;
|
|
}
|
|
|
|
_lastNotificationTimes[sensorEntityId] = DateTimeOffset.UtcNow;
|
|
|
|
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 });
|
|
|
|
_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 DateTimeOffset.UtcNow - lastTime < cooldown;
|
|
}
|
|
|
|
internal static string BuildNotificationId(string sensorEntityId)
|
|
{
|
|
var sanitized = sensorEntityId.Replace('.', '_');
|
|
return $"{LeakMonitorConsts.NotificationIdPrefix}{sanitized}";
|
|
}
|
|
}
|