f919d6b25b
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests - Expand single-statement guards and loops into braced blocks - Realign wrapped method arguments, chained calls and continuations - Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty` - Flatten the boiler config expression and reindent the remaining touched files
69 lines
2.2 KiB
C#
69 lines
2.2 KiB
C#
#region
|
|
|
|
using System.Globalization;
|
|
using BoilerMonitor.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using NetDaemon.HassModel;
|
|
using static BoilerMonitor.Models.BoilerMonitorConsts;
|
|
using static NordPool.Models.NordPoolConsts;
|
|
|
|
#endregion
|
|
|
|
namespace BoilerMonitorManager;
|
|
|
|
public sealed class BoilerConfigReader(IHaContext haContext, ILogger<BoilerConfigReader> logger) : IBoilerConfigReader
|
|
{
|
|
private const double DefaultDailyHours = 1.0;
|
|
|
|
public BoilerConfig ReadConfig()
|
|
{
|
|
var dailyHoursState = haContext.GetState(BoilerDailyHoursEntityId)?.State;
|
|
|
|
var dailyHours = double.TryParse(dailyHoursState, NumberStyles.Any, CultureInfo.InvariantCulture, out var dh)
|
|
? dh
|
|
: DefaultDailyHours;
|
|
|
|
var intervalCount = (int)(dailyHours * IntervalsPerHour);
|
|
|
|
if (intervalCount <= 0)
|
|
{
|
|
intervalCount = IntervalsPerHour;
|
|
}
|
|
|
|
var priceCapState = haContext.GetState(BoilerPriceCapEntityId)?.State;
|
|
|
|
var priceCap =
|
|
double.TryParse(priceCapState, NumberStyles.Any, CultureInfo.InvariantCulture, out var pc) && pc > 0
|
|
? pc
|
|
: double.MaxValue;
|
|
|
|
var deadline = DateTime.Today.AddDays(1);
|
|
var readyByState = haContext.GetState(BoilerReadyByEntityId)?.State;
|
|
|
|
if (!string.IsNullOrEmpty(readyByState) &&
|
|
TimeSpan.TryParse(readyByState, CultureInfo.InvariantCulture, out var readyByTime))
|
|
{
|
|
deadline = DateTime.Today.Add(readyByTime);
|
|
}
|
|
|
|
var skipPriceCapState = haContext.GetState(SkipPriceCapEntityId)?.State;
|
|
var skipPriceCap = string.Equals(skipPriceCapState, SwitchOnState, StringComparison.OrdinalIgnoreCase);
|
|
|
|
if (logger.IsEnabled(LogLevel.Debug))
|
|
{
|
|
logger.LogDebug(
|
|
"Boiler config: intervals={Intervals}, priceCap={Cap}, skipPriceCap={Skip}, deadline={Deadline}",
|
|
intervalCount, priceCap, skipPriceCap, deadline);
|
|
}
|
|
|
|
var config = new BoilerConfig
|
|
{
|
|
IntervalCount = intervalCount,
|
|
PriceCap = priceCap,
|
|
Deadline = deadline,
|
|
SkipPriceCap = skipPriceCap
|
|
};
|
|
|
|
return config;
|
|
}
|
|
} |