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
+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);
}
}