|  | @@ -3,26 +3,55 @@ use std::io::SeekFrom;
 | 
	
		
			
				|  |  |  use std::io::prelude::*;
 | 
	
		
			
				|  |  |  use super::Sensor;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +enum Status {
 | 
	
		
			
				|  |  | +    DISCHARGING,
 | 
	
		
			
				|  |  | +    CHARGING,
 | 
	
		
			
				|  |  | +    FULL
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  pub struct BatterySensor {
 | 
	
		
			
				|  |  | -    charge_now: File,
 | 
	
		
			
				|  |  | -    charge_full: File,
 | 
	
		
			
				|  |  | -    percentage: u32
 | 
	
		
			
				|  |  | +    now_file: File,
 | 
	
		
			
				|  |  | +    full_file: File,
 | 
	
		
			
				|  |  | +    status_file: File,
 | 
	
		
			
				|  |  | +    percentage: u32,
 | 
	
		
			
				|  |  | +    status: Status
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  impl BatterySensor {
 | 
	
		
			
				|  |  |      pub fn new(supply: &str) -> BatterySensor {
 | 
	
		
			
				|  |  |          let path = format!("/sys/class/power_supply/{}", supply);
 | 
	
		
			
				|  |  |          BatterySensor {
 | 
	
		
			
				|  |  | -            charge_now: File::open(format!("{}/{}", path, "charge_now")).unwrap(),
 | 
	
		
			
				|  |  | -            charge_full: File::open(format!("{}/{}", path, "charge_full")).unwrap(),
 | 
	
		
			
				|  |  | -            percentage: 0
 | 
	
		
			
				|  |  | +            now_file: File::open(format!("{}/{}", path, "charge_now")).unwrap(),
 | 
	
		
			
				|  |  | +            full_file: File::open(format!("{}/{}", path, "charge_full")).unwrap(),
 | 
	
		
			
				|  |  | +            status_file: File::open(format!("{}/{}", path, "status")).unwrap(),
 | 
	
		
			
				|  |  | +            percentage: 0,
 | 
	
		
			
				|  |  | +            status: Status::DISCHARGING
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    fn parse_status(s: &str) -> Status {
 | 
	
		
			
				|  |  | +        match s {
 | 
	
		
			
				|  |  | +            "Full" => Status::FULL,
 | 
	
		
			
				|  |  | +            "Charging" => Status::CHARGING,
 | 
	
		
			
				|  |  | +            _ => Status::DISCHARGING
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  impl Sensor for BatterySensor {
 | 
	
		
			
				|  |  |      fn icon(&self) -> String {
 | 
	
		
			
				|  |  | -        "".to_string()
 | 
	
		
			
				|  |  | +        match self.status {
 | 
	
		
			
				|  |  | +            Status::FULL => "",
 | 
	
		
			
				|  |  | +            Status::CHARGING => "",
 | 
	
		
			
				|  |  | +            Status::DISCHARGING => {
 | 
	
		
			
				|  |  | +                match (self.percentage+1) / 25 {
 | 
	
		
			
				|  |  | +                    0 => "",
 | 
	
		
			
				|  |  | +                    1 => "",
 | 
	
		
			
				|  |  | +                    2 => "",
 | 
	
		
			
				|  |  | +                    _ => ""
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }.to_string()
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      fn status(&self) -> String {
 | 
	
	
		
			
				|  | @@ -32,16 +61,22 @@ impl Sensor for BatterySensor {
 | 
	
		
			
				|  |  |      fn process(&mut self) {
 | 
	
		
			
				|  |  |          let mut s = String::new();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        self.charge_now.read_to_string(&mut s).ok().expect("Could not read current charge");
 | 
	
		
			
				|  |  | +        self.now_file.read_to_string(&mut s).ok().expect("Could not read current charge");
 | 
	
		
			
				|  |  |          let charge_now : u32 = s.trim().parse().ok().expect("Could not parse charge");
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          s.clear();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        self.charge_full.read_to_string(&mut s).ok().expect("Could not read current charge");
 | 
	
		
			
				|  |  | +        self.full_file.read_to_string(&mut s).ok().expect("Could not read current charge");
 | 
	
		
			
				|  |  |          let charge_full : u32 = s.trim().parse().ok().expect("Could not parse charge");
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        self.charge_now.seek(SeekFrom::Start(0)).ok();
 | 
	
		
			
				|  |  | -        self.charge_full.seek(SeekFrom::Start(0)).ok();
 | 
	
		
			
				|  |  | +        s.clear();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        self.status_file.read_to_string(&mut s).ok().expect("Could not read current charge");
 | 
	
		
			
				|  |  | +        self.status = BatterySensor::parse_status(s.trim());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        self.now_file.seek(SeekFrom::Start(0)).ok();
 | 
	
		
			
				|  |  | +        self.full_file.seek(SeekFrom::Start(0)).ok();
 | 
	
		
			
				|  |  | +        self.status_file.seek(SeekFrom::Start(0)).ok();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          self.percentage = charge_now / (charge_full / 100)
 | 
	
		
			
				|  |  |      }
 |