diff --git a/central_backend/src/energy/relay_state_history.rs b/central_backend/src/energy/relay_state_history.rs index b8e6e6b..0b76a4e 100644 --- a/central_backend/src/energy/relay_state_history.rs +++ b/central_backend/src/energy/relay_state_history.rs @@ -1,7 +1,7 @@ use crate::app_config::AppConfig; use crate::devices::device::{DeviceRelay, DeviceRelayID}; use crate::utils::files_utils; -use crate::utils::time_utils::{day_number, time_start_of_day}; +use crate::utils::time_utils::{day_number, time_secs, time_start_of_day}; const TIME_INTERVAL: usize = 30; @@ -128,9 +128,19 @@ pub fn relay_total_runtime_adjusted(relay: &DeviceRelay) -> usize { .unwrap_or(0); let time_start_day = time_start_of_day().unwrap_or(1726696800); - let start_time = time_start_day + reset_time as u64; - let end_time = time_start_day + 3600 * 24 + reset_time as u64; - relay_total_runtime(relay.id, start_time, end_time).unwrap_or(3600 * 24) + + // Check if we have reached reset_time today yet or not + if time_start_day + reset_time as u64 >= time_secs() { + let start_time = time_start_day + reset_time as u64; + let end_time = time_start_day + 3600 * 24 + reset_time as u64; + relay_total_runtime(relay.id, start_time, end_time).unwrap_or(3600 * 24) + } else { + // If we have not reached reset time yet, we need to focus on previous day + let time_start_yesterday = time_start_day - 3600 * 24; + let start_time = time_start_yesterday + reset_time as u64; + let end_time = time_start_day + reset_time as u64; + relay_total_runtime(relay.id, start_time, end_time).unwrap_or(3600 * 24) + } } #[cfg(test)] diff --git a/central_backend/src/utils/time_utils.rs b/central_backend/src/utils/time_utils.rs index 90df441..66170a7 100644 --- a/central_backend/src/utils/time_utils.rs +++ b/central_backend/src/utils/time_utils.rs @@ -1,7 +1,7 @@ use chrono::prelude::*; use std::time::{SystemTime, UNIX_EPOCH}; -/// Get the current time since epoch +/// Get the current time since epoch, in seconds pub fn time_secs() -> u64 { SystemTime::now() .duration_since(UNIX_EPOCH)