Refactor services to use DI for IHaContext and TimeProvider
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user