Skip to content

Malick-Tammal/battery_util

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

52 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”‹battery_util

Changelog - Request feature - Report bug


Contributors Stargazers Issues License

GIT Version NPM Version NPM Version Documentation Documentation

Battery util, get laptop battery information (health , capacity , serial number ...etc)

🌟 Features

  • Get battery health (Accurate reads)
  • Get battery information (Serial number , Battery id , Capacity ..)
  • Battery realtime level
  • Lightweight and reliable

πŸš€ Features will added

  • Cross platform support
  • Getting laptop model
  • Getting Battery Model
  • Battery realtime level (added)
  • Estimated time for full charge
  • Estimated time to fully discharge

Install

npm i battery_util --save

or

npm i battery_util

For developers

git clone https://github.com/Malick-Tammal/battery_util.git

cd battery_util

npm install

Usage

import the package

const bu = require("battery_util");

Get battery information

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

Get specific data

const bu = require("battery_util");

bu.batteryInfo()
  .then(data => {
    console.log(data.fullChargeCapacity); //(Example) 60800
  })
  .catch(err => {
    console.log(err);
});

data options :

  • fileSavedPath
  • measureUnit
  • designCapacity
  • fullChargeCapacity
  • health
  • cycleCount
  • id
  • serialNumber
  • more in the future ....

Get battery state

const bu = require("battery_util");

bu.batteryState()
  .then(data => {
    console.log(data);
  }).catch(err => {
    console.log(err);
});
// output (Example)
// level : 78
// isCharging : true

Get battery state avery second

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 }

Get specific data

const bu = require("battery_util");

bu.batteryState()
  .then(data => {
    console.log(data.level); // (Example) 78
  }).catch(err => {
    console.log(err);
});

data options :

  • level
  • isCharging

πŸ“– How it works

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%

2 - ▢️ Getting Started

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

❗ Issues

  • Only working in windows systems
  • if you find issue please open new issue

πŸ–‹οΈ Author

πŸ§‘πŸ½ Malick Tammal

🀝 Contributing

Contributions, issues and feature requests are welcome!

Feel free to check issues page.

πŸ”₯ Show your support

Give a ⭐️ if this project helped you!

πŸ“œ License

Copyright Β© 2024 Malick Tammal. All rights reserved.

Licensed under the MIT license.