Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to update model for battery #400

Merged
merged 6 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
Expand Down Expand Up @@ -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"),
}
Expand Down
19 changes: 10 additions & 9 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
17 changes: 3 additions & 14 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand Down
79 changes: 44 additions & 35 deletions src/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,29 @@ pub fn read_power_source() -> Result<bool, Error> {
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 {
Expand All @@ -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,
Expand All @@ -159,7 +169,7 @@ impl Battery {
Ok(obj)
}

pub fn read_charge(&mut self) -> Result<i8, Error> {
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)?;

Expand All @@ -169,24 +179,20 @@ impl Battery {
let charge = cap_str.parse::<i8>().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())
}
Expand All @@ -203,28 +209,24 @@ impl Battery {
}
}

pub fn get_condition(&mut self) -> Result<f32, Error> {
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> {
Expand Down Expand Up @@ -258,4 +260,11 @@ impl Battery {
self.charge_full = value.parse::<i32>().unwrap();
Ok(())
}

pub fn update(&mut self) -> Result<(), Error> {
self.get_condition()?;
self.read_charge()?;

Ok(())
}
}