-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.cs
117 lines (97 loc) · 4.56 KB
/
Worker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using IdeapadToolkit.Models;
using IdeapadToolkit.Services;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
using LenovoBatteryManager.Services;
namespace LenovoBatteryManager;
public class WindowsBackgroundService : IHostedService
{
private readonly LenovoPowerSettingsService _service;
private readonly ILogger<WindowsBackgroundService> _logger;
private readonly CIM_Battery _battery;
public WindowsBackgroundService(
LenovoPowerSettingsService service,
ILogger<WindowsBackgroundService> logger,
CIM_Battery battery) =>
(_service, _logger, _battery) = (service, logger, battery);
public async Task StartAsync(CancellationToken stoppingToken)
{
try
{
var confBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
Dictionary<string, PowerPlan> dictPower = new () {
{"IntelligentCooling", PowerPlan.IntelligentCooling},
{"EfficiencyMode", PowerPlan.EfficiencyMode},
{"ExtremePerformance", PowerPlan.ExtremePerformance},
};
while (!stoppingToken.IsCancellationRequested)
{
var conf = confBuilder.Build();
int targetLevel = conf.GetValue<int>("TargetBatteryLevel", 70);
int threshold = conf.GetValue<int>("BatteryLevelThreshold", 5);
string chargingModeString = conf.GetValue<string>("ChargingMode", "Normal") ?? "Normal";
int checkInterval = conf.GetValue<int>("CheckIntervalSeconds", 60);
string powerPlanBatteryString = conf.GetValue<string>("PowerPlanBattery") ?? "EfficiencyMode";
string powerPlanPowerString = conf.GetValue<string>("PowerPlanPower") ?? "IntelligentCooling";
_battery.FetchData();
ChargingMode chargingMode = ChargingMode.Normal;
if (chargingModeString.ToLower() == "rapid")
chargingMode = ChargingMode.Rapid;
PowerPlan powerPlanBattery = PowerPlan.EfficiencyMode;
PowerPlan powerPlanPower = PowerPlan.IntelligentCooling;
if (dictPower.ContainsKey(powerPlanBatteryString))
powerPlanBattery = dictPower[powerPlanBatteryString];
if (dictPower.ContainsKey(powerPlanPowerString))
powerPlanPower = dictPower[powerPlanPowerString];
var loginfo = (ChargingMode cm) => {
_logger.LogInformation($@"Battery level is {_battery.EstimatedChargeRemaining}
Changing charging mode to {cm}
Target battery level: {targetLevel}
Target threshold: {threshold}
Charging mode preferred: {chargingModeString}");
};
var logInfoPp = (PowerPlan pp) => {
_logger.LogInformation($@"Changing power plan to {pp}
PowerPlanBattery: {powerPlanBattery}
PowerPlanPower: {powerPlanPower}");
};
if (
_service.GetChargingMode() != chargingMode &&
_battery.EstimatedChargeRemaining < (targetLevel - threshold)
)
{
_service.SetChargingMode(chargingMode);
loginfo(chargingMode);
} else if (
_service.GetChargingMode() != ChargingMode.Conservation &&
_battery.EstimatedChargeRemaining >= targetLevel
)
{
_service.SetChargingMode(ChargingMode.Conservation);
loginfo(ChargingMode.Conservation);
}
if (_battery.BatteryStatus == 1 && _service.GetPowerPlan() != powerPlanBattery)
{
_service.SetPowerPlan(powerPlanBattery);
logInfoPp(powerPlanBattery);
} else if (_battery.BatteryStatus == 2 && _service.GetPowerPlan() != powerPlanPower)
{
_service.SetPowerPlan(powerPlanPower);
logInfoPp(powerPlanPower);
}
await Task.Delay(TimeSpan.FromSeconds(checkInterval), stoppingToken);
}
}
catch (TaskCanceledException) { }
catch (Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
Environment.Exit(1);
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopped.");
return Task.CompletedTask;
}
}