Refactor services to use DI for IHaContext and TimeProvider
This commit is contained in:
@@ -7,7 +7,9 @@ using ShowerMonitorManager.Models;
|
||||
|
||||
namespace ShowerMonitorManager;
|
||||
|
||||
public sealed class ExhaustFanControlService(IOptions<ShowerMonitorOptions> options) : IExhaustFanControlService
|
||||
public sealed class ExhaustFanControlService(
|
||||
IHaContext haContext,
|
||||
IOptions<ShowerMonitorOptions> options) : IExhaustFanControlService
|
||||
{
|
||||
private readonly ShowerMonitorOptions _options = options.Value;
|
||||
private readonly Lock _lock = new();
|
||||
@@ -15,7 +17,7 @@ public sealed class ExhaustFanControlService(IOptions<ShowerMonitorOptions> opti
|
||||
private bool _fanTurnedOnByUs;
|
||||
private DateTimeOffset? _showerEndedAt;
|
||||
|
||||
public void Evaluate(ShowerState state, DateTimeOffset timestamp, IHaContext haContext)
|
||||
public void Evaluate(ShowerState state, DateTimeOffset timestamp)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
@@ -23,24 +25,24 @@ public sealed class ExhaustFanControlService(IOptions<ShowerMonitorOptions> opti
|
||||
{
|
||||
case ShowerState.Active:
|
||||
case ShowerState.Suspected:
|
||||
TurnOnFanIfNeeded(haContext);
|
||||
TurnOnFanIfNeeded();
|
||||
_showerEndedAt = null;
|
||||
break;
|
||||
|
||||
case ShowerState.Declining:
|
||||
case ShowerState.Completed:
|
||||
_showerEndedAt ??= timestamp;
|
||||
EvaluatePostShowerFanOff(timestamp, haContext);
|
||||
EvaluatePostShowerFanOff(timestamp);
|
||||
break;
|
||||
|
||||
case ShowerState.Idle:
|
||||
EvaluatePostShowerFanOff(timestamp, haContext);
|
||||
EvaluatePostShowerFanOff(timestamp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset(IHaContext haContext)
|
||||
public void Reset()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
@@ -58,7 +60,7 @@ public sealed class ExhaustFanControlService(IOptions<ShowerMonitorOptions> opti
|
||||
}
|
||||
}
|
||||
|
||||
private void TurnOnFanIfNeeded(IHaContext haContext)
|
||||
private void TurnOnFanIfNeeded()
|
||||
{
|
||||
if (_fanTurnedOnByUs)
|
||||
{
|
||||
@@ -75,7 +77,7 @@ public sealed class ExhaustFanControlService(IOptions<ShowerMonitorOptions> opti
|
||||
_fanTurnedOnByUs = true;
|
||||
}
|
||||
|
||||
private void EvaluatePostShowerFanOff(DateTimeOffset timestamp, IHaContext haContext)
|
||||
private void EvaluatePostShowerFanOff(DateTimeOffset timestamp)
|
||||
{
|
||||
if (!_fanTurnedOnByUs || !_showerEndedAt.HasValue)
|
||||
{
|
||||
|
||||
@@ -5,6 +5,6 @@ namespace ShowerMonitorManager.Interface;
|
||||
|
||||
public interface IExhaustFanControlService
|
||||
{
|
||||
void Evaluate(ShowerState state, DateTimeOffset timestamp, IHaContext haContext);
|
||||
void Reset(IHaContext haContext);
|
||||
void Evaluate(ShowerState state, DateTimeOffset timestamp);
|
||||
void Reset();
|
||||
}
|
||||
|
||||
@@ -23,8 +23,9 @@ public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
private readonly IExhaustFanControlService _fanControlService;
|
||||
private readonly IShowerMonitorEntityService _entityService;
|
||||
private readonly ShowerMonitorOptions _options;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
|
||||
private bool _cleanedUp;
|
||||
private volatile bool _cleanedUp;
|
||||
|
||||
public ShowerMonitorInit(
|
||||
IHaContext haContext,
|
||||
@@ -33,7 +34,8 @@ public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
IShowerDetectionService detectionService,
|
||||
IExhaustFanControlService fanControlService,
|
||||
IShowerMonitorEntityService entityService,
|
||||
IOptions<ShowerMonitorOptions> options)
|
||||
IOptions<ShowerMonitorOptions> options,
|
||||
TimeProvider timeProvider)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_logger = logger;
|
||||
@@ -42,6 +44,7 @@ public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
_fanControlService = fanControlService;
|
||||
_entityService = entityService;
|
||||
_options = options.Value;
|
||||
_timeProvider = timeProvider;
|
||||
|
||||
_ = InitializeAndScheduleAsync();
|
||||
}
|
||||
@@ -94,22 +97,12 @@ public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
private void SchedulePeriodicPolling()
|
||||
{
|
||||
var pollInterval = TimeSpan.FromSeconds(_options.PollIntervalSeconds);
|
||||
var now = DateTime.Now;
|
||||
var now = _timeProvider.GetLocalNow();
|
||||
var currentSecondInInterval = (now.Minute * 60 + now.Second) % _options.PollIntervalSeconds;
|
||||
var secondsUntilNext = _options.PollIntervalSeconds - currentSecondInInterval;
|
||||
var nextPollTime = now.Add(TimeSpan.FromSeconds(secondsUntilNext));
|
||||
|
||||
_scheduler.RunEvery(pollInterval, nextPollTime, async void () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await PollAndUpdateAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during ShowerMonitor poll cycle");
|
||||
}
|
||||
});
|
||||
_scheduler.RunEvery(pollInterval, nextPollTime, () => _ = PollAndUpdateAsync());
|
||||
}
|
||||
|
||||
private void ScheduleMidnightReset()
|
||||
@@ -146,7 +139,7 @@ public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
await _entityService.CleanupEntitiesAsync();
|
||||
_cleanedUp = true;
|
||||
_detectionService.ResetDailyCounters();
|
||||
_fanControlService.Reset(_haContext);
|
||||
_fanControlService.Reset();
|
||||
_logger.LogDebug("ShowerMonitor cleanup complete");
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -174,11 +167,11 @@ public sealed class ShowerMonitorInit : IShowerMonitorInit
|
||||
return;
|
||||
}
|
||||
|
||||
var timestamp = DateTimeOffset.UtcNow;
|
||||
var timestamp = _timeProvider.GetUtcNow();
|
||||
var state = _detectionService.Evaluate(bathroomHumidity.Value, mainHomeHumidity.Value, timestamp);
|
||||
var differential = bathroomHumidity.Value - mainHomeHumidity.Value;
|
||||
|
||||
_fanControlService.Evaluate(state, timestamp, _haContext);
|
||||
_fanControlService.Evaluate(state, timestamp);
|
||||
|
||||
var context = new ShowerMonitorUpdateContext
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ public static class ShowerMonitorServiceRegistration
|
||||
.AddNetDaemonApp<ShowerMonitorInit>()
|
||||
.AddSingleton<IHumidityRateCalculator>(_ => new HumidityRateCalculator(options.RateWindowSize))
|
||||
.AddSingleton<IShowerDetectionService, ShowerDetectionService>()
|
||||
.AddSingleton<IExhaustFanControlService, ExhaustFanControlService>()
|
||||
.AddScoped<IExhaustFanControlService, ExhaustFanControlService>()
|
||||
.AddTransient<IShowerMonitorEntityService, ShowerMonitorEntityService>();
|
||||
|
||||
return services;
|
||||
|
||||
Reference in New Issue
Block a user