Skip to content

Commit

Permalink
Merge pull request #398 from Camerooooon/remove-hard-coded-path
Browse files Browse the repository at this point in the history
Remove hard coded paths in Battery logic
  • Loading branch information
Camerooooon authored Aug 17, 2022
2 parents 53b9598 + b176f24 commit acbea65
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result<Arc<Mutex<Daemo

// Create a new Daemon
let mut daemon: Daemon = Daemon {
battery: Battery::new(),
battery: Battery::new()?,
cpus: Vec::<CPU>::new(),
last_proc: Vec::<ProcStat>::new(),
message,
Expand Down
16 changes: 14 additions & 2 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand Down Expand Up @@ -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"),
Expand Down
44 changes: 24 additions & 20 deletions src/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand Down Expand Up @@ -130,22 +130,17 @@ pub struct Battery {
}

impl Battery {
pub fn new() -> Battery {
pub fn new() -> Result<Battery, Error> {
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<i8, Error> {
let path: &str = match get_best_path(BATTERY_CHARGE_PATH) {
Ok(path) => path,
Err(error) => {
Expand All @@ -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<i8, Error> {
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::<i8>().unwrap())
let charge = cap_str.parse::<i8>().unwrap();
self.capacity = charge;

Ok(charge)
}

pub fn print_status(&mut self, charging: bool) -> String {
Expand Down

0 comments on commit acbea65

Please sign in to comment.