diff options
| author | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-11-28 02:49:22 -0700 |
|---|---|---|
| committer | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-11-28 02:49:22 -0700 |
| commit | bc0da717eb33395984b214d18a374f4abcc7c55a (patch) | |
| tree | e5e127937d7bc29bbb33cbc7153775dbb7fccbc5 | |
| parent | 0980c1774d989ad861294f677d3bfe3da75112df (diff) | |
| download | varanus-bc0da717eb33395984b214d18a374f4abcc7c55a.tar.gz varanus-bc0da717eb33395984b214d18a374f4abcc7c55a.tar.bz2 varanus-bc0da717eb33395984b214d18a374f4abcc7c55a.zip | |
Getting current battery charge
| -rw-r--r-- | src/main.rs | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index 634f7ee..14b193d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,8 @@ use signal_hook::{consts::SIGINT,iterator::Signals}; mod forecast; +type Result<T> = std::result::Result<T, Box<dyn error::Error>>; + fn read_line<P: AsRef<Path>>(p: P) -> Result<String> { let s = read_to_string(p)?; Ok(s.lines().next().ok_or("")?.to_string()) @@ -94,14 +96,24 @@ struct Cli { #[derive(Debug, Serialize, Deserialize, Default)] struct Battery { path: PathBuf, + charge: f64, } impl Battery { fn new(p: &Path) -> Self { Battery { path: p.to_path_buf(), + charge: 1.0, } } + + fn update(&mut self) { + let cfp = self.path.join("charge_full"); + let cnp = self.path.join("charge_now"); + let cf: i32 = read_line(cfp).unwrap().parse().unwrap(); + let cn: i32 = read_line(cnp).unwrap().parse().unwrap(); + self.charge = cn as f64 / cf as f64 ; + } } #[derive(Debug, Serialize, Deserialize, Default)] @@ -135,6 +147,9 @@ impl Power { } fn update(&mut self) { + for bat in &mut self.batteries { + bat.update(); + } } } @@ -193,11 +208,10 @@ impl State { self.asof = SystemTime::now(); self.uptime = si.uptime; self.memory.update(si); + self.power.update(); } } -type Result<T> = std::result::Result<T, Box<dyn error::Error>>; - fn update_state(state: &mut State) { state.update(); } |
