Can request consumption history

This commit is contained in:
2024-09-25 22:31:00 +02:00
parent 821b4644a2
commit 2e72634abf
3 changed files with 56 additions and 0 deletions

View File

@ -97,6 +97,30 @@ impl ConsumptionHistoryFile {
)?;
Ok(())
}
/// Get the total runtime of a relay during a given time window
pub fn get_history(
r#type: ConsumptionHistoryType,
from: u64,
to: u64,
interval: u64,
) -> anyhow::Result<Vec<EnergyConsumption>> {
let mut res = Vec::with_capacity(((to - from) / interval) as usize);
let mut file = Self::open(from, r#type)?;
let mut curr_time = from;
while curr_time < to {
if !file.contains_time(curr_time) {
file = Self::open(curr_time, r#type)?;
}
res.push(file.get_consumption(curr_time)?);
curr_time += interval;
}
Ok(res)
}
}
#[cfg(test)]