feat: add PowerCostCalculationService with per-device cost accumulators
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
using PowerMonitorManager.Models;
|
||||
|
||||
namespace PowerMonitorManager.Interface;
|
||||
|
||||
public interface IPowerCostCalculationService
|
||||
{
|
||||
void RecordCosts(PowerCalculationResult calculation, double nordPoolPrice, int pollIntervalSeconds, DateTime now);
|
||||
CostSnapshot GetTotalCostSnapshot();
|
||||
CostSnapshot GetOtherCostSnapshot();
|
||||
CostSnapshot GetDeviceCostSnapshot(string entityId);
|
||||
IReadOnlyDictionary<string, CostSnapshot> GetAllDeviceCostSnapshots();
|
||||
double LastKnownPrice { get; }
|
||||
string PriceSource { get; }
|
||||
void SetPrice(double price);
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using System.Collections.Concurrent;
|
||||
using PowerMonitorManager.Interface;
|
||||
using PowerMonitorManager.Models;
|
||||
using PowerMonitorManager.Models.Consts;
|
||||
|
||||
namespace PowerMonitorManager;
|
||||
|
||||
public sealed class PowerCostCalculationService : IPowerCostCalculationService
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, CostAccumulator> _deviceAccumulators = new();
|
||||
private CostAccumulator? _totalAccumulator;
|
||||
private CostAccumulator? _otherAccumulator;
|
||||
|
||||
private double _lastKnownPrice;
|
||||
private string _priceSource = "live";
|
||||
private readonly object _priceLock = new();
|
||||
|
||||
public double LastKnownPrice
|
||||
{
|
||||
get { lock (_priceLock) { return _lastKnownPrice; } }
|
||||
}
|
||||
|
||||
public string PriceSource
|
||||
{
|
||||
get { lock (_priceLock) { return _priceSource; } }
|
||||
}
|
||||
|
||||
public void SetPrice(double price)
|
||||
{
|
||||
lock (_priceLock)
|
||||
{
|
||||
_lastKnownPrice = price;
|
||||
_priceSource = "stale";
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
return _totalAccumulator?.GetSnapshot() ?? CreateEmptySnapshot();
|
||||
}
|
||||
|
||||
public CostSnapshot GetOtherCostSnapshot()
|
||||
{
|
||||
return _otherAccumulator?.GetSnapshot() ?? CreateEmptySnapshot();
|
||||
}
|
||||
|
||||
public CostSnapshot GetDeviceCostSnapshot(string entityId)
|
||||
{
|
||||
return _deviceAccumulators.TryGetValue(entityId, out var accumulator)
|
||||
? accumulator.GetSnapshot()
|
||||
: CreateEmptySnapshot();
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<string, CostSnapshot> GetAllDeviceCostSnapshots()
|
||||
{
|
||||
return _deviceAccumulators.ToDictionary(
|
||||
kvp => kvp.Key,
|
||||
kvp => kvp.Value.GetSnapshot());
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return _totalAccumulator ??= new CostAccumulator(now);
|
||||
}
|
||||
|
||||
private CostAccumulator GetOrCreateOtherAccumulator(DateTime now)
|
||||
{
|
||||
return _otherAccumulator ??= new CostAccumulator(now);
|
||||
}
|
||||
|
||||
private static CostSnapshot CreateEmptySnapshot()
|
||||
{
|
||||
return new CostSnapshot { PartialWindow = true };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user