Apply code formatting cleanup across monitor projects
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
This commit is contained in:
@@ -8,7 +8,8 @@ using NordPoolManager.Models;
|
||||
|
||||
namespace NordPoolManager;
|
||||
|
||||
public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoolEntityService> logger) : INordPoolEntityService
|
||||
public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoolEntityService> logger)
|
||||
: INordPoolEntityService
|
||||
{
|
||||
private const string LastUpdateTimeEntityId = "sensor.nordpool_last_update_time";
|
||||
private const string TodayPricesEntityId = "sensor.nordpool_today_prices";
|
||||
@@ -51,7 +52,7 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
public async Task ResetEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
|
||||
await Task.WhenAll(
|
||||
ResetLastUpdateTimeAsync(),
|
||||
ResetCurrentPriceAsync(),
|
||||
@@ -63,6 +64,59 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task UpdateEntitiesAsync(EntityUpdateContext ctx, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var utcNow = DateTime.UtcNow;
|
||||
var timestamp = utcNow.ToString("O");
|
||||
var updateTimeState = utcNow.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
|
||||
var priceValue = ctx.CurrentPrice.ToString("F2", CultureInfo.InvariantCulture);
|
||||
var todayPricesCount = ctx.TodayNordPoolPrice?.Prices?.Count ?? 0;
|
||||
var tomorrowPricesCount = ctx.TomorrowNordPoolPrice?.Prices?.Count ?? 0;
|
||||
var mergedPrices = MergeTodayTomorrowPrices(ctx.TodayNordPoolPrice, ctx.TomorrowNordPoolPrice);
|
||||
|
||||
if (mergedPrices.Count > NordPoolConsts.MaxExpectedPriceEntries)
|
||||
logger.LogWarning(
|
||||
"Merged price count ({Count}) exceeds expected maximum ({Max}). Attribute payload may be oversized",
|
||||
mergedPrices.Count, NordPoolConsts.MaxExpectedPriceEntries);
|
||||
|
||||
var cheapestCount = ctx.CheapestIntervals?.Count ?? 0;
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
logger.LogInformation(
|
||||
"Updating sensor.nordpool_cheapest_intervals with {Count} intervals, is_now_cheap={IsNow}",
|
||||
cheapestCount, ctx.IsNowCheap);
|
||||
|
||||
var updateTasks = new[]
|
||||
{
|
||||
UpdateLastUpdateTimeAsync(updateTimeState),
|
||||
UpdateCurrentPriceAsync(priceValue, ctx, timestamp),
|
||||
UpdateTodayPricesAsync(ctx, todayPricesCount, timestamp),
|
||||
UpdateTomorrowPricesAsync(ctx, tomorrowPricesCount, timestamp),
|
||||
UpdateTodayTomorrowPricesAsync(mergedPrices, updateTimeState, timestamp),
|
||||
UpdateCheapestIntervalsAsync(ctx, cheapestCount, timestamp)
|
||||
};
|
||||
|
||||
await Task.WhenAll(updateTasks).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
try
|
||||
{
|
||||
var removeTasks = AllEntityIds.Select(entityManager.RemoveEntity);
|
||||
await Task.WhenAll(removeTasks).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error cleaning up NordPool entities");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ResetLastUpdateTimeAsync()
|
||||
{
|
||||
await SetEntityStateSafe(LastUpdateTimeEntityId, "unknown").ConfigureAwait(false);
|
||||
@@ -149,43 +203,6 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task UpdateEntitiesAsync(EntityUpdateContext ctx, CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var utcNow = DateTime.UtcNow;
|
||||
var timestamp = utcNow.ToString("O");
|
||||
var updateTimeState = utcNow.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
|
||||
var priceValue = ctx.CurrentPrice.ToString("F2", CultureInfo.InvariantCulture);
|
||||
var todayPricesCount = ctx.TodayNordPoolPrice?.Prices?.Count ?? 0;
|
||||
var tomorrowPricesCount = ctx.TomorrowNordPoolPrice?.Prices?.Count ?? 0;
|
||||
var mergedPrices = MergeTodayTomorrowPrices(ctx.TodayNordPoolPrice, ctx.TomorrowNordPoolPrice);
|
||||
|
||||
if (mergedPrices.Count > NordPoolConsts.MaxExpectedPriceEntries)
|
||||
{
|
||||
logger.LogWarning("Merged price count ({Count}) exceeds expected maximum ({Max}). Attribute payload may be oversized", mergedPrices.Count, NordPoolConsts.MaxExpectedPriceEntries);
|
||||
}
|
||||
|
||||
var cheapestCount = ctx.CheapestIntervals?.Count ?? 0;
|
||||
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation("Updating sensor.nordpool_cheapest_intervals with {Count} intervals, is_now_cheap={IsNow}", cheapestCount, ctx.IsNowCheap);
|
||||
}
|
||||
|
||||
var updateTasks = new[]
|
||||
{
|
||||
UpdateLastUpdateTimeAsync(updateTimeState),
|
||||
UpdateCurrentPriceAsync(priceValue, ctx, timestamp),
|
||||
UpdateTodayPricesAsync(ctx, todayPricesCount, timestamp),
|
||||
UpdateTomorrowPricesAsync(ctx, tomorrowPricesCount, timestamp),
|
||||
UpdateTodayTomorrowPricesAsync(mergedPrices, updateTimeState, timestamp),
|
||||
UpdateCheapestIntervalsAsync(ctx, cheapestCount, timestamp)
|
||||
};
|
||||
|
||||
await Task.WhenAll(updateTasks).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task UpdateLastUpdateTimeAsync(string updateTimeState)
|
||||
{
|
||||
await SetEntityStateSafe(LastUpdateTimeEntityId, updateTimeState).ConfigureAwait(false);
|
||||
@@ -229,7 +246,8 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task UpdateTodayTomorrowPricesAsync(List<NordPoolDayPrice> mergedPrices, string updateTimeState, string timestamp)
|
||||
private async Task UpdateTodayTomorrowPricesAsync(List<NordPoolDayPrice> mergedPrices, string updateTimeState,
|
||||
string timestamp)
|
||||
{
|
||||
await SetEntityStateSafe(TodayTomorrowPricesEntityId, updateTimeState).ConfigureAwait(false);
|
||||
await SetEntityAttributesSafe(TodayTomorrowPricesEntityId, new
|
||||
@@ -260,22 +278,6 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
try
|
||||
{
|
||||
var removeTasks = AllEntityIds.Select(entityManager.RemoveEntity);
|
||||
await Task.WhenAll(removeTasks).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error cleaning up NordPool entities");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
internal static ICollection<NordPoolEntity> GetInitEntitiesList(string? unitOfMeasurement)
|
||||
{
|
||||
const string currentPriceEntityId = NordPoolConsts.CurrentPriceEntityId;
|
||||
@@ -344,4 +346,4 @@ public sealed class NordPoolEntityService(IEntityManager entityManager, ILogger<
|
||||
logger.LogError(ex, "Failed to set attributes for {EntityId}", entityId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user