Changelog - Request feature - Report bug
Battery util, get laptop battery information (health , capacity , serial number ...etc)
- Get battery health (Accurate reads)
- Get battery information (Serial number , Battery id , Capacity ..)
- Battery realtime level
- Lightweight and reliable
- Cross platform support
- Getting laptop model
- Getting Battery Model
- Battery realtime level (added)
- Estimated time for full charge
- Estimated time to fully discharge
npm i battery_util --save
or
npm i battery_util
git clone https://github.com/Malick-Tammal/battery_util.git
cd battery_util
npm install
import the package
const bu = require("battery_util");
const bu = require("battery_util");
bu.batteryInfo()
.then(data => {
console.log(data);
})
.catch(err => {
console.log(err);
});
// output (Example)
// fileSavedPath (battery report)
// measureUnit : "mWh"
// designCapacity : 60800
// fullChargeCapacity : 60800
// health : 100%
// cycleCount : 0
// id : DELL 9GRYT8A
// serialNumber : 204
const bu = require("battery_util");
bu.batteryInfo()
.then(data => {
console.log(data.fullChargeCapacity); //(Example) 60800
})
.catch(err => {
console.log(err);
});
- fileSavedPath
- measureUnit
- designCapacity
- fullChargeCapacity
- health
- cycleCount
- id
- serialNumber
- more in the future ....
const bu = require("battery_util");
bu.batteryState()
.then(data => {
console.log(data);
}).catch(err => {
console.log(err);
});
// output (Example)
// level : 78
// isCharging : true
const bu = require("battery_util");
setInterval(() => {
bu.batteryState()
.then(data => {
console.log(data);
}).catch(err => {
console.log(err);
});
}, 1000);
// output (Example)
// { level: 78, isCharging: true }
// { level: 78, isCharging: true }
// { level: 79, isCharging: true }
// { level: 79, isCharging: false }
const bu = require("battery_util");
bu.batteryState()
.then(data => {
console.log(data.level); // (Example) 78
}).catch(err => {
console.log(err);
});
- level
- isCharging
With the help of Child Process package we can execute a powershell commend and return data from it (Microsoft battery report)
Importing child process
const exec = require("child_process").exec;
Function to execute a cmd commends (scripts)
const execute = (command, callback) => {
exec(command, { shell: "powershell.exe" }, (error, stdout, stderr) => {
callback(stdout, stderr);
});
};
Pscommend (powershell commend)
const psCommend = `powercfg /batteryreport /XML /OUTPUT "./batteryreport.xml"; Start-Sleep 1; [xml]$b = Get-Content "./batteryreport.xml"; $b.BatteryReport.Batteries | ForEach-Object { ; [PSCustomObject]@{DesignCapacity = $_.Battery.DesignCapacity; FullChargeCapacity = $_.Battery.FullChargeCapacity; CycleCount = $_.Battery.CycleCount; Id = $_.Battery.id; SerialNumber = $_.Battery.SerialNumber } }`;
Exec powershell command with JS
execute(psCommend, (stdout, stderr) => {
if (
util.getValue(stdout.split("\r\n"), "designCapacity", ":") !== "" &&
util.getValue(stdout.split("\r\n"), "fullChargeCapacity", ":") !== ""
) {
console.log(stdout);
// output (Example)
// fileSavedPath (batteryreport.xml path)
// measureUnit : "mWh"
// designCapacity : 60800
// fullChargeCapacity : 60800
// cycleCount : 0
// id : DELL 9GRYT8A
// serialNumber : 204
} else {
console.log(
`Error occurred while executing Powershell script.\n ${stderr}`
);
}
});
Calculating battery health
const calcBatteryHealth = (fChargeC, designC) => {
let batteryHealth = Math.round((fChargeC / designC) * 100);
return batteryHealth;
};
usage :
const health = `${calcBatteryHealth(fullChargeCapacity, designCapacity)}%`;
// (Example) 100%
Clone the repository
git clone https://github.com/Malick-Tammal/battery_util.git
cd battery_util
package folder for (npm package) / test folder for (testing)
Start CLI interface
npm run cli
Start testing
npm test
- Only working in windows systems
- if you find issue please open new issue
π§π½ Malick Tammal
- Website: Portfolio
- Github: @Malick-Tammal
- Instagram @Malick_Tammal
- Youtube @Malick_Tammal
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Give a βοΈ if this project helped you!
Copyright Β© 2024 Malick Tammal. All rights reserved.
Licensed under the MIT license.