diff --git a/src/args.rs b/src/args.rs index 1135013c..6191530d 100644 --- a/src/args.rs +++ b/src/args.rs @@ -322,7 +322,7 @@ pub fn parse_args(config: config::Config) { match daemon_init(settings, config) { Ok(d) => { - daemon::run(d).unwrap_err(); + daemon::run(d).unwrap(); } Err(_) => eprint!("Could not run daemon in edit mode"), } @@ -374,7 +374,7 @@ pub fn parse_args(config: config::Config) { match daemon_init(settings, config) { Ok(d) => { - daemon::run(d).unwrap_err(); + daemon::run(d).unwrap(); } Err(_) => eprint!("Could not run daemon in monitor mode"), } diff --git a/src/daemon.rs b/src/daemon.rs index 812ccf3b..bfcd3c72 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -196,7 +196,7 @@ impl Checker for Daemon { // Update current states self.charging = read_power_source()?; - self.charge = self.battery.read_charge()?; + self.charge = self.battery.capacity; self.lid_state = read_lid_state()?; self.usage = calculate_average_usage(&self.cpus) * 100.0; @@ -240,7 +240,15 @@ impl Checker for Daemon { } /// Calls update on each cpu to update the state of each one + /// Also updates battery fn update_all(&mut self) -> Result<(), Error> { + match self.battery.update() { + Ok(_) => {} + Err(e) => self + .logger + .log(&format!("Battery error: {:?}", e), logger::Severity::Error), + } + let cur_proc = parse_proc_file(read_proc_stat_file()?)?; for cpu in self.cpus.iter_mut() { cpu.update()?; @@ -279,14 +287,7 @@ impl Checker for Daemon { // Prints battery percent or N/A if not let battery_status = self.battery.print_status(self.charging); - - let battery_condition: String; - match self.battery.get_condition() { - Ok(condition) => { - battery_condition = format!("Condition: {}%", condition); - } - Err(_) => battery_condition = "Condition: N/A".to_string(), - } + let battery_condition = format!("Condition: {}%", self.battery.condition); format!( "{}{}{}\n{}\n{}\n", diff --git a/src/display.rs b/src/display.rs index 8bf0d7d9..72e034a0 100644 --- a/src/display.rs +++ b/src/display.rs @@ -54,11 +54,11 @@ pub fn print_power(lid: LidState, bat: i8, plugged: bool, raw: bool) { } } -pub fn print_bat_cond(c: f32, raw: bool) { +pub fn print_bat_cond(c: i8, raw: bool) { if raw { println!("{}", c); } else { - println!("{:.2}%", c * 100.0) + println!("{:.2}%", c) } } diff --git a/src/interface.rs b/src/interface.rs index 8d42f66a..fabe9434 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -50,14 +50,6 @@ impl Getter for Get { } }; - let bat = match battery.read_charge() { - Ok(bat) => bat, - Err(_) => { - eprintln!("Failed to get read battery charger"); - return; - } - }; - let lid = match read_lid_state() { Ok(lid) => lid, Err(_) => { @@ -66,7 +58,7 @@ impl Getter for Get { } }; - print_power(lid, bat, plugged, raw); + print_power(lid, battery.capacity, plugged, raw); } fn usage(&self, raw: bool) { @@ -131,17 +123,14 @@ impl Getter for Get { } fn bat_cond(&self, raw: bool) { - let mut battery = match Battery::new() { + let 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"), - } + print_bat_cond(battery.condition, raw) } } diff --git a/src/power.rs b/src/power.rs index 33ce3387..154032d3 100644 --- a/src/power.rs +++ b/src/power.rs @@ -113,20 +113,29 @@ pub fn read_power_source() -> Result { Ok(pwr_str == "1") } -enum BatteryConditionType { +pub enum BatteryConditionType { Energy, Charge, None, } + +pub enum BatteryStatus { + Charging, + Discharging, + Full, + Unknown, +} + pub struct Battery { - sys_parent_path: String, - capacity: i8, - condition_type: BatteryConditionType, - condition: f32, - charge_full: i32, - charge_full_design: i32, - energy_full: i32, - energy_full_design: i32, + pub sys_parent_path: String, + pub capacity: i8, + pub condition_type: BatteryConditionType, + pub condition: i8, + pub charge_full: i32, + pub charge_full_design: i32, + pub energy_full: i32, + pub energy_full_design: i32, + pub status: BatteryStatus, } impl Battery { @@ -135,11 +144,12 @@ impl Battery { sys_parent_path: "unknown".to_string(), capacity: 0_i8, condition_type: BatteryConditionType::None, - condition: 0_f32, + condition: 0_i8, charge_full: 0_i32, charge_full_design: 0_i32, energy_full: 0_i32, energy_full_design: 0_i32, + status: BatteryStatus::Unknown, }; let path: &str = match get_best_path(BATTERY_CHARGE_PATH) { Ok(path) => path, @@ -159,7 +169,7 @@ impl Battery { Ok(obj) } - pub fn read_charge(&mut self) -> Result { + fn read_charge(&mut self) -> Result<(), Error> { let charge_path = self.sys_parent_path.to_string() + "capacity"; let mut cap_str = fs::read_to_string(charge_path)?; @@ -169,24 +179,20 @@ impl Battery { let charge = cap_str.parse::().unwrap(); self.capacity = charge; - Ok(charge) + Ok(()) } + // TODO: Move this to display.rs pub fn print_status(&mut self, charging: bool) -> String { if has_battery() { - match self.read_charge() { - Ok(bat) => { - format!( - "Battery: {}", - if charging { - format!("{}%", bat).green() - } else { - format!("{}%", bat).red() - }, - ) - } - Err(e) => format!("Battery charge could not be read\n{:?}", e), - } + format!( + "Battery: {}", + if charging { + format!("{}%", self.capacity).green() + } else { + format!("{}%", self.capacity).red() + }, + ) } else { format!("Battery: {}", "N/A".bold()) } @@ -203,28 +209,24 @@ impl Battery { } } - pub fn get_condition(&mut self) -> Result { + fn get_condition(&mut self) -> Result<(), Error> { match self.condition_type { BatteryConditionType::Energy => { self.read_energy_full()?; - self.condition = self.energy_full as f32 / self.energy_full_design as f32 + self.condition = + ((self.energy_full as f32 / self.energy_full_design as f32) * 100_f32) as i8 } BatteryConditionType::Charge => { self.read_charge_full()?; - self.condition = self.charge_full as f32 / self.charge_full_design as f32 + self.condition = + ((self.charge_full as f32 / self.charge_full_design as f32) * 100_f32) as i8 } BatteryConditionType::None => { return Err(Error::Unknown); } } - let mut bat_cond = self.condition * 100.0; - if bat_cond >= 100.0 { - bat_cond = 100.00; - } else if bat_cond <= 0.0 { - bat_cond = 0.0; - } - Ok(bat_cond.round()) + Ok(()) } fn read_energy_full(&mut self) -> Result<(), Error> { @@ -258,4 +260,11 @@ impl Battery { self.charge_full = value.parse::().unwrap(); Ok(()) } + + pub fn update(&mut self) -> Result<(), Error> { + self.get_condition()?; + self.read_charge()?; + + Ok(()) + } }