cmd-johnson / node-power-info

Cross-platform node library for accessing battery power information
https://www.npmjs.com/package/node-power-info
MIT License
3 stars 2 forks source link

Don't rely on platform specific console commands to get the power status #2

Open cmd-johnson opened 8 years ago

cmd-johnson commented 8 years ago

All three major platforms have native APIs for accessing information on the power supply. Put together platform dependent implementations (possibly in C/C++) that will be compiled on installation of the package, to reliably get the power information, without the fear of different paths or command outputs, as well as support for Windows platforms.

stevenengler commented 8 years ago

For Windows 10 with the Windows subsystem for Linux, an almost identical solution to the linux.js code can be used. But just waiting on the latest Windows insider build to make it to the stable Windows branch to fix a bug.

Would you be interested in a PR to add support for computers with Windows 10 and the Windows subsystem for Linux? Then you could wait for this Windows bug to get fixed before merging. Or did you plan on going ahead with the native module approach?

cmd-johnson commented 8 years ago

If the work to be done on the Windows 10 solution is as simple as you make it sound and you have some spare time on your hands you are willing to dedicate to working on this, please, go for it. I'm happy about every additional platform this module can work with.

In the long run, I don't think there's any way around the native implementation to provide support for all major platforms, but right now I'm pretty busy learning for university. Plus I'll need some time to get into the native build process in combination with node in order to begin with the actual implementation. I hope I find time to work on this in another month or so...

Anyway, thank you for your interest in improving this module!

[EDIT]: I'm still amazed that I'm unable to find a single cross-platform API (written in C/C++) to read battery status information. If I do this the native way, I might as well put the C/C++ implementation into its own repository and include it from here... The Windows API part looks pretty straight-forward (For general information, just use GetSystemPowerStatus, multi-battery systems might require a bit more work) On Linux... Well, the most common way to do it seems to just parse whatever there is to be found in /sys/class/power_supply (as this module already does), alternatively one could parse stdout of some command line tool or talk to upower via its D-Bus interface (or command line directly, still, parsing CLI output just doesn't sound like a very reliable thing to do). Since upower isn't something every Linux system has installed and the /sys/class/power_supply seems to be available on all modern Linux variations, I'll probably opt for the first (and easier) solution. I never wrote native code that had to run on OS X, but this might be a good point to start (didn't actually check out any of the links, though)

stevenengler commented 8 years ago

I'd really like to be able to use your Atom package on my Windows laptop, so happy to contribute some code. School is starting up for me again in September, so I've got a couple weeks before then.

With bash on Win 10, it's as simple as the command bash -c "cat /sys/class/power_supply/battery/capacity", which can easily replace the fs.readFile(...) using child_process.exec(...) instead. Since the files are small, cat works well. Anyways, I'll submit a PR in the next couple weeks, and if you're happy with it, feel free to merge it.

cmd-johnson commented 8 years ago

Awesome, thanks for the effort.

In addition to the simple check if you're on a Windows system, you'll probably also need to check if it's Windows 10 (or above), looks like this is the way to do it, a list of possible version numbers can be found here.

elaberge commented 7 years ago

For OSX, pmset code is available: https://opensource.apple.com/source/PowerManagement/PowerManagement-572.50.1/pmset/

cmd-johnson commented 7 years ago

Thanks for that link, judging from a quick look, it seems like all the information needed for this plugin on macOS can be obtained from calls to the IOKit framework.