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
@@ -10,6 +10,7 @@ public sealed class PowerCostCalculationService : IPowerCostCalculationService
private readonly ConcurrentDictionary<string, CostAccumulator> _deviceAccumulators = new();
private CostAccumulator? _totalAccumulator;
private CostAccumulator? _otherAccumulator;
private readonly object _accumulatorLock = new();
private double _lastKnownPrice;
private string _priceSource = "live";
@@ -63,12 +64,18 @@ public sealed class PowerCostCalculationService : IPowerCostCalculationService
public CostSnapshot GetTotalCostSnapshot()
{
return _totalAccumulator?.GetSnapshot() ?? CreateEmptySnapshot();
lock (_accumulatorLock)
{
return _totalAccumulator?.GetSnapshot() ?? CreateEmptySnapshot();
}
}
public CostSnapshot GetOtherCostSnapshot()
{
return _otherAccumulator?.GetSnapshot() ?? CreateEmptySnapshot();
lock (_accumulatorLock)
{
return _otherAccumulator?.GetSnapshot() ?? CreateEmptySnapshot();
}
}
public CostSnapshot GetDeviceCostSnapshot(string entityId)
@@ -93,12 +100,18 @@ public sealed class PowerCostCalculationService : IPowerCostCalculationService
private CostAccumulator GetOrCreateTotalAccumulator(DateTime now)
{
return _totalAccumulator ??= new CostAccumulator(now);
lock (_accumulatorLock)
{
return _totalAccumulator ??= new CostAccumulator(now);
}
}
private CostAccumulator GetOrCreateOtherAccumulator(DateTime now)
{
return _otherAccumulator ??= new CostAccumulator(now);
lock (_accumulatorLock)
{
return _otherAccumulator ??= new CostAccumulator(now);
}
}
private static CostSnapshot CreateEmptySnapshot()