Apply code formatting cleanup across the monitor projects
- 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
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#region
|
||||
|
||||
using System.Globalization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
@@ -7,6 +9,8 @@ using PowerMonitorManager.Interface;
|
||||
using PowerMonitorManager.Models;
|
||||
using PowerMonitorManager.Models.Consts;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace PowerMonitorManager;
|
||||
|
||||
public sealed class PowerLimitEnforcementService(
|
||||
@@ -25,13 +29,17 @@ public sealed class PowerLimitEnforcementService(
|
||||
|
||||
public void EnforceLimit(double currentPowerWatts)
|
||||
{
|
||||
if (!_options.EnforceLimitEnabled) return;
|
||||
if (!_options.EnforceLimitEnabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var now = timeProvider.GetUtcNow();
|
||||
|
||||
if (!IsLimitActive)
|
||||
{
|
||||
TryEnforce(currentPowerWatts, now);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -43,6 +51,7 @@ public sealed class PowerLimitEnforcementService(
|
||||
if (IsLimitActive)
|
||||
{
|
||||
logger.LogInformation("Debug: trigger requested but limit already active");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -55,6 +64,7 @@ public sealed class PowerLimitEnforcementService(
|
||||
if (!IsLimitActive)
|
||||
{
|
||||
logger.LogInformation("Debug: reset requested but limit not active");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -64,9 +74,15 @@ public sealed class PowerLimitEnforcementService(
|
||||
|
||||
private void ExecuteEnforce(double currentPowerWatts)
|
||||
{
|
||||
foreach (var entityId in _options.EnforceSwitchOffEntityIds) TurnOff(entityId);
|
||||
foreach (var entityId in _options.EnforceSwitchOffEntityIds)
|
||||
{
|
||||
TurnOff(entityId);
|
||||
}
|
||||
|
||||
foreach (var entityId in _options.PauseAndRestoreEntityIds) TurnOff(entityId);
|
||||
foreach (var entityId in _options.PauseAndRestoreEntityIds)
|
||||
{
|
||||
TurnOff(entityId);
|
||||
}
|
||||
|
||||
NotifyLimitEnforced(currentPowerWatts, _options.PowerLimitWatts);
|
||||
|
||||
@@ -78,7 +94,10 @@ public sealed class PowerLimitEnforcementService(
|
||||
|
||||
private void ExecuteRecover()
|
||||
{
|
||||
foreach (var entityId in _options.PauseAndRestoreEntityIds) TurnOn(entityId);
|
||||
foreach (var entityId in _options.PauseAndRestoreEntityIds)
|
||||
{
|
||||
TurnOn(entityId);
|
||||
}
|
||||
|
||||
DismissLimitNotification();
|
||||
|
||||
@@ -88,22 +107,25 @@ public sealed class PowerLimitEnforcementService(
|
||||
|
||||
private void TryEnforce(double currentPowerWatts, DateTimeOffset now)
|
||||
{
|
||||
if (currentPowerWatts <= _options.PowerLimitWatts) return;
|
||||
if (currentPowerWatts <= _options.PowerLimitWatts)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (now - _lastActionTime < TimeSpan.FromSeconds(_options.LimitCooldownSeconds))
|
||||
{
|
||||
if (logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
logger.LogDebug(
|
||||
"Power limit breach detected ({Power:F0} W > {Limit:F0} W) but cooldown active, skipping",
|
||||
currentPowerWatts, _options.PowerLimitWatts);
|
||||
"Power limit breach detected ({Power:F0} W > {Limit:F0} W) but cooldown active, skipping",
|
||||
currentPowerWatts, _options.PowerLimitWatts);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
logger.LogWarning("Power limit exceeded: {Power:F0} W > {Limit:F0} W, enforcing turn-off", currentPowerWatts,
|
||||
_options.PowerLimitWatts);
|
||||
_options.PowerLimitWatts);
|
||||
|
||||
ExecuteEnforce(currentPowerWatts);
|
||||
}
|
||||
@@ -112,12 +134,16 @@ public sealed class PowerLimitEnforcementService(
|
||||
{
|
||||
var elapsed = now - _enforcedAt;
|
||||
|
||||
if (elapsed < TimeSpan.FromSeconds(_options.RecoverySeconds)) return;
|
||||
if (elapsed < TimeSpan.FromSeconds(_options.RecoverySeconds))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentPowerWatts >= _options.PowerLimitWatts)
|
||||
{
|
||||
logger.LogWarning("Recovery time reached but power still {Power:F0} W >= {Limit:F0} W, staying enforced",
|
||||
currentPowerWatts, _options.PowerLimitWatts);
|
||||
currentPowerWatts, _options.PowerLimitWatts);
|
||||
|
||||
_enforcedAt = now;
|
||||
|
||||
return;
|
||||
@@ -126,11 +152,14 @@ public sealed class PowerLimitEnforcementService(
|
||||
if (logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
logger.LogInformation(
|
||||
"Power recovered to {Power:F0} W (< {Limit:F0} W) after {Seconds}s, restoring paused monitors",
|
||||
currentPowerWatts, _options.PowerLimitWatts, (int)elapsed.TotalSeconds);
|
||||
"Power recovered to {Power:F0} W (< {Limit:F0} W) after {Seconds}s, restoring paused monitors",
|
||||
currentPowerWatts, _options.PowerLimitWatts, (int)elapsed.TotalSeconds);
|
||||
}
|
||||
|
||||
foreach (var entityId in _options.PauseAndRestoreEntityIds) TurnOn(entityId);
|
||||
foreach (var entityId in _options.PauseAndRestoreEntityIds)
|
||||
{
|
||||
TurnOn(entityId);
|
||||
}
|
||||
|
||||
DismissLimitNotification();
|
||||
|
||||
@@ -141,46 +170,46 @@ public sealed class PowerLimitEnforcementService(
|
||||
private void TurnOff(string entityId)
|
||||
{
|
||||
haContext.CallService(PowerMonitorConsts.SwitchDomain, PowerMonitorConsts.TurnOffService,
|
||||
ServiceTarget.FromEntity(entityId));
|
||||
ServiceTarget.FromEntity(entityId));
|
||||
}
|
||||
|
||||
private void TurnOn(string entityId)
|
||||
{
|
||||
haContext.CallService(PowerMonitorConsts.SwitchDomain, PowerMonitorConsts.TurnOnService,
|
||||
ServiceTarget.FromEntity(entityId));
|
||||
ServiceTarget.FromEntity(entityId));
|
||||
}
|
||||
|
||||
private void NotifyLimitEnforced(double currentPowerWatts, double limitWatts)
|
||||
{
|
||||
var message = BuildLimitMessage(currentPowerWatts / PowerMonitorConsts.WattsPerKilowatt,
|
||||
limitWatts / PowerMonitorConsts.WattsPerKilowatt);
|
||||
limitWatts / PowerMonitorConsts.WattsPerKilowatt);
|
||||
|
||||
haContext.CallService(
|
||||
PowerMonitorConsts.PersistentNotificationDomain,
|
||||
PowerMonitorConsts.PersistentNotificationCreateService,
|
||||
data: new
|
||||
{
|
||||
title = PowerMonitorConsts.PowerLimitNotificationTitle,
|
||||
message,
|
||||
notification_id = PowerMonitorConsts.PowerLimitNotificationId
|
||||
});
|
||||
PowerMonitorConsts.PersistentNotificationDomain,
|
||||
PowerMonitorConsts.PersistentNotificationCreateService,
|
||||
data: new
|
||||
{
|
||||
title = PowerMonitorConsts.PowerLimitNotificationTitle,
|
||||
message,
|
||||
notification_id = PowerMonitorConsts.PowerLimitNotificationId
|
||||
});
|
||||
}
|
||||
|
||||
private void DismissLimitNotification()
|
||||
{
|
||||
haContext.CallService(
|
||||
PowerMonitorConsts.PersistentNotificationDomain,
|
||||
PowerMonitorConsts.PersistentNotificationDismissService,
|
||||
data: new
|
||||
{
|
||||
notification_id = PowerMonitorConsts.PowerLimitNotificationId
|
||||
});
|
||||
PowerMonitorConsts.PersistentNotificationDomain,
|
||||
PowerMonitorConsts.PersistentNotificationDismissService,
|
||||
data: new
|
||||
{
|
||||
notification_id = PowerMonitorConsts.PowerLimitNotificationId
|
||||
});
|
||||
}
|
||||
|
||||
internal static string BuildLimitMessage(double currentKw, double limitKw)
|
||||
{
|
||||
return string.Format(CultureInfo.InvariantCulture,
|
||||
"Home power usage **{0:F2} kW** exceeded the limit of **{1:F1} kW**.\n\nHigh-power appliances have been turned off and automatic control paused. Normal control resumes after power drops below the limit.",
|
||||
currentKw, limitKw);
|
||||
"Home power usage **{0:F2} kW** exceeded the limit of **{1:F1} kW**.\n\nHigh-power appliances have been turned off and automatic control paused. Normal control resumes after power drops below the limit.",
|
||||
currentKw, limitKw);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user