From 0f06ac2286397082228f32fc5f9642778cf84d72 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 14 May 2018 20:15:39 -0400 Subject: factor out os-specific battery detection code stub out osx for now, will come back with a real implementation later --- src/power.rs | 139 ----------------------------------------------------------- 1 file changed, 139 deletions(-) delete mode 100644 src/power.rs (limited to 'src/power.rs') diff --git a/src/power.rs b/src/power.rs deleted file mode 100644 index 6ae762c..0000000 --- a/src/power.rs +++ /dev/null @@ -1,139 +0,0 @@ -use std; -use walkdir; - -use std::io::Read; - -// XXX maybe extract this out into a separate crate? - -#[derive(PartialEq, Eq, Debug, Clone)] -enum PowerSupplyType { - AC, - Battery, -} - -#[derive(Debug)] -pub struct PowerInfo { - power_supplies: Vec, -} - -#[derive(Debug, Clone)] -struct PowerSupplyInfo { - name: String, - ty: PowerSupplyType, - energy_now: Option, - energy_full: Option, - online: Option, -} - -impl PowerInfo { - pub fn new() -> PowerInfo { - let mut power_supplies = vec![]; - for entry in walkdir::WalkDir::new("/sys/class/power_supply/") - .min_depth(1) - .max_depth(1) - .follow_links(true) - { - let entry = entry.unwrap(); - - let name = entry - .path() - .file_name() - .unwrap() - .to_string_lossy() - .into_owned(); - let ty = slurp(entry.path().join("type")) - .map(|t: String| PowerSupplyType::from_str(&t)) - .expect("couldn't find power supply type"); - let energy_full = slurp(entry.path().join("energy_full")); - let energy_now = slurp(entry.path().join("energy_now")); - let online = - slurp(entry.path().join("online")).map(|n: u8| n != 0); - - power_supplies.push(PowerSupplyInfo { - name, - ty, - energy_now, - energy_full, - online, - }) - } - - PowerInfo { - power_supplies, - } - } - - pub fn battery_usage(&self) -> Option { - let mut total_now = 0; - let mut total_full = 0; - for battery in self.batteries() { - if let Some(now) = battery.energy_now { - total_now += now; - } - else { - return None; - } - if let Some(full) = battery.energy_full { - total_full += full; - } - else { - return None; - } - } - - if total_full > 0 { - Some((total_now as f64) / (total_full as f64)) - } - else { - None - } - } - - pub fn charging(&self) -> bool { - for mains in self.mains() { - if mains.online == Some(true) { - return true; - } - } - false - } - - pub fn has_batteries(&self) -> bool { - self.batteries().count() > 0 - } - - fn batteries(&self) -> impl Iterator { - self.power_supplies - .iter() - .filter(|p| p.ty == PowerSupplyType::Battery) - } - - fn mains(&self) -> impl Iterator { - self.power_supplies - .iter() - .filter(|p| p.ty == PowerSupplyType::AC) - } -} - -impl PowerSupplyType { - fn from_str(ty: &str) -> Self { - match ty { - "Mains" => PowerSupplyType::AC, - "Battery" => PowerSupplyType::Battery, - _ => panic!("unknown power supply type {}", ty), - } - } -} - -fn slurp(path: U) -> Option -where - T: std::str::FromStr, - U: AsRef, -{ - let mut contents = String::new(); - std::fs::File::open(path).ok().and_then(|mut fh| { - fh.read_to_string(&mut contents) - .ok() - .and_then(|_| contents.trim().parse().ok()) - }) -} -- cgit v1.2.3-54-g00ecf