Files
HomeAssistant/PowerMonitor.Manager/Models/CostAccumulator.cs
T
gitea_admin 41ac93733e Clear Qodana findings in monitor runtime code
- Guard logger calls with IsEnabled so value-type arguments are not boxed
  when the level is disabled
- Make helper members static, narrow write-only properties to init or
  private get, and delete members no call site reaches
- Flatten nested conditionals and convert if/else chains into switch and
  ternary form
- Drop the unused *MonitorInit interfaces; NetDaemon instantiates the
  concrete app classes
- Drop the unused IConfiguration parameter from AddNordPool and
  AddPowerMonitor, and the unused IEntityManager.CreateEntitiesAsync
- Remove the ignored parameters of the boiler run-time tracker stub
- Pin the NordPool DTO price mapping explicitly so the getter use is
  visible to static analysis
- Fail Qodana on any finding and record the registration changes in
  AGENTS.md
2026-11-29 21:42:47 +02:00

112 lines
3.0 KiB
C#

namespace PowerMonitorManager.Models;
public sealed class CostAccumulator(DateTime now)
{
private readonly Lock _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 = now;
private DateTime _last1HourReset = now;
private DateTime _lastDailyReset = now;
private DateTime _lastMonthlyReset = now;
private bool _partialWindow = true;
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))
{
return;
}
_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;
}
}