Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cmd-johnson/node-power-info
Cross-platform node library for accessing battery power information
https://github.com/cmd-johnson/node-power-info
Last synced: 3 months ago
JSON representation
Cross-platform node library for accessing battery power information
- Host: GitHub
- URL: https://github.com/cmd-johnson/node-power-info
- Owner: cmd-johnson
- License: mit
- Created: 2015-08-15T12:03:30.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-08-05T23:08:45.000Z (over 6 years ago)
- Last Synced: 2024-09-13T13:38:10.350Z (3 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/node-power-info
- Size: 41 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# node-power-info
Cross-platform node library for accessing battery and power adapter information## Features
Reads power status in percent, along with information on the charge mode of all
installed batteries (charging, discharging, full, unknown) as well as the
estimated remaining time until the battery is fully (dis-)charged.## Cross-platform
For the time being, only supports linux and darwin platforms. Windows is planned
but it could take some time until I get to do that.
If you think you can provide an implementation for windows or any other platform,
feel free to make a pull request!## Known issues
Since I'm not owning a mac, the darwin implementation is completely untested. The
current solution simply runs `pmset -g batt` to get basic information on the
batteries installed and runs the output through a regex to extract percentage
and charge status data. That should work in theory, but if you have time to test
it in a real environment (possibly including some edge cases, like output when
at 100%), please, take that time and report your findings!## How it works
To get information on the available batteries, the module sports a list of
information providers that are each supported on different systems. When
requiring the module, it first checks every provider for availability. The first
found supported provider will be used as default provider for accessing battery
information.In this way, the module doesn't depend on node's `require('os').type()` value,
but tries all available options for maximum compatibility.## Example
```js
const powerInfo = require('node-power-info')powerInfo.getDefault()
.then(provider => provider.getBatteries())
.then(batteries => batteries.forEach(battery => {
console.dir(battery)
console.log(`${battery.id} is at ${battery.powerLevel}% and ${battery.chargeStatus}.`)
if (battery.isTimeAvailable) {
const remainingMinutes = ('0' + battery.remainingTimeMinutes).slice(-2)
console.log(`${battery.remainingTimeHours}:${remainingMinutes} remaining`)
}
}))
```## API
### `powerInfo`
#### `powerInfo.getDefault(): Promise`
Resolves to the default provider as soon as all providers were checked for
availability.#### `powerInfo.getSupportedProviders(): Promise`
Resolves to a list of all providers that are supported on the current system.#### `powerInfo.providers: Provider[]`
A list of all providers known to the module, disregarding if they are
supported or not.### `Provider`
#### `Provider.name: string`
The name of the provider.#### `Provider.getBatteries(): Promise`
Fetches information on all available batteries.#### `Provider.checkIsSupported(): Promise`
Resovles to true if the provider is available on the current system.### `Battery`
#### `Battery.id: string`
The id of the battery as provided by the OS.#### `Battery.powerLevel: number`
The power level of the battery in percent.#### `Battery.chargeStatus: string`
The battery's status. Either `'charging'`, `'discharging'`, `'full'` or
`'unknown'`.#### `Battery.isTimeAvailable: boolean`
True if a remaining time was set.#### `Battery.remainingTimeHours: number`
The remaining (dis-)charge time in whole hours, rounded down.
Is null if `isTimeAvailable` is `false`.#### `Battery.remainingTimeMinutes: number`
The remaining (dis-)charge time in minutes after subtracting the remaining
hours, rounded down.
Is null if `isTimeAvailable` is `false`.## Licence
MIT Licence (see LICENSE file)