diff --git a/central_backend/src/energy/consumption_history_file.rs b/central_backend/src/energy/consumption_history_file.rs index d4dc96b..f4f8757 100644 --- a/central_backend/src/energy/consumption_history_file.rs +++ b/central_backend/src/energy/consumption_history_file.rs @@ -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> { + 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)] diff --git a/central_backend/src/server/servers.rs b/central_backend/src/server/servers.rs index f6d67be..1dbced0 100644 --- a/central_backend/src/server/servers.rs +++ b/central_backend/src/server/servers.rs @@ -131,10 +131,18 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()> "/web_api/energy/curr_consumption", web::get().to(energy_controller::curr_consumption), ) + .route( + "/web_api/energy/curr_consumption/history", + web::get().to(energy_controller::curr_consumption_history), + ) .route( "/web_api/energy/cached_consumption", web::get().to(energy_controller::cached_consumption), ) + .route( + "/web_api/energy/relays_consumption/history", + web::get().to(energy_controller::relays_consumption_history), + ) // Devices controller .route( "/web_api/devices/list_pending", diff --git a/central_backend/src/server/web_api/energy_controller.rs b/central_backend/src/server/web_api/energy_controller.rs index 2ab729e..4147321 100644 --- a/central_backend/src/server/web_api/energy_controller.rs +++ b/central_backend/src/server/web_api/energy_controller.rs @@ -1,6 +1,9 @@ +use crate::app_config::ConsumptionHistoryType; +use crate::energy::consumption_history_file::ConsumptionHistoryFile; use crate::energy::{consumption, energy_actor}; use crate::server::custom_error::HttpResult; use crate::server::WebEnergyActor; +use crate::utils::time_utils::time_secs; use actix_web::HttpResponse; #[derive(serde::Serialize)] @@ -15,9 +18,30 @@ pub async fn curr_consumption() -> HttpResult { Ok(HttpResponse::Ok().json(Consumption { consumption })) } +/// Get curr consumption history +pub async fn curr_consumption_history() -> HttpResult { + let history = ConsumptionHistoryFile::get_history( + ConsumptionHistoryType::GridConsumption, + time_secs() - 3600 * 24, + time_secs(), + 60 * 10, + )?; + Ok(HttpResponse::Ok().json(history)) +} + /// Get cached energy consumption pub async fn cached_consumption(energy_actor: WebEnergyActor) -> HttpResult { let consumption = energy_actor.send(energy_actor::GetCurrConsumption).await?; Ok(HttpResponse::Ok().json(Consumption { consumption })) } + +pub async fn relays_consumption_history() -> HttpResult { + let history = ConsumptionHistoryFile::get_history( + ConsumptionHistoryType::RelayConsumption, + time_secs() - 3600 * 24, + time_secs(), + 60 * 10, + )?; + Ok(HttpResponse::Ok().json(history)) +}