using System.Collections.Concurrent; using PowerMonitorManager.Interface; using PowerMonitorManager.Models; using PowerMonitorManager.Models.Consts; namespace PowerMonitorManager; public sealed class PowerCostCalculationService : IPowerCostCalculationService { private readonly ConcurrentDictionary _deviceAccumulators = new(); private CostAccumulator? _totalAccumulator; private CostAccumulator? _otherAccumulator; private readonly Lock _accumulatorLock = new(); private double _lastKnownPrice; private string _priceSource = "live"; private readonly Lock _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() { lock (_accumulatorLock) { return _totalAccumulator?.GetSnapshot() ?? CreateEmptySnapshot(); } } public CostSnapshot GetOtherCostSnapshot() { lock (_accumulatorLock) { return _otherAccumulator?.GetSnapshot() ?? CreateEmptySnapshot(); } } public CostSnapshot GetDeviceCostSnapshot(string entityId) { return _deviceAccumulators.TryGetValue(entityId, out var accumulator) ? accumulator.GetSnapshot() : CreateEmptySnapshot(); } public IReadOnlyDictionary 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) { 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 }; } }