Refactor services to use DI for IHaContext and TimeProvider
This commit is contained in:
@@ -25,9 +25,10 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
private readonly IPowerCostCalculationService _costService;
|
||||
private readonly IPowerMonitorEntityService _entityService;
|
||||
private readonly IPowerNotificationService _notificationService;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
|
||||
private int _mainMeterConsecutiveFailures;
|
||||
private DateTime? _anomalyStartTime;
|
||||
private DateTimeOffset? _anomalyStartTime;
|
||||
private bool _highPowerNotificationActive;
|
||||
|
||||
public PowerMonitorInit(
|
||||
@@ -39,7 +40,8 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
IOtherPowerCalculationService calculationService,
|
||||
IPowerCostCalculationService costService,
|
||||
IPowerMonitorEntityService entityService,
|
||||
IPowerNotificationService notificationService)
|
||||
IPowerNotificationService notificationService,
|
||||
TimeProvider timeProvider)
|
||||
{
|
||||
_haContext = haContext;
|
||||
_logger = logger;
|
||||
@@ -50,6 +52,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
_costService = costService;
|
||||
_entityService = entityService;
|
||||
_notificationService = notificationService;
|
||||
_timeProvider = timeProvider;
|
||||
|
||||
_ = InitializeAndScheduleAsync();
|
||||
}
|
||||
@@ -100,7 +103,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
|
||||
private void SchedulePeriodicPolling()
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var now = _timeProvider.GetLocalNow();
|
||||
var intervalSeconds = _options.PollIntervalSeconds;
|
||||
var currentSecondInInterval = (now.Minute * 60 + now.Second) % intervalSeconds;
|
||||
var secondsUntilNext = intervalSeconds - currentSecondInInterval;
|
||||
@@ -111,17 +114,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
"Scheduling PowerMonitor polling every {Interval}s, first at {Time}",
|
||||
intervalSeconds, nextPollTime);
|
||||
|
||||
_scheduler.RunEvery(TimeSpan.FromSeconds(intervalSeconds), nextPollTime, async void () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await PollAndUpdateAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during PowerMonitor poll cycle");
|
||||
}
|
||||
});
|
||||
_scheduler.RunEvery(TimeSpan.FromSeconds(intervalSeconds), nextPollTime, () => _ = PollAndUpdateAsync());
|
||||
}
|
||||
|
||||
private void SubscribeToRediscoverButton()
|
||||
@@ -195,12 +188,14 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
var priceSource = nordPoolPrice > 0 ? "live" : _costService.PriceSource;
|
||||
var effectivePrice = nordPoolPrice > 0 ? nordPoolPrice : _costService.LastKnownPrice;
|
||||
|
||||
_costService.RecordCosts(calculation, nordPoolPrice, _options.PollIntervalSeconds, DateTime.Now);
|
||||
var now = _timeProvider.GetLocalNow();
|
||||
|
||||
_costService.RecordCosts(calculation, nordPoolPrice, _options.PollIntervalSeconds, now.LocalDateTime);
|
||||
|
||||
var isAnomaly = calculation.SmoothedOtherPowerWatts > _options.AnomalyThresholdWatts;
|
||||
if (isAnomaly && _anomalyStartTime is null)
|
||||
{
|
||||
_anomalyStartTime = DateTime.Now;
|
||||
_anomalyStartTime = now;
|
||||
}
|
||||
else if (!isAnomaly)
|
||||
{
|
||||
@@ -208,7 +203,7 @@ public sealed class PowerMonitorInit : IPowerMonitorInit
|
||||
}
|
||||
|
||||
var anomalyDuration = _anomalyStartTime.HasValue
|
||||
? DateTime.Now - _anomalyStartTime.Value
|
||||
? now - _anomalyStartTime.Value
|
||||
: TimeSpan.Zero;
|
||||
|
||||
var systemStatus = _mainMeterConsecutiveFailures >= PowerMonitorConsts.MainMeterMaxConsecutiveFailures
|
||||
|
||||
Reference in New Issue
Block a user