npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

battery_util

v2.2.5

Published

nodeJS package to get information about laptop battery

Downloads

97

Readme


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.