Refactor boiler control and price logic: Introduce PriceConsts, improve BoilerAction handling with switch, and update namespaces for better structure.

This commit is contained in:
Marius
2026-03-16 12:08:14 +02:00
parent 42b1fe3b18
commit 17a2e67078
8 changed files with 34 additions and 19 deletions
@@ -2,7 +2,7 @@ using FluentAssertions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Moq; using Moq;
using NordPoolManager; using NordPoolManager;
using NordPoolManager.Models; using NordPoolManager.Models.Enums;
namespace NordPool.Manager.Tests; namespace NordPool.Manager.Tests;
+1 -1
View File
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NordPoolManager.Interface; using NordPoolManager.Interface;
using NordPoolManager.Models; using NordPoolManager.Models.Enums;
namespace NordPoolManager; namespace NordPoolManager;
@@ -1,4 +1,4 @@
using NordPoolManager.Models; using NordPoolManager.Models.Enums;
namespace NordPoolManager.Interface; namespace NordPoolManager.Interface;
@@ -1,4 +1,4 @@
namespace NordPoolManager.Models; namespace NordPoolManager.Models.Consts;
public static class ConversionConsts public static class ConversionConsts
{ {
@@ -0,0 +1,7 @@
namespace NordPoolManager.Models.Consts;
public static class PriceConsts
{
public const int MaxDailyIntervals = 96;
public const int IntervalsPerHour = 4;
}
@@ -1,4 +1,4 @@
namespace NordPoolManager.Models; namespace NordPoolManager.Models.Enums;
public enum BoilerAction public enum BoilerAction
{ {
+17 -8
View File
@@ -7,7 +7,8 @@ using NetDaemon.Extensions.Scheduler;
using NordPool.Models; using NordPool.Models;
using NordPoolManager.Interface; using NordPoolManager.Interface;
using NordPoolManager.Models; using NordPoolManager.Models;
using static NordPoolManager.Models.BoilerAction; using NordPoolManager.Models.Consts;
using static NordPoolManager.Models.Enums.BoilerAction;
namespace NordPoolManager; namespace NordPoolManager;
@@ -239,7 +240,7 @@ public class NordPoolInit : INordPoolInit
{ {
var boilerConfig = ReadBoilerConfig(); var boilerConfig = ReadBoilerConfig();
var effectivePriceCap = boilerConfig.SkipPriceCap ? double.MaxValue : boilerConfig.PriceCap; var effectivePriceCap = boilerConfig.SkipPriceCap ? double.MaxValue : boilerConfig.PriceCap;
var effectiveIntervalCount = boilerConfig.SkipPriceCap ? 96 : boilerConfig.IntervalCount; var effectiveIntervalCount = boilerConfig.SkipPriceCap ? PriceConsts.MaxDailyIntervals : boilerConfig.IntervalCount;
try try
{ {
@@ -279,11 +280,12 @@ public class NordPoolInit : INordPoolInit
var boilerConfig = new BoilerConfig(); var boilerConfig = new BoilerConfig();
var dailyHours = BoilerDailyHoursEntity.State ?? 1.0; var dailyHours = BoilerDailyHoursEntity.State ?? 1.0;
var intervalCount = (int)(dailyHours * 4);
var intervalCount = (int)(dailyHours * PriceConsts.IntervalsPerHour);
if (intervalCount <= 0) if (intervalCount <= 0)
{ {
intervalCount = 4; intervalCount = PriceConsts.IntervalsPerHour;
} }
boilerConfig.IntervalCount = intervalCount; boilerConfig.IntervalCount = intervalCount;
@@ -348,13 +350,20 @@ public class NordPoolInit : INordPoolInit
var boilerAction = _boilerControlService.DetermineAction(isNowCheap, waterReady, boilerSwitchState); var boilerAction = _boilerControlService.DetermineAction(isNowCheap, waterReady, boilerSwitchState);
if (boilerAction == TurnOn) switch (boilerAction)
{ {
case TurnOn:
BoilerSwitchEntity.TurnOn(); BoilerSwitchEntity.TurnOn();
}
else if (boilerAction == TurnOff) break;
{ case TurnOff:
BoilerSwitchEntity.TurnOff(); BoilerSwitchEntity.TurnOff();
break;
case NoChange:
break;
default:
throw new ArgumentOutOfRangeException();
} }
var context = new EntityUpdateContext var context = new EntityUpdateContext
+3 -4
View File
@@ -5,7 +5,7 @@ using Microsoft.Extensions.Logging;
using NordPool.Models; using NordPool.Models;
using NordPool.Models.Dtos; using NordPool.Models.Dtos;
using NordPoolManager.Interface; using NordPoolManager.Interface;
using NordPoolManager.Models; using NordPoolManager.Models.Consts;
namespace NordPoolManager; namespace NordPoolManager;
@@ -49,8 +49,7 @@ public class NordPoolPriceService(Services services, IMapper mapper, ILogger<Nor
LastUpdatedDate = DateTime.UtcNow; LastUpdatedDate = DateTime.UtcNow;
LastFetchedDate = today; LastFetchedDate = today;
logger.LogInformation( logger.LogInformation("Prices ready for today ({Today}). Today prices count: {TodayCount}, Tomorrow prices count: {TomorrowCount}",
"Prices ready for today ({Today}). Today prices count: {TodayCount}, Tomorrow prices count: {TomorrowCount}",
today, TodayNordPoolPrice?.Prices?.Count ?? 0, TomorrowNordPoolPrice?.Prices?.Count ?? 0); today, TodayNordPoolPrice?.Prices?.Count ?? 0, TomorrowNordPoolPrice?.Prices?.Count ?? 0);
} }
@@ -149,7 +148,7 @@ public class NordPoolPriceService(Services services, IMapper mapper, ILogger<Nor
LastFetchedDate = today; LastFetchedDate = today;
} }
internal async Task<NordPoolPrice> GetNordPoolPriceAsync(DateOnly date) private async Task<NordPoolPrice> GetNordPoolPriceAsync(DateOnly date)
{ {
var nordPoolPricesDto = await _nordPoolServices.GetPricesForDateAsync( var nordPoolPricesDto = await _nordPoolServices.GetPricesForDateAsync(
new NordpoolGetPricesForDateParameters new NordpoolGetPricesForDateParameters