mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
System Startup charge limiter
This commit is contained in:
@@ -254,7 +254,7 @@ public class AsusACPI
|
|||||||
return _connected;
|
return _connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AsusACPI()
|
public AsusACPI(bool light = false)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -278,6 +278,8 @@ public class AsusACPI
|
|||||||
Logger.WriteLine($"Can't connect to ACPI: {ex.Message}");
|
Logger.WriteLine($"Can't connect to ACPI: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (light) return;
|
||||||
|
|
||||||
if (AppConfig.IsAdvantageEdition())
|
if (AppConfig.IsAdvantageEdition())
|
||||||
{
|
{
|
||||||
MaxTotal = 250;
|
MaxTotal = 250;
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
using GHelper.Helpers;
|
using GHelper.Helpers;
|
||||||
using Microsoft.Win32.TaskScheduler;
|
using Microsoft.Win32.TaskScheduler;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Security.Principal;
|
using System.Security.Principal;
|
||||||
|
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
|
|
||||||
static string taskName = "GHelper";
|
static string taskName = "GHelper";
|
||||||
|
static string chargeTaskName = taskName + "Charge";
|
||||||
|
static string strExeFilePath = Application.ExecutablePath.Trim();
|
||||||
|
|
||||||
public static bool IsScheduled()
|
public static bool IsScheduled()
|
||||||
{
|
{
|
||||||
@@ -32,7 +33,6 @@ public class Startup
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string strExeFilePath = Application.ExecutablePath.Trim();
|
|
||||||
string action = task.Definition.Actions.FirstOrDefault()!.ToString().Trim();
|
string action = task.Definition.Actions.FirstOrDefault()!.ToString().Trim();
|
||||||
if (!strExeFilePath.Equals(action, StringComparison.OrdinalIgnoreCase) && !File.Exists(action))
|
if (!strExeFilePath.Equals(action, StringComparison.OrdinalIgnoreCase) && !File.Exists(action))
|
||||||
{
|
{
|
||||||
@@ -41,11 +41,60 @@ public class Startup
|
|||||||
UnSchedule();
|
UnSchedule();
|
||||||
Schedule();
|
Schedule();
|
||||||
}
|
}
|
||||||
} catch (Exception ex)
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Logger.WriteLine($"Can't check startup task: {ex.Message}");
|
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()
|
public static void Schedule()
|
||||||
{
|
{
|
||||||
|
|
||||||
string strExeFilePath = Application.ExecutablePath;
|
|
||||||
|
|
||||||
if (strExeFilePath is null) return;
|
|
||||||
|
|
||||||
var userId = WindowsIdentity.GetCurrent().Name;
|
|
||||||
|
|
||||||
using (TaskDefinition td = TaskService.Instance.NewTask())
|
using (TaskDefinition td = TaskService.Instance.NewTask())
|
||||||
{
|
{
|
||||||
|
|
||||||
td.RegistrationInfo.Description = "G-Helper Auto Start";
|
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);
|
td.Actions.Add(strExeFilePath);
|
||||||
|
|
||||||
if (ProcessHelper.IsUserAdministrator())
|
if (ProcessHelper.IsUserAdministrator())
|
||||||
td.Principal.RunLevel = TaskRunLevel.Highest;
|
td.Principal.RunLevel = TaskRunLevel.Highest;
|
||||||
|
|
||||||
td.Settings.StopIfGoingOnBatteries = false;
|
td.Settings.StopIfGoingOnBatteries = false;
|
||||||
td.Settings.DisallowStartIfOnBatteries = false;
|
td.Settings.DisallowStartIfOnBatteries = false;
|
||||||
td.Settings.ExecutionTimeLimit = TimeSpan.Zero;
|
td.Settings.ExecutionTimeLimit = TimeSpan.Zero;
|
||||||
|
|
||||||
Debug.WriteLine(strExeFilePath);
|
|
||||||
Debug.WriteLine(userId);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TaskService.Instance.RootFolder.RegisterTaskDefinition(taskName, td);
|
TaskService.Instance.RootFolder.RegisterTaskDefinition(taskName, td);
|
||||||
@@ -89,6 +129,8 @@ public class Startup
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ScheduleCharge();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void UnSchedule()
|
public static void UnSchedule()
|
||||||
@@ -107,5 +149,7 @@ public class Startup
|
|||||||
ProcessHelper.RunAsAdmin();
|
ProcessHelper.RunAsAdmin();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UnscheduleCharge();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,6 +53,13 @@ namespace GHelper
|
|||||||
string action = "";
|
string action = "";
|
||||||
if (args.Length > 0) action = args[0];
|
if (args.Length > 0) action = args[0];
|
||||||
|
|
||||||
|
if (action == "charge")
|
||||||
|
{
|
||||||
|
BatteryLimit();
|
||||||
|
Application.Exit();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
string language = AppConfig.GetString("language");
|
string language = AppConfig.GetString("language");
|
||||||
|
|
||||||
if (language != null && language.Length > 0)
|
if (language != null && language.Length > 0)
|
||||||
@@ -205,7 +212,7 @@ namespace GHelper
|
|||||||
if (settingsForm.matrixForm is not null && settingsForm.matrixForm.Text != "")
|
if (settingsForm.matrixForm is not null && settingsForm.matrixForm.Text != "")
|
||||||
settingsForm.matrixForm.InitTheme();
|
settingsForm.matrixForm.InitTheme();
|
||||||
|
|
||||||
if (settingsForm.handheldForm is not null && settingsForm.handheldForm.Text != "")
|
if (settingsForm.handheldForm is not null && settingsForm.handheldForm.Text != "")
|
||||||
settingsForm.handheldForm.InitTheme();
|
settingsForm.handheldForm.InitTheme();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -243,7 +250,8 @@ namespace GHelper
|
|||||||
if (AppConfig.IsAlly())
|
if (AppConfig.IsAlly())
|
||||||
{
|
{
|
||||||
allyControl.Init();
|
allyControl.Init();
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
settingsForm.AutoKeyboard();
|
settingsForm.AutoKeyboard();
|
||||||
}
|
}
|
||||||
@@ -288,7 +296,7 @@ namespace GHelper
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var screen = Screen.PrimaryScreen;
|
var screen = Screen.PrimaryScreen;
|
||||||
if (screen is null) screen = Screen.FromControl(settingsForm);
|
if (screen is null) screen = Screen.FromControl(settingsForm);
|
||||||
|
|
||||||
settingsForm.Location = screen.WorkingArea.Location;
|
settingsForm.Location = screen.WorkingArea.Location;
|
||||||
@@ -299,7 +307,7 @@ namespace GHelper
|
|||||||
settingsForm.Activate();
|
settingsForm.Activate();
|
||||||
|
|
||||||
settingsForm.Left = screen.WorkingArea.Width - 10 - settingsForm.Width;
|
settingsForm.Left = screen.WorkingArea.Width - 10 - settingsForm.Width;
|
||||||
|
|
||||||
if (AppConfig.IsAlly())
|
if (AppConfig.IsAlly())
|
||||||
settingsForm.Top = Math.Max(10, screen.Bounds.Height - 110 - settingsForm.Height);
|
settingsForm.Top = Math.Max(10, screen.Bounds.Height - 110 - settingsForm.Height);
|
||||||
else
|
else
|
||||||
@@ -328,6 +336,23 @@ namespace GHelper
|
|||||||
Application.Exit();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user