From b176f249a62ee799286239aec9bf10796453e6e3 Mon Sep 17 00:00:00 2001 From: Camerooooon Date: Tue, 16 Aug 2022 22:30:56 -0700 Subject: [PATCH] Remove hard coded paths in favor of multiple dynamic paths to support multiple systes --- src/daemon.rs | 2 +- src/interface.rs | 16 ++++++++++++++-- src/power.rs | 44 ++++++++++++++++++++++++-------------------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index aef217cf..812ccf3b 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -475,7 +475,7 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result::new(), last_proc: Vec::::new(), message, diff --git a/src/interface.rs b/src/interface.rs index 844b0221..8d42f66a 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -35,7 +35,13 @@ impl Getter for Get { } fn power(&self, raw: bool) { - let mut battery = Battery::new(); + let mut battery = match Battery::new() { + Ok(plugged) => plugged, + Err(_) => { + eprintln!("Failed to get battery"); + return; + } + }; let plugged = match read_power_source() { Ok(plugged) => plugged, Err(_) => { @@ -125,7 +131,13 @@ impl Getter for Get { } fn bat_cond(&self, raw: bool) { - let mut battery = Battery::new(); + let mut battery = match Battery::new() { + Ok(plugged) => plugged, + Err(_) => { + eprintln!("Failed to get battery"); + return; + } + }; match battery.get_condition() { Ok(bat_cond) => print_bat_cond(bat_cond, raw), Err(_) => println!("Failed to get battery condition"), diff --git a/src/power.rs b/src/power.rs index 1787903b..33ce3387 100644 --- a/src/power.rs +++ b/src/power.rs @@ -16,10 +16,10 @@ const LID_STATUS_PATH: [&str; 4] = [ ]; const BATTERY_CHARGE_PATH: [&str; 4] = [ - "/sys/class/power_supply/BAT/capacity", - "/sys/class/power_supply/BAT0/capacity", - "/sys/class/power_supply/BAT1/capacity", - "/sys/class/power_supply/BAT2/capacity", + "/sys/class/power_supply/BAT/", + "/sys/class/power_supply/BAT0/", + "/sys/class/power_supply/BAT1/", + "/sys/class/power_supply/BAT2/", ]; const POWER_SOURCE_PATH: [&str; 4] = [ @@ -130,22 +130,17 @@ pub struct Battery { } impl Battery { - pub fn new() -> Battery { + pub fn new() -> Result { let mut obj = Battery { - sys_parent_path: "/sys/class/power_supply/BAT0/".to_string(), - capacity: 0 as i8, + sys_parent_path: "unknown".to_string(), + capacity: 0_i8, condition_type: BatteryConditionType::None, - condition: 0 as f32, - charge_full: 0 as i32, - charge_full_design: 0 as i32, - energy_full: 0 as i32, - energy_full_design: 0 as i32, + condition: 0_f32, + charge_full: 0_i32, + charge_full_design: 0_i32, + energy_full: 0_i32, + energy_full_design: 0_i32, }; - obj.check_condition_type(); - obj - } - - pub fn read_charge(&mut self) -> Result { let path: &str = match get_best_path(BATTERY_CHARGE_PATH) { Ok(path) => path, Err(error) => { @@ -156,16 +151,25 @@ impl Battery { // If it doesn't exist then it is plugged in so make it 100% percent capacity eprintln!("We could not detect your battery."); create_issue!("If you are on a laptop"); - return Ok(100); + return Err(Error::Unknown); } }; + obj.sys_parent_path = path.to_string(); + obj.check_condition_type(); + Ok(obj) + } - let mut cap_str = fs::read_to_string(path)?; + pub fn read_charge(&mut self) -> Result { + let charge_path = self.sys_parent_path.to_string() + "capacity"; + let mut cap_str = fs::read_to_string(charge_path)?; // Remove the \n char cap_str.pop(); - Ok(cap_str.parse::().unwrap()) + let charge = cap_str.parse::().unwrap(); + self.capacity = charge; + + Ok(charge) } pub fn print_status(&mut self, charging: bool) -> String {