Remove the MCP server and split NordPool today/tomorrow fetch decisions
- Drop the HomeAssistant.McpServer wiring from opencode.json along with the Cursor live-validation hook, rule and skill; update AGENTS.md and README - Rename ShouldFetchNewPrices to ShouldFetchTodaysPrices and add a separate ShouldFetchTomorrowPrice that waits until the CET publish hour (TomorrowPricesPublishHour) - Inject TimeProvider into NordPoolPriceService, use the CET zone for the current date and record fetch timestamps from it - Rework the NordPool tests around FakeTimeProvider and cover fetching only today before the publish hour
This commit is contained in:
@@ -17,8 +17,10 @@ namespace NordPoolManager;
|
||||
/// <c>IHaContext</c>/generated entity dependencies so it can be a singleton.
|
||||
/// Fetching prices from Home Assistant lives in <see cref="NordPoolInit" />.
|
||||
/// </summary>
|
||||
public class NordPoolPriceService(IMapper mapper, ILogger<NordPoolPriceService> logger) : INordPoolPriceService
|
||||
public class NordPoolPriceService(IMapper mapper, ILogger<NordPoolPriceService> logger, TimeProvider timeProvider) : INordPoolPriceService
|
||||
{
|
||||
private static readonly TimeZoneInfo CetTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
|
||||
public NordPoolPrice? TodayNordPoolPrice { get; internal set; }
|
||||
public NordPoolPrice? TomorrowNordPoolPrice { get; internal set; }
|
||||
public double CurrentPrice { get; internal set; }
|
||||
@@ -28,11 +30,10 @@ public class NordPoolPriceService(IMapper mapper, ILogger<NordPoolPriceService>
|
||||
|
||||
public async Task FetchPricesAsync(Func<DateOnly, Task<NordPoolPrice>> fetchPricesForDate)
|
||||
{
|
||||
var cetTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
var nowCet = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cetTimeZone);
|
||||
var dateNow = DateOnly.FromDateTime(nowCet);
|
||||
var nowCet = TimeZoneInfo.ConvertTime(timeProvider.GetUtcNow(), CetTimeZone);
|
||||
var dateNow = DateOnly.FromDateTime(nowCet.DateTime);
|
||||
|
||||
if ((TodayNordPoolPrice?.Prices == null || TodayNordPoolPrice.Prices.Count == 0) && ShouldFetchNewPrices(dateNow))
|
||||
if ((TodayNordPoolPrice?.Prices == null || TodayNordPoolPrice.Prices.Count == 0) && ShouldFetchTodaysPrices(dateNow))
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
@@ -42,7 +43,7 @@ public class NordPoolPriceService(IMapper mapper, ILogger<NordPoolPriceService>
|
||||
TodayNordPoolPrice = await FetchPricesForDateWithRetryAsync(dateNow, fetchPricesForDate);
|
||||
}
|
||||
|
||||
if ((TomorrowNordPoolPrice?.Prices == null || TomorrowNordPoolPrice.Prices.Count == 0) && ShouldFetchNewPrices(dateNow.AddDays(1)))
|
||||
if ((TomorrowNordPoolPrice?.Prices == null || TomorrowNordPoolPrice.Prices.Count == 0) && ShouldFetchTomorrowPrice())
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
@@ -52,13 +53,13 @@ public class NordPoolPriceService(IMapper mapper, ILogger<NordPoolPriceService>
|
||||
TomorrowNordPoolPrice = await FetchPricesForDateWithRetryAsync(dateNow.AddDays(1), fetchPricesForDate);
|
||||
}
|
||||
|
||||
LastUpdatedDate = DateTime.UtcNow;
|
||||
LastUpdatedDate = timeProvider.GetUtcNow().UtcDateTime;
|
||||
LastFetchedDate = dateNow;
|
||||
}
|
||||
|
||||
public void UpdateCurrentPrice()
|
||||
{
|
||||
var index = GetTimeQuarterIndex(DateTime.UtcNow);
|
||||
var index = GetTimeQuarterIndex(timeProvider.GetUtcNow().UtcDateTime);
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
@@ -96,31 +97,19 @@ public class NordPoolPriceService(IMapper mapper, ILogger<NordPoolPriceService>
|
||||
return CurrentPrice;
|
||||
}
|
||||
|
||||
public bool ShouldFetchNewPrices(DateOnly date)
|
||||
public bool ShouldFetchTodaysPrices(DateOnly todayDate)
|
||||
{
|
||||
if (!LastFetchedDate.HasValue)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (LastFetchedDate.Value < date)
|
||||
if (LastFetchedDate.Value < todayDate)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (TodayNordPoolPrice?.Prices == null || TodayNordPoolPrice.Prices.Count == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (TomorrowNordPoolPrice?.Prices is { Count: > 0 })
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var currentHour = DateTime.Now.Hour;
|
||||
|
||||
return currentHour >= 13;
|
||||
return TodayNordPoolPrice?.Prices is not { Count: > 0 };
|
||||
}
|
||||
|
||||
public void RollOverPrices(DateOnly date)
|
||||
@@ -231,4 +220,16 @@ public class NordPoolPriceService(IMapper mapper, ILogger<NordPoolPriceService>
|
||||
|
||||
return new NordPoolPrice();
|
||||
}
|
||||
}
|
||||
|
||||
private bool ShouldFetchTomorrowPrice()
|
||||
{
|
||||
var cetNow = TimeZoneInfo.ConvertTime(timeProvider.GetUtcNow(), CetTimeZone).DateTime;
|
||||
|
||||
if (cetNow.Hour < NordPoolConsts.TomorrowPricesPublishHour)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return TomorrowNordPoolPrice?.Prices is not { Count: > 0 };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user