All checks were successful
continuous-integration/drone/push Build is passing
67 lines
2.0 KiB
Rust
67 lines
2.0 KiB
Rust
use crate::app_config::ConsumptionHistoryType;
|
|
use crate::energy::consumption::EnergyConsumption;
|
|
use crate::energy::consumption_history_file::ConsumptionHistoryFile;
|
|
use crate::energy::{consumption, energy_actor};
|
|
use crate::server::WebEnergyActor;
|
|
use crate::server::custom_error::HttpResult;
|
|
use crate::utils::time_utils::time_secs;
|
|
use actix_web::HttpResponse;
|
|
|
|
#[derive(serde::Serialize)]
|
|
struct Consumption {
|
|
consumption: Option<i32>,
|
|
}
|
|
|
|
/// Get current energy consumption
|
|
pub async fn curr_consumption() -> HttpResult {
|
|
Ok(match consumption::get_curr_consumption().await {
|
|
Ok(v) => HttpResponse::Ok().json(Consumption {
|
|
consumption: Some(v),
|
|
}),
|
|
Err(e) => {
|
|
log::error!("Failed to fetch current consumption! {e}");
|
|
HttpResponse::Ok().json(Consumption { consumption: None })
|
|
}
|
|
})
|
|
}
|
|
|
|
/// 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: Some(consumption),
|
|
}))
|
|
}
|
|
|
|
/// Get current relays consumption
|
|
pub async fn relays_consumption(energy_actor: WebEnergyActor) -> HttpResult {
|
|
let consumption =
|
|
energy_actor.send(energy_actor::RelaysConsumption).await? as EnergyConsumption;
|
|
|
|
Ok(HttpResponse::Ok().json(Consumption {
|
|
consumption: Some(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))
|
|
}
|