diff --git a/Hardware.Info/Hardware.Info.csproj b/Hardware.Info/Hardware.Info.csproj index 517deee..395a3b9 100644 --- a/Hardware.Info/Hardware.Info.csproj +++ b/Hardware.Info/Hardware.Info.csproj @@ -9,7 +9,7 @@ Hardware.Info Hardware.Info - 10.0.0.1 + 10.0.1.0 Jinjinov Battery, BIOS, CPU - processor, storage drive, keyboard, RAM - memory, monitor, motherboard, mouse, NIC - network adapter, printer, sound card - audio card, graphics card - video card. Hardware.Info is a .NET Standard 2.0 library and uses WMI on Windows, /dev, /proc, /sys on Linux and sysctl, system_profiler on macOS. Computer;Device;Hardware;Info;Information;NET Standard;Windows;Linux;macOS diff --git a/Hardware.Info/Mac/HardwareInfoRetrieval.cs b/Hardware.Info/Mac/HardwareInfoRetrieval.cs index 54e4926..81ed950 100644 --- a/Hardware.Info/Mac/HardwareInfoRetrieval.cs +++ b/Hardware.Info/Mac/HardwareInfoRetrieval.cs @@ -186,57 +186,121 @@ public MemoryStatus GetMemoryStatus() public List GetBatteryList() { List batteryList = new List(); + Battery battery = new Battery(); - /* https://www.iphonetricks.org/check-battery-health-on-mac-using-terminal/ -Juliens-MacBook-Pro:~ tadel$ ioreg -l -w0 | grep ' \"MaxCapacity\" ' | grep -Eo "\d+" -5034 -Juliens-MacBook-Pro:~ tadel$ ioreg -l -w0 | grep ' \"CurrentCapacity\" ' | grep -Eo "\d+" -4860 -Juliens-MacBook-Pro:~ tadel$ ioreg -l -w0 | grep ' \"DesignCapacity\" ' | grep -Eo "\d+" -5770 -Juliens-MacBook-Pro:~ tadel$ ioreg -l -w0 | grep -E ' \"Voltage\" ' | grep -Eo "\d+" -12054 - */ - //if (uint.TryParse(ReadProcessOutput("ioreg", "-l -w0 | grep ' \\\"DesignCapacitys\\\" ' | grep -Eo \"\\d+\""), out var designCapacity)) battery.DesignCapacity = designCapacity; - //if (uint.TryParse(ReadProcessOutput("ioreg", "-l -w0 | grep ' \\\"MaxCapacity\\\" ' | grep -Eo \"\\d+\""), out var fullChargeCapacity)) battery.FullChargeCapacity = fullChargeCapacity; - - /* https://coderwall.com/p/bechiq/macos-get-battery-percentage-from-command-line -Juliens-MacBook-Pro:~ tadel$ pmset -g batt -Now drawing from 'Battery Power' - -InternalBattery-0 (id=3539043) 96%; discharging; 5:03 remaining present: true -Juliens-MacBook-Pro:~ tadel$ pmset -g batt | grep -Eo "\d+%" | cut -d% -f1 -96 -Juliens-MacBook-Pro:~ tadel$ pmset -g batt | grep -Eo "\d+:\d+" -6:23 -Juliens-MacBook-Pro:~ tadel$ pmset -g batt -Now drawing from 'AC Power' - -InternalBattery-0 (id=3539043) 92%; charging; (no estimate) present: true - */ - var pmsetBattOutput = ReadProcessOutput("pmset", "-g batt"); - - // EstimatedChargeRemaining - var estimatedChargeRemainingRegex = new Regex("(\\d+)%"); - if (ushort.TryParse(estimatedChargeRemainingRegex.Match(pmsetBattOutput).Groups[1].Value, out var estimatedChargeRemaining)) - battery.EstimatedChargeRemaining = estimatedChargeRemaining; - - // EstimatedRunTime - var estimatedRunTimeRegex = new Regex("(\\d+:\\d+)"); - var remainingTime = estimatedRunTimeRegex.Match(pmsetBattOutput).Groups[1].Value; - if (remainingTime.Contains(":")) - { - string[] timeParts = remainingTime.Split(':'); + // https://stackoverflow.com/questions/29278961/check-mac-battery-percentage-in-swift + + // https://developer.apple.com/documentation/iokit/iopowersources_h + + /* + SPPowerDataType +Power: + + System Power Settings: + + AC Power: + System Sleep Timer (Minutes): 10 + Disk Sleep Timer (Minutes): 10 + Display Sleep Timer (Minutes): 10 + Sleep on Power Button: Yes + Current Power Source: Yes + Hibernate Mode: 0 + Standby Delay: 4200 + Standby Enabled: 1 + + Hardware Configuration: + + UPS Installed: No + + Model Information: + Serial Number: W01396THJD3LA + Manufacturer: SMP + Device Name: bq20z451 + Pack Lot Code: 0 + PCB Lot Code: 0 + Firmware Version: 201 + Hardware Revision: 000a + Cell Revision: 165 + Charge Information: + Charge Remaining (mAh): 5013 + Fully Charged: Yes + Charging: No + Full Charge Capacity (mAh): 5086 + Health Information: + Cycle Count: 72 + Condition: Normal + Battery Installed: Yes + Amperage (mA): -300 + Voltage (mV): 12303 + /**/ + + // https://www.iphonetricks.org/check-battery-health-on-mac-using-terminal/ + /* + Juliens-MacBook-Pro:~ tadel$ ioreg -l -w0 | grep ' \"MaxCapacity\" ' | grep -Eo "\d+" + 5034 + Juliens-MacBook-Pro:~ tadel$ ioreg -l -w0 | grep ' \"CurrentCapacity\" ' | grep -Eo "\d+" + 4860 + Juliens-MacBook-Pro:~ tadel$ ioreg -l -w0 | grep ' \"DesignCapacity\" ' | grep -Eo "\d+" + 5770 + Juliens-MacBook-Pro:~ tadel$ ioreg -l -w0 | grep -E ' \"Voltage\" ' | grep -Eo "\d+" + 12054 + /**/ + + /* + string processOutput = ReadProcessOutput("ioreg", "-l -w0 | grep ' \\\"DesignCapacity\\\" ' | grep -Eo \"\\d+\""); + + if (uint.TryParse(processOutput, out uint designCapacity)) + battery.DesignCapacity = designCapacity; + + processOutput = ReadProcessOutput("ioreg", "-l -w0 | grep ' \\\"MaxCapacity\\\" ' | grep -Eo \"\\d+\""); + + if (uint.TryParse(processOutput, out uint fullChargeCapacity)) + battery.FullChargeCapacity = fullChargeCapacity; + /**/ + + // https://coderwall.com/p/bechiq/macos-get-battery-percentage-from-command-line + /* + Juliens-MacBook-Pro:~ tadel$ pmset -g batt + Now drawing from 'Battery Power' + -InternalBattery-0 (id=3539043) 96%; discharging; 5:03 remaining present: true + Juliens-MacBook-Pro:~ tadel$ pmset -g batt | grep -Eo "\d+%" | cut -d% -f1 + 96 + Juliens-MacBook-Pro:~ tadel$ pmset -g batt | grep -Eo "\d+:\d+" + 6:23 + Juliens-MacBook-Pro:~ tadel$ pmset -g batt + Now drawing from 'AC Power' + -InternalBattery-0 (id=3539043) 92%; charging; (no estimate) present: true + /**/ + + string processOutput = ReadProcessOutput("pmset", "-g batt"); - if (timeParts.Length == 2) + Regex estimatedChargeRemainingRegex = new Regex("(\\d+)%"); + Match match = estimatedChargeRemainingRegex.Match(processOutput); + + if (match.Success && match.Groups.Count > 1) + { + if (ushort.TryParse(match.Groups[1].Value, out ushort estimatedChargeRemaining)) { - int hours = int.Parse(timeParts[0]); - int minutes = int.Parse(timeParts[1]); + battery.EstimatedChargeRemaining = estimatedChargeRemaining; + } + } + + Regex estimatedRunTimeRegex = new Regex("(\\d+:\\d+)"); + match = estimatedRunTimeRegex.Match(processOutput); + + if (match.Success && match.Groups.Count > 1) + { + string[] estimatedRunTime = match.Groups[1].Value.Split(':'); - battery.EstimatedRunTime = (ushort)(hours * 60 + minutes); + if (estimatedRunTime.Length == 2 && uint.TryParse(estimatedRunTime[0], out uint hours) && uint.TryParse(estimatedRunTime[1], out uint minutes)) + { + battery.EstimatedRunTime = hours * 60 + minutes; } } batteryList.Add(battery); + return batteryList; } diff --git a/README.md b/README.md index b1a7879..8c7c196 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Battery, BIOS, CPU - processor, storage drive, keyboard, RAM - memory, monitor, 1. Include NuGet package from https://www.nuget.org/packages/Hardware.Info - + 2. Call `RefreshAll()` or one of the other `Refresh*()` methods: @@ -180,6 +180,8 @@ Setting `includeNetworkAdapterConfiguration` to `false` has only a small impact ## Version history: +- 10.0.1.0: + - Added `GetBatteryList()` in macOS - by [@Tadelsucht](https://github.com/Tadelsucht) - 10.0.0.1: - Fixed `GetBatteryList()` in Linux - by [@Tadelsucht](https://github.com/Tadelsucht) - 10.0.0.0: