Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
#region
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
using PowerMonitorManager.Interface;
|
||||
using PowerMonitorManager.Models;
|
||||
using PowerMonitorManager.Models.Consts;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace PowerMonitorManager;
|
||||
|
||||
public sealed class PowerCostCalculationService : IPowerCostCalculationService
|
||||
{
|
||||
private readonly Lock _accumulatorLock = new();
|
||||
private readonly ConcurrentDictionary<string, CostAccumulator> _deviceAccumulators = new();
|
||||
private readonly Lock _priceLock = new();
|
||||
private readonly Lock _accumulatorLock = new Lock();
|
||||
private readonly ConcurrentDictionary<string, CostAccumulator> _deviceAccumulators = new ConcurrentDictionary<string, CostAccumulator>();
|
||||
private readonly Lock _priceLock = new Lock();
|
||||
|
||||
private double _lastKnownPrice;
|
||||
private CostAccumulator? _otherAccumulator;
|
||||
@@ -38,8 +42,10 @@ public sealed class PowerCostCalculationService : IPowerCostCalculationService
|
||||
}
|
||||
}
|
||||
|
||||
public void RecordCosts(PowerCalculationResult calculation, double nordPoolPrice, int pollIntervalSeconds,
|
||||
DateTime now)
|
||||
public void RecordCosts(PowerCalculationResult calculation,
|
||||
double nordPoolPrice,
|
||||
int pollIntervalSeconds,
|
||||
DateTime now)
|
||||
{
|
||||
lock (_priceLock)
|
||||
{
|
||||
@@ -57,6 +63,7 @@ public sealed class PowerCostCalculationService : IPowerCostCalculationService
|
||||
|
||||
var otherCost =
|
||||
CalculateCostIncrement(calculation.SmoothedOtherPowerWatts, effectivePrice, pollIntervalSeconds);
|
||||
|
||||
GetOrCreateOtherAccumulator(now).AddCost(otherCost, now);
|
||||
|
||||
foreach (var (entityId, powerWatts) in calculation.DevicePowerReadings)
|
||||
@@ -86,8 +93,8 @@ public sealed class PowerCostCalculationService : IPowerCostCalculationService
|
||||
public IReadOnlyDictionary<string, CostSnapshot> GetAllDeviceCostSnapshots()
|
||||
{
|
||||
return _deviceAccumulators.ToDictionary(
|
||||
kvp => kvp.Key,
|
||||
kvp => kvp.Value.GetSnapshot());
|
||||
kvp => kvp.Key,
|
||||
kvp => kvp.Value.GetSnapshot());
|
||||
}
|
||||
|
||||
public void SetPrice(double price)
|
||||
@@ -102,13 +109,14 @@ public sealed class PowerCostCalculationService : IPowerCostCalculationService
|
||||
public CostSnapshot GetDeviceCostSnapshot(string entityId)
|
||||
{
|
||||
return _deviceAccumulators.TryGetValue(entityId, out var accumulator)
|
||||
? accumulator.GetSnapshot()
|
||||
: CreateEmptySnapshot();
|
||||
? accumulator.GetSnapshot()
|
||||
: CreateEmptySnapshot();
|
||||
}
|
||||
|
||||
internal static double CalculateCostIncrement(double powerWatts, double pricePerKwh, int pollIntervalSeconds)
|
||||
{
|
||||
var powerKw = powerWatts / PowerMonitorConsts.WattsPerKilowatt;
|
||||
|
||||
return powerKw * pricePerKwh * ((double)pollIntervalSeconds / PowerMonitorConsts.SecondsPerHour);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user