diff options
| author | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-11-28 00:52:39 -0700 |
|---|---|---|
| committer | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-11-28 00:52:39 -0700 |
| commit | 0980c1774d989ad861294f677d3bfe3da75112df (patch) | |
| tree | 6c5b86ff05aaf08813c198a163db0fe498736f08 | |
| parent | 87298226d0033a2db699d5bb6b1e1dbd7960c04a (diff) | |
| download | varanus-0980c1774d989ad861294f677d3bfe3da75112df.tar.gz varanus-0980c1774d989ad861294f677d3bfe3da75112df.tar.bz2 varanus-0980c1774d989ad861294f677d3bfe3da75112df.zip | |
Listing batteries
| -rw-r--r-- | src/main.rs | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs index ccff659..634f7ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,11 +24,14 @@ * * Forecast * + * Handle "no socket file" more gracefully + * */ use std::error; -use std::fs::remove_file; +use std::fs::{read_dir,read_to_string,remove_file}; use std::io::{Read,Write}; +use std::path::{Path,PathBuf}; use std::os::unix::net::{UnixStream,UnixListener}; use std::str; use std::sync::{Arc,Mutex}; @@ -37,10 +40,15 @@ use std::time::{Duration,Instant,SystemTime}; use clap::Parser; use serde::{Serialize,Deserialize}; -use signal_hook::{consts::SIGINT, iterator::Signals}; +use signal_hook::{consts::SIGINT,iterator::Signals}; mod forecast; +fn read_line<P: AsRef<Path>>(p: P) -> Result<String> { + let s = read_to_string(p)?; + Ok(s.lines().next().ok_or("")?.to_string()) +} + #[derive(Default)] #[repr(C)] struct Sysinfo { @@ -83,17 +91,50 @@ struct Cli { socket: String, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Default)] struct Battery { + path: PathBuf, } -#[derive(Debug, Serialize, Deserialize)] +impl Battery { + fn new(p: &Path) -> Self { + Battery { + path: p.to_path_buf(), + } + } +} + +#[derive(Debug, Serialize, Deserialize, Default)] +struct Mains { + online: bool, +} + +#[derive(Debug, Serialize, Deserialize, Default)] struct Power { + batteries: Vec<Battery>, + mains: Vec<Mains>, } impl Power { fn new() -> Self { - return Power{} + let mut p = Power::default(); + for path in read_dir("/sys/class/power_supply").unwrap() { + match path { + Ok(de) => { + let typefile = de.path().join("type"); + let line = read_line(typefile).unwrap(); + if line == "Battery" { + let bat = Battery::new(&de.path()); + p.batteries.push(bat); + } + }, + Err(_) => (), + } + } + return p + } + + fn update(&mut self) { } } @@ -199,7 +240,7 @@ impl Listener { let listener = UnixListener::bind(sfn2).unwrap(); loop { match listener.accept() { - Ok((mut socket,accept)) => { + Ok((mut socket,_addr)) => { let json = { let state = state_mutex.lock().unwrap(); serde_json::to_string(&*state).unwrap() |
