Refactor services to use DI for IHaContext and TimeProvider
This commit is contained in:
@@ -13,16 +13,19 @@ public sealed class LeakNotificationService : ILeakNotificationService
|
||||
private readonly IHaContext _haContext;
|
||||
private readonly ILogger<LeakNotificationService> _logger;
|
||||
private readonly LeakMonitorOptions _options;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private readonly ConcurrentDictionary<string, DateTimeOffset> _lastNotificationTimes = new();
|
||||
|
||||
public LeakNotificationService(
|
||||
IHaContext haContext,
|
||||
ILogger<LeakNotificationService> logger,
|
||||
IOptions<LeakMonitorOptions> options)
|
||||
IOptions<LeakMonitorOptions> options,
|
||||
TimeProvider timeProvider)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
_timeProvider = timeProvider;
|
||||
}
|
||||
|
||||
public void SendLeakAlert(string sensorEntityId)
|
||||
@@ -35,7 +38,7 @@ public sealed class LeakNotificationService : ILeakNotificationService
|
||||
return;
|
||||
}
|
||||
|
||||
_lastNotificationTimes[sensorEntityId] = DateTimeOffset.UtcNow;
|
||||
_lastNotificationTimes[sensorEntityId] = _timeProvider.GetUtcNow();
|
||||
|
||||
var notificationId = BuildNotificationId(sensorEntityId);
|
||||
var message = $"Water detected by {sensorEntityId}! Check immediately.";
|
||||
@@ -102,7 +105,7 @@ public sealed class LeakNotificationService : ILeakNotificationService
|
||||
}
|
||||
|
||||
var cooldown = TimeSpan.FromMinutes(_options.NotificationCooldownMinutes);
|
||||
return DateTimeOffset.UtcNow - lastTime < cooldown;
|
||||
return _timeProvider.GetUtcNow() - lastTime < cooldown;
|
||||
}
|
||||
|
||||
internal static string BuildNotificationId(string sensorEntityId)
|
||||
|
||||
Reference in New Issue
Block a user