1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

daemon: implement polling_interval

This commit is contained in:
RGBCube 2025-05-20 18:41:02 +03:00
parent 606cedb68a
commit 4fa59b7ed4
Signed by: RGBCube
SSH key fingerprint: SHA256:CzqbPcfwt+GxFYNnFVCqoN5Itn4YFrshg1TrnACpA5M

View file

@ -39,7 +39,11 @@ struct Daemon {
/// Power supply status log. /// Power supply status log.
power_supply_log: VecDeque<PowerSupplyLog>, power_supply_log: VecDeque<PowerSupplyLog>,
/// Whether if we are charging right now.
charging: bool, charging: bool,
/// The last computed polling interval.
last_polling_interval: Option<Duration>,
} }
struct CpuLog { struct CpuLog {
@ -167,7 +171,7 @@ impl Daemon {
} }
impl Daemon { impl Daemon {
fn polling_interval(&self) -> Duration { fn polling_interval(&mut self) -> Duration {
let mut interval = Duration::from_secs(5); let mut interval = Duration::from_secs(5);
// We are on battery, so we must be more conservative with our polling. // We are on battery, so we must be more conservative with our polling.
@ -185,6 +189,8 @@ impl Daemon {
} }
} }
// If we can't deterine the discharge rate, that means that
// we were very recently started. Which is user activity.
None => { None => {
interval *= 2; interval *= 2;
} }
@ -213,7 +219,20 @@ impl Daemon {
} }
} }
todo!("implement rest from daemon_old.rs") let interval = match self.last_polling_interval {
Some(last_interval) => Duration::from_secs_f64(
// 30% of current computed interval, 70% of last interval.
interval.as_secs_f64() * 0.3 + last_interval.as_secs_f64() * 0.7,
),
None => interval,
};
let interval = Duration::from_secs_f64(interval.as_secs_f64().clamp(1.0, 30.0));
self.last_polling_interval = Some(interval);
interval
} }
} }