Files
HomeAssistant/PowerMonitor.Manager/PowerCostCalculationService.cs
gitea_admin f919d6b25b 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
2026-12-01 12:33:31 +02:00

143 lines
4.1 KiB
C#

#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 Lock();
private readonly ConcurrentDictionary<string, CostAccumulator> _deviceAccumulators = new ConcurrentDictionary<string, CostAccumulator>();
private readonly Lock _priceLock = new Lock();
private double _lastKnownPrice;
private CostAccumulator? _otherAccumulator;
private string _priceSource = "live";
private CostAccumulator? _totalAccumulator;
public double LastKnownPrice
{
get
{
lock (_priceLock)
{
return _lastKnownPrice;
}
}
}
public string PriceSource
{
get
{
lock (_priceLock)
{
return _priceSource;
}
}
}
public void RecordCosts(PowerCalculationResult calculation,
double nordPoolPrice,
int pollIntervalSeconds,
DateTime now)
{
lock (_priceLock)
{
if (nordPoolPrice > 0)
{
_lastKnownPrice = nordPoolPrice;
_priceSource = "live";
}
}
var effectivePrice = nordPoolPrice > 0 ? nordPoolPrice : LastKnownPrice;
var totalCost = CalculateCostIncrement(calculation.TotalPowerWatts, effectivePrice, pollIntervalSeconds);
GetOrCreateTotalAccumulator(now).AddCost(totalCost, now);
var otherCost =
CalculateCostIncrement(calculation.SmoothedOtherPowerWatts, effectivePrice, pollIntervalSeconds);
GetOrCreateOtherAccumulator(now).AddCost(otherCost, now);
foreach (var (entityId, powerWatts) in calculation.DevicePowerReadings)
{
var deviceCost = CalculateCostIncrement(powerWatts, effectivePrice, pollIntervalSeconds);
var accumulator = _deviceAccumulators.GetOrAdd(entityId, _ => new CostAccumulator(now));
accumulator.AddCost(deviceCost, now);
}
}
public CostSnapshot GetTotalCostSnapshot()
{
lock (_accumulatorLock)
{
return _totalAccumulator?.GetSnapshot() ?? CreateEmptySnapshot();
}
}
public CostSnapshot GetOtherCostSnapshot()
{
lock (_accumulatorLock)
{
return _otherAccumulator?.GetSnapshot() ?? CreateEmptySnapshot();
}
}
public IReadOnlyDictionary<string, CostSnapshot> GetAllDeviceCostSnapshots()
{
return _deviceAccumulators.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value.GetSnapshot());
}
public void SetPrice(double price)
{
lock (_priceLock)
{
_lastKnownPrice = price;
_priceSource = "stale";
}
}
public CostSnapshot GetDeviceCostSnapshot(string entityId)
{
return _deviceAccumulators.TryGetValue(entityId, out var accumulator)
? 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);
}
private CostAccumulator GetOrCreateTotalAccumulator(DateTime now)
{
lock (_accumulatorLock)
{
return _totalAccumulator ??= new CostAccumulator(now);
}
}
private CostAccumulator GetOrCreateOtherAccumulator(DateTime now)
{
lock (_accumulatorLock)
{
return _otherAccumulator ??= new CostAccumulator(now);
}
}
private static CostSnapshot CreateEmptySnapshot()
{
return new CostSnapshot { PartialWindow = true };
}
}