diff --git a/src/daemon.rs b/src/daemon.rs index f309895c..c5c57e8f 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -7,7 +7,6 @@ use nix::unistd::Uid; use super::config::Config; use super::cpu::{Speed, CPU}; -use super::debug; use super::graph::{Graph, Grapher}; use super::logger; use super::logger::Interface; @@ -80,11 +79,6 @@ pub struct Daemon { pub usage: f32, pub logger: logger::Logger, pub config: Config, - pub already_charging: bool, - pub already_closed: bool, - pub already_under_powersave_under_percent: bool, - pub already_high_temp: bool, - pub already_high_usage: bool, pub last_below_cpu_usage_percent: Option, pub state: State, pub graph: String, @@ -182,8 +176,21 @@ impl Checker for Daemon { fn run_state_machine(&mut self) -> Result { let mut state = State::Normal; - if self.usage > 70.0 { - state = State::CpuUsageHigh; + if self.usage > 70.0 && self.last_below_cpu_usage_percent.is_none() { + self.last_below_cpu_usage_percent = Some(SystemTime::now()); + } + + if self.usage <= 70.0 { + self.last_below_cpu_usage_percent = None; + } + + match self.last_below_cpu_usage_percent { + Some(last) => { + if SystemTime::now().duration_since(last)?.as_secs() >= 15 { + state = State::CpuUsageHigh; + } + } + None => {} } if self.lid_state == LidState::Closed { @@ -243,13 +250,6 @@ impl Checker for Daemon { self.timeout_battery = time::Duration::from_millis(self.settings.delay_battery); self.timeout = time::Duration::from_millis(self.settings.delay); - - // If we just daemonized then make sure the states are the opposite of what they should - // The logic after this block will make sure that they are set to the correct state - // but only if the previous states were incorrect - self.already_charging = !self.charging; - self.already_under_powersave_under_percent = !(self.charge < self.config.powersave_under); - self.already_closed = self.lid_state == LidState::Closed; } fn start_loop(&mut self) -> Result<(), Error> { @@ -530,11 +530,6 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result logs: Vec::::new(), }, config, - already_charging: false, - already_closed: false, - already_under_powersave_under_percent: false, - already_high_temp: false, - already_high_usage: false, last_below_cpu_usage_percent: None, graph: String::new(), grapher: Graph { freqs: vec![0.0] },