Skip to content

Commit

Permalink
通过winapi获取windows系统的电池剩余量
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Mar 19, 2024
1 parent 675e4bd commit da3d888
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 2 deletions.
12 changes: 10 additions & 2 deletions NewLife.Core/Common/MachineInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using NewLife.Serialization;
using System.Runtime.Versioning;
using System.Diagnostics.CodeAnalysis;
using NewLife.Windows;

#if NETFRAMEWORK
using System.Management;
using Microsoft.VisualBasic.Devices;
Expand Down Expand Up @@ -572,6 +574,8 @@ private void RefreshWindows()

CpuRate = total == 0 ? 0 : (Single)Math.Round((Single)(total - idle) / total, 4);

var power = new PowerStatus();

#if NETFRAMEWORK
if (!_excludes.Contains(nameof(Temperature)))
{
Expand All @@ -595,7 +599,9 @@ private void RefreshWindows()
}
}

if (!_excludes.Contains(nameof(Battery)))
if (power.BatteryLifePercent > 0)
Battery = power.BatteryLifePercent;
else if (!_excludes.Contains(nameof(Battery)))
{
// 电池剩余
var str = GetInfo("Win32_Battery", "EstimatedChargeRemaining");
Expand Down Expand Up @@ -625,7 +631,9 @@ private void RefreshWindows()
}
}

if (!_excludes.Contains(nameof(Battery)))
if (power.BatteryLifePercent > 0)
Battery = power.BatteryLifePercent;
else if (!_excludes.Contains(nameof(Battery)))
{
var battery = ReadWmic("path win32_battery", "EstimatedChargeRemaining");
if (battery != null && battery.Count > 0)
Expand Down
109 changes: 109 additions & 0 deletions NewLife.Core/Windows/PowerStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System.Runtime.InteropServices;

namespace NewLife.Windows;

/// <summary>系统电源状态</summary>
public enum PowerLineStatus
{
/// <summary>脱机状态</summary>
Offline = 0,
/// <summary>联机状态</summary>
Online = 1,
/// <summary>电源状态未知</summary>
Unknown = 255
}

/// <summary>充电状态信息</summary>
[Flags]
public enum BatteryChargeStatus
{
/// <summary>指示电池能量级别较高</summary>
High = 1,
/// <summary>指示电池能量级别较低</summary>
Low = 2,
/// <summary>指示电池能量严重不足</summary>
Critical = 4,
/// <summary>指示电池正在充电</summary>
Charging = 8,
/// <summary>指示没有电池存在</summary>
NoSystemBattery = 0x80,
/// <summary>指示未知电池状态</summary>
Unknown = 0xFF
}

/// <summary>电源状态</summary>
public class PowerStatus
{
private SYSTEM_POWER_STATUS systemPowerStatus;

/// <summary>当前的系统电源状态</summary>
public PowerLineStatus PowerLineStatus
{
get
{
UpdateSystemPowerStatus();
return (PowerLineStatus)systemPowerStatus.ACLineStatus;
}
}

/// <summary>当前的电池电量状态</summary>
public BatteryChargeStatus BatteryChargeStatus
{
get
{
UpdateSystemPowerStatus();
return (BatteryChargeStatus)systemPowerStatus.BatteryFlag;
}
}

/// <summary>报告的主电池电源的完全充电寿命(以秒为单位)</summary>
public Int32 BatteryFullLifetime
{
get
{
UpdateSystemPowerStatus();
return systemPowerStatus.BatteryFullLifeTime;
}
}

/// <summary>电池剩余电量的近似量</summary>
public Single BatteryLifePercent
{
get
{
UpdateSystemPowerStatus();
var num = systemPowerStatus.BatteryLifePercent / 100f;
return num > 1f ? 1f : num;
}
}

/// <summary>电池的剩余使用时间的近似秒数</summary>
public Int32 BatteryLifeRemaining
{
get
{
UpdateSystemPowerStatus();
return systemPowerStatus.BatteryLifeTime;
}
}

private void UpdateSystemPowerStatus() => GetSystemPowerStatus(ref systemPowerStatus);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
static extern Boolean GetSystemPowerStatus([In][Out] ref SYSTEM_POWER_STATUS systemPowerStatus);

struct SYSTEM_POWER_STATUS
{
public Byte ACLineStatus;

public Byte BatteryFlag;

public Byte BatteryLifePercent;

public Byte Reserved1;

public Int32 BatteryLifeTime;

public Int32 BatteryFullLifeTime;
}
}

0 comments on commit da3d888

Please sign in to comment.