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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -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,17 +102,11 @@ 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())
|
||||
@@ -73,9 +116,6 @@ public class Startup
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -243,7 +250,8 @@ namespace GHelper
|
||||
if (AppConfig.IsAlly())
|
||||
{
|
||||
allyControl.Init();
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
settingsForm.AutoKeyboard();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user