diff --git a/PowerMonitor.Manager/Models/CostAccumulator.cs b/PowerMonitor.Manager/Models/CostAccumulator.cs new file mode 100644 index 0000000..7e17d2a --- /dev/null +++ b/PowerMonitor.Manager/Models/CostAccumulator.cs @@ -0,0 +1,117 @@ +namespace PowerMonitorManager.Models; + +public sealed class CostAccumulator +{ + private readonly object _lock = new(); + + private double _cost15Min; + private double _cost1Hour; + private double _costDaily; + private double _costMonthly; + + private double _previous15Min; + private double _previous1Hour; + private double _previousDaily; + private double _previousMonthly; + + private DateTime _last15MinReset; + private DateTime _last1HourReset; + private DateTime _lastDailyReset; + private DateTime _lastMonthlyReset; + + private bool _partialWindow = true; + + public CostAccumulator(DateTime now) + { + _last15MinReset = now; + _last1HourReset = now; + _lastDailyReset = now; + _lastMonthlyReset = now; + } + + public void AddCost(double costIncrement, DateTime now) + { + lock (_lock) + { + CheckAndResetWindows(now); + + _cost15Min += costIncrement; + _cost1Hour += costIncrement; + _costDaily += costIncrement; + _costMonthly += costIncrement; + } + } + + public CostSnapshot GetSnapshot() + { + lock (_lock) + { + return new CostSnapshot + { + Cost15Min = _cost15Min, + Cost1Hour = _cost1Hour, + CostDaily = _costDaily, + CostMonthly = _costMonthly, + Previous15Min = _previous15Min, + Previous1Hour = _previous1Hour, + PreviousDaily = _previousDaily, + PreviousMonthly = _previousMonthly, + PartialWindow = _partialWindow + }; + } + } + + private void CheckAndResetWindows(DateTime now) + { + if (IsNew15MinWindow(now, _last15MinReset)) + { + _previous15Min = _cost15Min; + _cost15Min = 0; + _last15MinReset = now; + _partialWindow = false; + } + + if (IsNewHourWindow(now, _last1HourReset)) + { + _previous1Hour = _cost1Hour; + _cost1Hour = 0; + _last1HourReset = now; + } + + if (IsNewDayWindow(now, _lastDailyReset)) + { + _previousDaily = _costDaily; + _costDaily = 0; + _lastDailyReset = now; + } + + if (IsNewMonthWindow(now, _lastMonthlyReset)) + { + _previousMonthly = _costMonthly; + _costMonthly = 0; + _lastMonthlyReset = now; + } + } + + internal static bool IsNew15MinWindow(DateTime now, DateTime lastReset) + { + var currentSlot = now.Minute / 15; + var lastSlot = lastReset.Minute / 15; + return now.Date != lastReset.Date || now.Hour != lastReset.Hour || currentSlot != lastSlot; + } + + internal static bool IsNewHourWindow(DateTime now, DateTime lastReset) + { + return now.Date != lastReset.Date || now.Hour != lastReset.Hour; + } + + internal static bool IsNewDayWindow(DateTime now, DateTime lastReset) + { + return now.Date != lastReset.Date; + } + + internal static bool IsNewMonthWindow(DateTime now, DateTime lastReset) + { + return now.Year != lastReset.Year || now.Month != lastReset.Month; + } +} diff --git a/PowerMonitor.Manager/Models/CostSnapshot.cs b/PowerMonitor.Manager/Models/CostSnapshot.cs new file mode 100644 index 0000000..b4d6d1d --- /dev/null +++ b/PowerMonitor.Manager/Models/CostSnapshot.cs @@ -0,0 +1,14 @@ +namespace PowerMonitorManager.Models; + +public sealed class CostSnapshot +{ + public double Cost15Min { get; init; } + public double Cost1Hour { get; init; } + public double CostDaily { get; init; } + public double CostMonthly { get; init; } + public double Previous15Min { get; init; } + public double Previous1Hour { get; init; } + public double PreviousDaily { get; init; } + public double PreviousMonthly { get; init; } + public bool PartialWindow { get; init; } +} diff --git a/PowerMonitor.Manager/Models/PowerCalculationResult.cs b/PowerMonitor.Manager/Models/PowerCalculationResult.cs new file mode 100644 index 0000000..4d9b6e3 --- /dev/null +++ b/PowerMonitor.Manager/Models/PowerCalculationResult.cs @@ -0,0 +1,11 @@ +namespace PowerMonitorManager.Models; + +public sealed class PowerCalculationResult +{ + public double TotalPowerWatts { get; init; } + public double KnownDevicesSumWatts { get; init; } + public double RawOtherPowerWatts { get; init; } + public double SmoothedOtherPowerWatts { get; init; } + public string DataQuality { get; init; } = "ok"; + public IReadOnlyDictionary DevicePowerReadings { get; init; } = new Dictionary(); +} diff --git a/PowerMonitor.Manager/Models/PowerUpdateContext.cs b/PowerMonitor.Manager/Models/PowerUpdateContext.cs new file mode 100644 index 0000000..dd43388 --- /dev/null +++ b/PowerMonitor.Manager/Models/PowerUpdateContext.cs @@ -0,0 +1,18 @@ +namespace PowerMonitorManager.Models; + +public sealed class PowerUpdateContext +{ + public required PowerCalculationResult Calculation { get; init; } + public required double CurrentNordPoolPrice { get; init; } + public required string PriceUnit { get; init; } + public required string PriceSource { get; init; } + public required IReadOnlyDictionary DeviceCosts { get; init; } + public required CostSnapshot TotalCost { get; init; } + public required CostSnapshot OtherCost { get; init; } + public required bool IsAnomaly { get; init; } + public required TimeSpan AnomalyDuration { get; init; } + public required double AnomalyThresholdWatts { get; init; } + public required int TrackedSensorCount { get; init; } + public required IReadOnlyList DegradedSensors { get; init; } + public required string SystemStatus { get; init; } +} diff --git a/PowerMonitor.Models/PowerMonitorEntity.cs b/PowerMonitor.Models/PowerMonitorEntity.cs new file mode 100644 index 0000000..18e86d8 --- /dev/null +++ b/PowerMonitor.Models/PowerMonitorEntity.cs @@ -0,0 +1,25 @@ +using HomeAssistant.Models; +using NetDaemon.Extensions.MqttEntityManager; + +namespace PowerMonitor.Models; + +public class PowerMonitorEntity : EntityCreateModel +{ + public string Domain { get; set; } + public string Identifier { get; set; } + public string Name { get; set; } + + public PowerMonitorEntity(string domain, string identifier, string name) + { + Domain = domain; + Identifier = identifier; + EntityId = $"{Domain}.{Identifier}"; + Name = string.IsNullOrWhiteSpace(name) ? identifier : name; + + Options = new EntityCreationOptions + { + UniqueId = EntityId, + Name = Name + }; + } +} diff --git a/PowerMonitor.Models/PowerSensorInfo.cs b/PowerMonitor.Models/PowerSensorInfo.cs new file mode 100644 index 0000000..ccd1fd0 --- /dev/null +++ b/PowerMonitor.Models/PowerSensorInfo.cs @@ -0,0 +1,9 @@ +namespace PowerMonitor.Models; + +public sealed class PowerSensorInfo +{ + public required string EntityId { get; init; } + public required string FriendlyName { get; init; } + public required string DeviceName { get; init; } + public string UnitOfMeasurement { get; init; } = "W"; +}