System Startup charge limiter

This commit is contained in:
Serge
2024-09-09 12:33:22 +02:00
parent d0f1b70c9f
commit 421dc0c05c
3 changed files with 90 additions and 19 deletions

View File

@@ -254,7 +254,7 @@ public class AsusACPI
return _connected;
}
public AsusACPI()
public AsusACPI(bool light = false)
{
try
{
@@ -278,6 +278,8 @@ public class AsusACPI
Logger.WriteLine($"Can't connect to ACPI: {ex.Message}");
}
if (light) return;
if (AppConfig.IsAdvantageEdition())
{
MaxTotal = 250;

View File

@@ -1,12 +1,13 @@
using GHelper.Helpers;
using Microsoft.Win32.TaskScheduler;
using System.Diagnostics;
using System.Security.Principal;
public class Startup
{
static string taskName = "GHelper";
static string chargeTaskName = taskName + "Charge";
static string strExeFilePath = Application.ExecutablePath.Trim();
public static bool IsScheduled()
{
@@ -32,7 +33,6 @@ public class Startup
{
try
{
string strExeFilePath = Application.ExecutablePath.Trim();
string action = task.Definition.Actions.FirstOrDefault()!.ToString().Trim();
if (!strExeFilePath.Equals(action, StringComparison.OrdinalIgnoreCase) && !File.Exists(action))
{
@@ -41,11 +41,60 @@ public class Startup
UnSchedule();
Schedule();
}
} catch (Exception ex)
}
catch (Exception ex)
{
Logger.WriteLine($"Can't check startup task: {ex.Message}");
}
if (taskService.RootFolder.AllTasks.FirstOrDefault(t => t.Name == chargeTaskName) == null) ScheduleCharge();
}
}
}
public static void UnscheduleCharge()
{
using (TaskService taskService = new TaskService())
{
try
{
taskService.RootFolder.DeleteTask(chargeTaskName);
}
catch (Exception e)
{
Logger.WriteLine("Can't remove charge limit task: " + e.Message);
}
}
}
public static void ScheduleCharge()
{
if (strExeFilePath is null) return;
using (TaskDefinition td = TaskService.Instance.NewTask())
{
td.RegistrationInfo.Description = "G-Helper Charge Limit";
td.Triggers.Add(new BootTrigger());
td.Actions.Add(strExeFilePath, "charge");
td.Principal.RunLevel = TaskRunLevel.LUA;
td.Principal.LogonType = TaskLogonType.S4U;
td.Principal.UserId = WindowsIdentity.GetCurrent().Name;
td.Settings.StopIfGoingOnBatteries = false;
td.Settings.DisallowStartIfOnBatteries = false;
td.Settings.ExecutionTimeLimit = TimeSpan.Zero;
try
{
TaskService.Instance.RootFolder.RegisterTaskDefinition(chargeTaskName, td);
Logger.WriteLine("Charge limit task scheduled: " + strExeFilePath);
}
catch (Exception e)
{
Logger.WriteLine("Can't create a charge limit task: " + e.Message);
}
}
}
@@ -53,29 +102,20 @@ public class Startup
public static void Schedule()
{
string strExeFilePath = Application.ExecutablePath;
if (strExeFilePath is null) return;
var userId = WindowsIdentity.GetCurrent().Name;
using (TaskDefinition td = TaskService.Instance.NewTask())
{
td.RegistrationInfo.Description = "G-Helper Auto Start";
td.Triggers.Add(new LogonTrigger { UserId = userId, Delay = TimeSpan.FromSeconds(1) });
td.Triggers.Add(new LogonTrigger { UserId = WindowsIdentity.GetCurrent().Name, Delay = TimeSpan.FromSeconds(1) });
td.Actions.Add(strExeFilePath);
if (ProcessHelper.IsUserAdministrator())
if (ProcessHelper.IsUserAdministrator())
td.Principal.RunLevel = TaskRunLevel.Highest;
td.Settings.StopIfGoingOnBatteries = false;
td.Settings.DisallowStartIfOnBatteries = false;
td.Settings.ExecutionTimeLimit = TimeSpan.Zero;
Debug.WriteLine(strExeFilePath);
Debug.WriteLine(userId);
try
{
TaskService.Instance.RootFolder.RegisterTaskDefinition(taskName, td);
@@ -89,6 +129,8 @@ public class Startup
}
}
ScheduleCharge();
}
public static void UnSchedule()
@@ -107,5 +149,7 @@ public class Startup
ProcessHelper.RunAsAdmin();
}
}
UnscheduleCharge();
}
}

View File

@@ -53,6 +53,13 @@ namespace GHelper
string action = "";
if (args.Length > 0) action = args[0];
if (action == "charge")
{
BatteryLimit();
Application.Exit();
return;
}
string language = AppConfig.GetString("language");
if (language != null && language.Length > 0)
@@ -205,7 +212,7 @@ namespace GHelper
if (settingsForm.matrixForm is not null && settingsForm.matrixForm.Text != "")
settingsForm.matrixForm.InitTheme();
if (settingsForm.handheldForm is not null && settingsForm.handheldForm.Text != "")
if (settingsForm.handheldForm is not null && settingsForm.handheldForm.Text != "")
settingsForm.handheldForm.InitTheme();
break;
@@ -243,7 +250,8 @@ namespace GHelper
if (AppConfig.IsAlly())
{
allyControl.Init();
} else
}
else
{
settingsForm.AutoKeyboard();
}
@@ -288,7 +296,7 @@ namespace GHelper
}
else
{
var screen = Screen.PrimaryScreen;
var screen = Screen.PrimaryScreen;
if (screen is null) screen = Screen.FromControl(settingsForm);
settingsForm.Location = screen.WorkingArea.Location;
@@ -299,7 +307,7 @@ namespace GHelper
settingsForm.Activate();
settingsForm.Left = screen.WorkingArea.Width - 10 - settingsForm.Width;
if (AppConfig.IsAlly())
settingsForm.Top = Math.Max(10, screen.Bounds.Height - 110 - settingsForm.Height);
else
@@ -328,6 +336,23 @@ namespace GHelper
Application.Exit();
}
static void BatteryLimit()
{
try
{
int limit = 80;
if (limit > 0 && limit < 100)
{
Logger.WriteLine($"Startup Battery Limit {limit}");
acpi = new AsusACPI(true);
acpi.DeviceSet(AsusACPI.BatteryLimit, limit, "Limit");
}
}
catch (Exception ex)
{
Logger.WriteLine("Startup Battery Limit Error: " + ex.Message);
}
}
}
}