Bring PumoPM to a working state
PumoPM successfuly runs now, displays a notification on low battery, warning on very low battery, and suspends the computer on critical battery level.
This commit is contained in:
@@ -1,19 +1,23 @@
|
||||
use std::cmp::PartialOrd;
|
||||
|
||||
use notify_rust::{Hint, Notification, Urgency};
|
||||
|
||||
#[derive(PartialEq, PartialOrd, Eq, Debug)]
|
||||
pub enum VerbosityLevel {
|
||||
None = 0,
|
||||
Some = 1,
|
||||
Lots = 2,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BatteryState {
|
||||
manager: battery::Manager,
|
||||
battery: battery::Battery,
|
||||
refresh_rate: u64,
|
||||
|
||||
low_level: f32,
|
||||
very_low_level: f32,
|
||||
critical_level: f32,
|
||||
low_level: u8,
|
||||
very_low_level: u8,
|
||||
critical_level: u8,
|
||||
|
||||
is_triggered_low: bool,
|
||||
is_triggered_very_low: bool,
|
||||
@@ -21,16 +25,39 @@ pub struct BatteryState {
|
||||
verbosity: VerbosityLevel,
|
||||
}
|
||||
|
||||
pub const DEFAULT_LOW: f32 = 25_f32;
|
||||
pub const DEFAULT_VERY_LOW: f32 = 15_f32;
|
||||
pub const DEFAULT_CRITICAL: f32 = 10_f32;
|
||||
pub const DEFAULT_LOW: u8 = 25_u8;
|
||||
pub const DEFAULT_VERY_LOW: u8 = 15_u8;
|
||||
pub const DEFAULT_CRITICAL: u8 = 10_u8;
|
||||
pub const DEFAULT_REFRESH: u64 = 5;
|
||||
|
||||
macro_rules! trigger_warning {
|
||||
($message:literal, $urgency:ident, $trigger:expr, $battery:expr, $verbosity:expr, $self:expr) => {
|
||||
if $trigger {
|
||||
return;
|
||||
}
|
||||
$trigger = true;
|
||||
let level = ($battery.state_of_charge().value * 100_f32) as u8;
|
||||
let message = format!($message, $self.remaining_time($self.battery.time_to_full()), level);
|
||||
match $verbosity {
|
||||
VerbosityLevel::None => {}
|
||||
_ => println!("{}", message),
|
||||
}
|
||||
Notification::new()
|
||||
.summary("Low battery")
|
||||
.body(message.as_str())
|
||||
.hint(Hint::Category("battery".to_owned()))
|
||||
.urgency($urgency)
|
||||
.show()
|
||||
.unwrap();
|
||||
};
|
||||
}
|
||||
|
||||
impl BatteryState {
|
||||
/// Create and initialize new `BatteryState` object
|
||||
pub fn new(
|
||||
low_level: f32,
|
||||
mut very_low_level: f32,
|
||||
mut critical_level: f32,
|
||||
low_level: u8,
|
||||
mut very_low_level: u8,
|
||||
mut critical_level: u8,
|
||||
refresh_rate: u64,
|
||||
verbosity: VerbosityLevel,
|
||||
) -> battery::Result<Self> {
|
||||
@@ -48,14 +75,29 @@ impl BatteryState {
|
||||
}
|
||||
};
|
||||
|
||||
let low_level = low_level.min(50_f32).max(5_f32);
|
||||
// Keep values safe
|
||||
let low_level = low_level.min(95_u8).max(5_u8);
|
||||
if very_low_level > low_level {
|
||||
very_low_level = f32::max(low_level - 1_f32, 5_f32)
|
||||
very_low_level = u8::max(low_level - 1_u8, 5_u8)
|
||||
};
|
||||
if critical_level > very_low_level {
|
||||
critical_level = f32::max(very_low_level - 1_f32, 5_f32)
|
||||
critical_level = u8::max(very_low_level - 1_u8, 5_u8)
|
||||
};
|
||||
|
||||
match verbosity {
|
||||
VerbosityLevel::None => {}
|
||||
_ => {
|
||||
println!("Low battery: {}%", low_level);
|
||||
println!("Very low battery: {}%", very_low_level);
|
||||
println!("Critical battery: {}%", critical_level);
|
||||
println!("Refresh rate: {}s", refresh_rate);
|
||||
match verbosity {
|
||||
VerbosityLevel::Some => println!("Some verbose info"),
|
||||
_ => println!("Lots of verbose info"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
manager,
|
||||
battery,
|
||||
@@ -72,66 +114,127 @@ impl BatteryState {
|
||||
})
|
||||
}
|
||||
|
||||
fn get_charge(&self) -> f32 {
|
||||
self.battery.state_of_charge().value * 100_f32
|
||||
}
|
||||
|
||||
/// Reset current progress toward an empty battery
|
||||
fn reset_levels(&mut self) {
|
||||
self.is_triggered_low = false;
|
||||
self.is_triggered_very_low = false;
|
||||
}
|
||||
|
||||
fn trigger_low(&mut self) {
|
||||
if !self.is_triggered_low {
|
||||
self.is_triggered_low = true;
|
||||
let time = match self.battery.time_to_empty() {
|
||||
Some(e) => e.value.to_string(),
|
||||
None => String::from("unknown"),
|
||||
};
|
||||
let level = self.battery.state_of_charge().value * 100.0;
|
||||
let message =
|
||||
format!("Battery level is low! {} left ({}%)", time, level);
|
||||
Notification::new()
|
||||
.summary("Low Battery")
|
||||
.body(message.as_str())
|
||||
.hint(Hint::Category("battery".to_owned()))
|
||||
.urgency(Urgency::Normal)
|
||||
.show()
|
||||
/// Get level of charge of the battery
|
||||
fn get_charge(&self) -> u8 {
|
||||
(self.battery.state_of_charge().value * 100_f32) as u8
|
||||
}
|
||||
|
||||
pub fn remaining_time(&self, time: Option<battery::units::Time>) -> String {
|
||||
match time {
|
||||
Some(e) => {
|
||||
let time = e.value as u64;
|
||||
let time = time::Time::try_from_hms(
|
||||
(time / 3600) as u8,
|
||||
((time % 3600) / 60) as u8,
|
||||
(time % 60) as u8,
|
||||
)
|
||||
.unwrap();
|
||||
time.to_string()
|
||||
}
|
||||
None => {
|
||||
eprintln!("Couldn’t read remaining time");
|
||||
String::from("unknown remaining time")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Warn the user once about low battery
|
||||
fn trigger_low(&mut self) {
|
||||
use Urgency::Normal;
|
||||
trigger_warning!(
|
||||
"Battery level is low! {} left ({}%)",
|
||||
Normal,
|
||||
self.is_triggered_low,
|
||||
self.battery,
|
||||
self.verbosity,
|
||||
&self
|
||||
);
|
||||
}
|
||||
|
||||
/// Warn the user once about very low battery
|
||||
fn trigger_very_low(&mut self) {
|
||||
if !self.is_triggered_very_low {
|
||||
self.is_triggered_very_low = true;
|
||||
let time = match self.battery.time_to_empty() {
|
||||
Some(e) => e.value.to_string(),
|
||||
None => String::from("unknown"),
|
||||
};
|
||||
let level = self.battery.state_of_charge().value * 100.0;
|
||||
let message = format!(
|
||||
"Battery level is very low! {} left ({}%)",
|
||||
time, level
|
||||
);
|
||||
Notification::new()
|
||||
.summary("Low Battery")
|
||||
.body(message.as_str())
|
||||
.hint(Hint::Category("battery".to_owned()))
|
||||
.urgency(Urgency::Critical)
|
||||
.show()
|
||||
.unwrap();
|
||||
use Urgency::Critical;
|
||||
trigger_warning!(
|
||||
"Battery level is low! {} left ({}%)",
|
||||
Critical,
|
||||
self.is_triggered_very_low,
|
||||
self.battery,
|
||||
self.verbosity,
|
||||
&self
|
||||
);
|
||||
}
|
||||
|
||||
fn trigger_critical(&mut self) {
|
||||
use std::process::Command;
|
||||
let out = Command::new("systemctl")
|
||||
.arg("suspend")
|
||||
.output()
|
||||
.expect("process failed to execute");
|
||||
eprintln!("{}", out.status);
|
||||
eprintln!(
|
||||
"{}",
|
||||
String::from_utf8(out.stderr)
|
||||
.unwrap_or(String::from("Could not read stderr"))
|
||||
);
|
||||
eprintln!(
|
||||
"{:?}",
|
||||
String::from_utf8(out.stdout)
|
||||
.unwrap_or(String::from("Could not read stdin"))
|
||||
);
|
||||
loop {
|
||||
self.manager.refresh(&mut self.battery).unwrap();
|
||||
use std::{thread, time::Duration};
|
||||
thread::sleep(Duration::from_secs(self.refresh_rate));
|
||||
if self.battery.state() == battery::State::Charging {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
self.manager.refresh(&mut self.battery).unwrap();
|
||||
use battery::State::{Discharging, Empty};
|
||||
|
||||
let charge = self.get_charge();
|
||||
|
||||
if self.verbosity >= VerbosityLevel::Some {
|
||||
match self.battery.state() {
|
||||
battery::State::Charging => println!(
|
||||
"Charging: {}%, time left: {}",
|
||||
charge,
|
||||
self.remaining_time(self.battery.time_to_full())
|
||||
),
|
||||
battery::State::Discharging => println!(
|
||||
"Discharging: {}%, time left: {}",
|
||||
charge,
|
||||
self.remaining_time(self.battery.time_to_empty())
|
||||
),
|
||||
battery::State::Full => println!("Full"),
|
||||
battery::State::Empty => println!("Empty"),
|
||||
_ => eprintln!("Error: unknown battery state"),
|
||||
}
|
||||
}
|
||||
if self.verbosity == VerbosityLevel::Lots {
|
||||
eprintln!("====\nDebug self:\n{:?}\n====", self);
|
||||
}
|
||||
|
||||
match self.battery.state() {
|
||||
Discharging | Empty => match self.get_charge() {
|
||||
x if x < self.very_low_level => self.trigger_very_low(),
|
||||
x if x < self.low_level => self.trigger_low(),
|
||||
_ => {}
|
||||
},
|
||||
battery::State::Discharging | battery::State::Empty => {
|
||||
if !self.is_triggered_low && charge <= self.low_level {
|
||||
self.trigger_low();
|
||||
} else if !self.is_triggered_very_low
|
||||
&& charge <= self.very_low_level
|
||||
{
|
||||
self.trigger_very_low();
|
||||
} else if charge <= self.critical_level {
|
||||
self.trigger_critical();
|
||||
}
|
||||
}
|
||||
_ => self.reset_levels(),
|
||||
}
|
||||
|
||||
|
||||
15
src/main.rs
15
src/main.rs
@@ -11,13 +11,13 @@ use clap::Clap;
|
||||
)]
|
||||
struct Opts {
|
||||
#[clap(short, long, default_value = "25")]
|
||||
low: f32,
|
||||
low: u8,
|
||||
|
||||
#[clap(short = "L", long, default_value = "15")]
|
||||
very_low: f32,
|
||||
very_low: u8,
|
||||
|
||||
#[clap(short, long, default_value = "10")]
|
||||
critical: f32,
|
||||
critical: u8,
|
||||
|
||||
#[clap(short, long = "refresh-rate", default_value = "5")]
|
||||
refresh_rate: u64,
|
||||
@@ -28,15 +28,6 @@ struct Opts {
|
||||
|
||||
fn main() {
|
||||
let opts: Opts = Opts::parse();
|
||||
println!("Low battery: {}%", opts.low);
|
||||
println!("Very low battery: {}%", opts.very_low);
|
||||
println!("Critical battery: {}%", opts.critical);
|
||||
println!("Refresh rate: {}s", opts.refresh_rate);
|
||||
match opts.verbose {
|
||||
0 => println!("No verbose info"),
|
||||
1 => println!("Some verbose info"),
|
||||
_ => println!("Lots of verbose info"),
|
||||
}
|
||||
|
||||
let mut battery = BatteryState::new(
|
||||
opts.low,
|
||||
|
||||
Reference in New Issue
Block a user