Can count the time a relay was up during a given amount of time

This commit is contained in:
Pierre HUBERT 2024-09-18 22:05:23 +02:00
parent 92878e6548
commit 09c25a67c5

View File

@ -64,6 +64,11 @@ impl RelayStateHistory {
Ok(((relative_time / 8) as usize, (relative_time % 8) as u8)) Ok(((relative_time / 8) as usize, (relative_time % 8) as u8))
} }
/// Check if a time is contained in this history
pub fn contains_time(&self, time: u64) -> bool {
self.resolve_offset(time).is_ok()
}
/// Set new state of relay /// Set new state of relay
pub fn set_state(&mut self, time: u64, on: bool) -> anyhow::Result<()> { pub fn set_state(&mut self, time: u64, on: bool) -> anyhow::Result<()> {
let (idx, offset) = self.resolve_offset(time)?; let (idx, offset) = self.resolve_offset(time)?;
@ -93,10 +98,31 @@ impl RelayStateHistory {
} }
} }
/// Get the total runtime of a relay during a given time window
pub fn relay_total_runtime(device_id: DeviceRelayID, from: u64, to: u64) -> anyhow::Result<usize> {
let mut total = 0;
let mut file = RelayStateHistory::open(device_id, from)?;
let mut curr_time = from;
while curr_time < to {
if !file.contains_time(curr_time) {
file = RelayStateHistory::open(device_id, curr_time)?;
}
if file.get_state(curr_time)? {
total += TIME_INTERVAL;
}
curr_time += TIME_INTERVAL as u64;
}
Ok(total)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::devices::device::DeviceRelayID; use crate::devices::device::DeviceRelayID;
use crate::energy::relay_state_history::RelayStateHistory; use crate::energy::relay_state_history::{relay_total_runtime, RelayStateHistory};
#[test] #[test]
fn test_relay_state_history() { fn test_relay_state_history() {
@ -138,4 +164,12 @@ mod tests {
assert!(history.get_state(8989898).is_err()); assert!(history.get_state(8989898).is_err());
} }
#[test]
fn test_relay_total_runtime() {
assert_eq!(
relay_total_runtime(DeviceRelayID::default(), 50, 3600 * 24 * 60 + 500).unwrap(),
0
);
}
} }