Refactor services to use DI for IHaContext and TimeProvider

This commit is contained in:
2026-07-03 12:15:44 +03:00
parent 7f280bea7b
commit 2706a6b188
23 changed files with 146 additions and 374 deletions
@@ -4,7 +4,7 @@ using Microsoft.Extensions.Logging;
namespace BoilerMonitorManager;
public class BoilerControlService(ILogger<BoilerControlService> logger) : IBoilerControlService
public sealed class BoilerControlService(ILogger<BoilerControlService> logger) : IBoilerControlService
{
private const string SwitchOnState = "on";
+30 -36
View File
@@ -31,7 +31,7 @@ public sealed class BoilerMonitorInit : IBoilerMonitorInit
private readonly TimeProvider _timeProvider;
private bool _lastWaterReady;
private bool _cleanedUp;
private volatile bool _cleanedUp;
private DateTimeOffset? _lastWaterReadyNotificationSentAt;
public BoilerMonitorInit(
@@ -178,17 +178,7 @@ public sealed class BoilerMonitorInit : IBoilerMonitorInit
_logger.LogInformation("Scheduling BoilerMonitor polling every {Interval}s, first at {Time}", DefaultPollIntervalSeconds, nextPollTime);
_scheduler.RunEvery(TimeSpan.FromSeconds(DefaultPollIntervalSeconds), nextPollTime, async void () =>
{
try
{
await PollAndUpdateAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error during BoilerMonitor poll cycle");
}
});
_scheduler.RunEvery(TimeSpan.FromSeconds(DefaultPollIntervalSeconds), nextPollTime, () => _ = PollAndUpdateAsync());
}
private void ScheduleMidnightReset()
@@ -311,18 +301,20 @@ public sealed class BoilerMonitorInit : IBoilerMonitorInit
foreach (var entityId in configEntityIds)
{
var subscription = await _mqttEntityManager.PrepareCommandSubscriptionAsync(entityId).ConfigureAwait(false);
subscription.Subscribe(async void (state) =>
{
try
{
_logger.LogInformation("Config entity {EntityId} received command: {Value}", entityId, state);
await _mqttEntityManager.SetStateAsync(entityId, state).ConfigureAwait(false);
}
catch (Exception e)
{
_logger.LogError(e, "Error handling command for entity {EntityId}", entityId);
}
});
subscription.Subscribe(state => _ = HandleConfigCommandAsync(entityId, state));
}
}
private async Task HandleConfigCommandAsync(string entityId, string? state)
{
try
{
_logger.LogInformation("Config entity {EntityId} received command: {Value}", entityId, state);
await _mqttEntityManager.SetStateAsync(entityId, state ?? string.Empty).ConfigureAwait(false);
}
catch (Exception e)
{
_logger.LogError(e, "Error handling command for entity {EntityId}", entityId);
}
}
@@ -338,18 +330,20 @@ public sealed class BoilerMonitorInit : IBoilerMonitorInit
foreach (var entityId in switchEntityIds)
{
var subscription = await _mqttEntityManager.PrepareCommandSubscriptionAsync(entityId).ConfigureAwait(false);
subscription.Subscribe(async void (state) =>
{
try
{
_logger.LogInformation("Switch {EntityId} received command: {Value}", entityId, state);
await _mqttEntityManager.SetStateAsync(entityId, state).ConfigureAwait(false);
}
catch (Exception e)
{
_logger.LogError(e, "Error handling command for switch entity {EntityId}", entityId);
}
});
subscription.Subscribe(state => _ = HandleSwitchCommandAsync(entityId, state));
}
}
private async Task HandleSwitchCommandAsync(string entityId, string? state)
{
try
{
_logger.LogInformation("Switch {EntityId} received command: {Value}", entityId, state);
await _mqttEntityManager.SetStateAsync(entityId, state ?? string.Empty).ConfigureAwait(false);
}
catch (Exception e)
{
_logger.LogError(e, "Error handling command for switch entity {EntityId}", entityId);
}
}
@@ -15,10 +15,6 @@ public static class BoilerMonitorServiceRegistration
.GetSection(BoilerMonitorOptions.SectionName)
.Get<BoilerMonitorOptions>() ?? new BoilerMonitorOptions();
// Always register: NordPool and other features resolve boiler tuning from HA entity state
// even when the BoilerMonitor app itself is disabled.
services.AddScoped<IBoilerConfigReader, BoilerConfigReader>();
if (!options.Enabled)
{
return services;
@@ -29,8 +25,7 @@ public static class BoilerMonitorServiceRegistration
.AddSingleton<IBoilerControlService, BoilerControlService>()
.AddSingleton<IBoilerRunTimeTracker, BoilerRunTimeTracker>()
.AddTransient<IBoilerMonitorEntityService, BoilerMonitorEntityService>()
.AddSingleton<IWaterReadyService, WaterReadyService>()
.AddSingleton(TimeProvider.System);
.AddSingleton<IWaterReadyService, WaterReadyService>();
return services;
}
@@ -3,7 +3,7 @@ using BoilerMonitorManager.Models;
namespace BoilerMonitorManager;
public class BoilerRunTimeTracker : IBoilerRunTimeTracker
public sealed class BoilerRunTimeTracker : IBoilerRunTimeTracker
{
private readonly Lock _lock = new();
private TimeSpan _accumulatedRunTime = TimeSpan.Zero;