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

@ -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))
}