-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d44933
commit cb3550f
Showing
10 changed files
with
556 additions
and
549 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,80 @@ | ||
namespace PowerPlanSwitcher | ||
namespace PowerPlanSwitcher | ||
{ | ||
using System.Runtime.InteropServices; | ||
|
||
using PowerPlanSwitcher.Properties; | ||
using static Vanara.PInvoke.Kernel32; | ||
|
||
public class BatteryMonitor | ||
{ | ||
// 定义SYSTEM_POWER_STATUS结构体 | ||
[StructLayout(LayoutKind.Sequential)] | ||
public struct SYSTEM_POWER_STATUS | ||
{ | ||
public byte ACLineStatus; // AC 电源线状态:0 表示离线(电池供电),1 表示在线(插电) | ||
public byte BatteryFlag; // 电池标志 | ||
public byte BatteryLifePercent; // 电池剩余百分比 | ||
public byte Reserved1; // 保留 | ||
public int BatteryLifeTime; // 电池剩余时间(以秒为单位) | ||
public int BatteryFullLifeTime; // 电池满电使用时间(以秒为单位) | ||
} | ||
|
||
// 引入GetSystemPowerStatus函数 | ||
[DllImport("kernel32.dll")] //和GetSystemPowerStatus有关 | ||
private static extern bool GetSystemPowerStatus(out SYSTEM_POWER_STATUS lpSystemPowerStatus); | ||
private static bool ToggleMark = true; // 切换标记 | ||
// private static PowerRule ACListData; | ||
// private static PowerRule BatteryListData; | ||
private static Guid? BatteryGuid; // 电池供电时的电源计划GUID | ||
private static Guid? AcPowerGuid; // 插电时的电源计划GUID | ||
public static PowerStatus BatteryNull; | ||
private static SYSTEM_POWER_STATUS powerStatus; | ||
|
||
public static void PlanValue() | ||
private static Guid AcPowerSchemeGuid => | ||
Settings.Default.AcPowerSchemeGuid; | ||
private static Guid BatterPowerSchemeGuid => | ||
Settings.Default.BatterPowerSchemeGuid; | ||
|
||
private static bool toggleMark = true; | ||
|
||
public static bool HasSystemBattery() => | ||
SystemInformation.PowerStatus.BatteryChargeStatus | ||
!= BatteryChargeStatus.NoSystemBattery; | ||
|
||
public static void Initialize() | ||
{ | ||
BatteryNull = SystemInformation.PowerStatus; | ||
if (BatteryNull.BatteryChargeStatus != BatteryChargeStatus.NoSystemBattery) | ||
{ | ||
// 调用SettingsHelper.GetGuidsWithVisibility()方法并解构返回的元组 | ||
var (_acGuid, _batteryGuid) = PowerSchemeSettings.GetGuidsWithVisibility(); | ||
|
||
// 将解构得到的GUID值赋给对应的静态字段 | ||
AcPowerGuid = _acGuid; | ||
BatteryGuid = _batteryGuid; | ||
if (!GetSystemPowerStatus(out var powerStatus)) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
public static void BatteryMonitorInitialization() | ||
{ | ||
PlanValue(); | ||
MonitorBatterySwitcInitialization(); | ||
} | ||
|
||
private static void MonitorBatterySwitcInitialization() | ||
{ | ||
if (!ToggleMark && BatteryGuid != Guid.Empty && AcPowerGuid == Guid.Empty && powerStatus.ACLineStatus == 1) // 插电状态 02 | ||
|
||
if (!toggleMark | ||
&& BatterPowerSchemeGuid != Guid.Empty | ||
&& AcPowerSchemeGuid == Guid.Empty | ||
&& powerStatus.ACLineStatus == AC_STATUS.AC_ONLINE) | ||
{ | ||
ToggleMark = true; | ||
toggleMark = true; | ||
} | ||
else if (ToggleMark && AcPowerGuid != Guid.Empty && BatteryGuid == Guid.Empty && powerStatus.ACLineStatus == 0) //电池供电 01 | ||
|
||
if (toggleMark | ||
&& AcPowerSchemeGuid != Guid.Empty | ||
&& BatterPowerSchemeGuid == Guid.Empty | ||
&& powerStatus.ACLineStatus == AC_STATUS.AC_OFFLINE) | ||
{ | ||
ToggleMark = false; | ||
} | ||
toggleMark = false; | ||
} | ||
} | ||
|
||
// 监测电池状态并切换电源计划的方法 | ||
public static void MonitorBatterySwitc() | ||
|
||
public static Guid GetPowerPlanGuid() | ||
{ | ||
if (BatteryNull.BatteryChargeStatus != BatteryChargeStatus.NoSystemBattery) | ||
if (!HasSystemBattery()) | ||
{ | ||
GetSystemPowerStatus(out powerStatus); | ||
var PMGAPSG = PowerManager.GetActivePowerSchemeGuid(); | ||
|
||
if (!ToggleMark && AcPowerGuid != Guid.Empty && powerStatus.ACLineStatus == 1 && PMGAPSG != AcPowerGuid) // 插电状态 01 03 | ||
{ | ||
PowerManager.SetActivePowerScheme(AcPowerGuid.Value); // 激活插电时的电源计划 | ||
ProcessMonitor.baselinePowerSchemeGuid = AcPowerGuid.Value; // 电池供电时的电源计划GUID | ||
ToggleMark = true; | ||
} | ||
else if (ToggleMark && BatteryGuid != Guid.Empty && powerStatus.ACLineStatus == 0 && PMGAPSG != BatteryGuid) //电池供电 02 03 | ||
{ | ||
PowerManager.SetActivePowerScheme(BatteryGuid.Value); // 激活电池供电时的电源计划 | ||
ProcessMonitor.baselinePowerSchemeGuid = BatteryGuid.Value; | ||
ToggleMark = false; | ||
} | ||
else | ||
{ | ||
MonitorBatterySwitcInitialization(); | ||
} | ||
return Guid.Empty; | ||
} | ||
|
||
if (!GetSystemPowerStatus(out var powerStatus)) | ||
{ | ||
return Guid.Empty; | ||
} | ||
|
||
var activePowerSchemeGuid = PowerManager.GetActivePowerSchemeGuid(); | ||
|
||
if (!toggleMark | ||
&& AcPowerSchemeGuid != Guid.Empty | ||
&& powerStatus.ACLineStatus == AC_STATUS.AC_ONLINE | ||
&& activePowerSchemeGuid != AcPowerSchemeGuid) | ||
{ | ||
toggleMark = true; | ||
return AcPowerSchemeGuid; | ||
} | ||
|
||
if (toggleMark | ||
&& BatterPowerSchemeGuid != Guid.Empty | ||
&& powerStatus.ACLineStatus == AC_STATUS.AC_OFFLINE | ||
&& activePowerSchemeGuid != BatterPowerSchemeGuid) | ||
{ | ||
toggleMark = false; | ||
return BatterPowerSchemeGuid; | ||
} | ||
|
||
Initialize(); | ||
|
||
return Guid.Empty; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.