svartalf / rust-battery

Rust crate providing cross-platform information about the notebook batteries.
https://crates.io/crates/battery
Apache License 2.0
354 stars 40 forks source link

Unable to access battery information #63

Closed mkoakintomiwa closed 4 years ago

mkoakintomiwa commented 4 years ago

Operating system: Windows

I got Unable to access battery information Error: Error { source: Kind(InvalidData), description: Some("Device rate value is unknown") } error: process didn't exit successfully:target\debug\battery.exe(exit code: 1) When i ran this:

extern crate battery;

use std::io;
use std::thread;
use std::time::Duration;

fn main() -> battery::Result<()> {
    let manager = battery::Manager::new()?;
    let mut battery = match manager.batteries()?.next() {
        Some(Ok(battery)) => battery,
        Some(Err(e)) => {
            eprintln!("Unable to access battery information");
            return Err(e);
        }
        None => {
            eprintln!("Unable to find any batteries");
            return Err(io::Error::from(io::ErrorKind::NotFound).into());
        }
    };

    loop {
        println!("{:?}", battery);
        thread::sleep(Duration::from_secs(1));
        manager.refresh(&mut battery)?;
    }
}
svartalf commented 4 years ago

Hi, @mkoakintomiwa! Thank you for the bug report!

I managed to write a fix for it, can you test it? It's in the git branch right now, so you need to temporary replace your battery dependency in Cargo.toml with the following dependency:

[dependencies]
battery = { git = "https://github.com/svartalf/rust-battery.git", branch = "issue-63" }

From what I understand, it can be reproduced when your laptop battery is fully charged and AC is plugged on, so your battery neither charging nor discharging.

svartalf commented 4 years ago

I'm going to assume that 0d7b9e4 fixes this bug, so this issue is closed now. Thank you once again for the bug report!