Compare commits
2 Commits
7b9db9c7c3
...
f594ebfbaa
| Author | SHA1 | Date | |
|---|---|---|---|
| f594ebfbaa | |||
| 57a9c03308 |
@@ -71,7 +71,11 @@ pub async fn get_curr_consumption() -> anyhow::Result<EnergyConsumption> {
|
|||||||
let response = match curl {
|
let response = match curl {
|
||||||
false => reqwest::get(url).await?.json::<FroniusResponse>().await?,
|
false => reqwest::get(url).await?.json::<FroniusResponse>().await?,
|
||||||
true => {
|
true => {
|
||||||
let res = std::process::Command::new("curl").arg(url).output()?;
|
let res = std::process::Command::new("curl")
|
||||||
|
.arg("--connect-timeout")
|
||||||
|
.arg("1.5")
|
||||||
|
.arg(url)
|
||||||
|
.output()?;
|
||||||
|
|
||||||
if !res.status.success() {
|
if !res.status.success() {
|
||||||
return Err(ConsumptionError::CurlReqFailed.into());
|
return Err(ConsumptionError::CurlReqFailed.into());
|
||||||
|
|||||||
@@ -25,7 +25,14 @@ impl EnergyActor {
|
|||||||
pub async fn new() -> anyhow::Result<Self> {
|
pub async fn new() -> anyhow::Result<Self> {
|
||||||
let consumption_cache_size =
|
let consumption_cache_size =
|
||||||
AppConfig::get().refresh_interval / AppConfig::get().energy_fetch_interval;
|
AppConfig::get().refresh_interval / AppConfig::get().energy_fetch_interval;
|
||||||
let curr_consumption = consumption::get_curr_consumption().await?;
|
let curr_consumption = match consumption::get_curr_consumption().await {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("Failed to fetch consumption, using default value! {e}");
|
||||||
|
constants::FALLBACK_PRODUCTION_VALUE
|
||||||
|
}
|
||||||
|
};
|
||||||
|
log::info!("Initial consumption value: {curr_consumption}");
|
||||||
let mut consumption_cache = ConsumptionCache::new(consumption_cache_size as usize);
|
let mut consumption_cache = ConsumptionCache::new(consumption_cache_size as usize);
|
||||||
consumption_cache.add_value(curr_consumption);
|
consumption_cache.add_value(curr_consumption);
|
||||||
|
|
||||||
|
|||||||
@@ -9,14 +9,20 @@ use actix_web::HttpResponse;
|
|||||||
|
|
||||||
#[derive(serde::Serialize)]
|
#[derive(serde::Serialize)]
|
||||||
struct Consumption {
|
struct Consumption {
|
||||||
consumption: i32,
|
consumption: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get current energy consumption
|
/// Get current energy consumption
|
||||||
pub async fn curr_consumption() -> HttpResult {
|
pub async fn curr_consumption() -> HttpResult {
|
||||||
let consumption = consumption::get_curr_consumption().await?;
|
Ok(match consumption::get_curr_consumption().await {
|
||||||
|
Ok(v) => HttpResponse::Ok().json(Consumption {
|
||||||
Ok(HttpResponse::Ok().json(Consumption { consumption }))
|
consumption: Some(v),
|
||||||
|
}),
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("Failed to fetch current consumption! {e}");
|
||||||
|
HttpResponse::Ok().json(Consumption { consumption: None })
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get curr consumption history
|
/// Get curr consumption history
|
||||||
@@ -34,7 +40,9 @@ pub async fn curr_consumption_history() -> HttpResult {
|
|||||||
pub async fn cached_consumption(energy_actor: WebEnergyActor) -> HttpResult {
|
pub async fn cached_consumption(energy_actor: WebEnergyActor) -> HttpResult {
|
||||||
let consumption = energy_actor.send(energy_actor::GetCurrConsumption).await?;
|
let consumption = energy_actor.send(energy_actor::GetCurrConsumption).await?;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().json(Consumption { consumption }))
|
Ok(HttpResponse::Ok().json(Consumption {
|
||||||
|
consumption: Some(consumption),
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get current relays consumption
|
/// Get current relays consumption
|
||||||
@@ -42,7 +50,9 @@ pub async fn relays_consumption(energy_actor: WebEnergyActor) -> HttpResult {
|
|||||||
let consumption =
|
let consumption =
|
||||||
energy_actor.send(energy_actor::RelaysConsumption).await? as EnergyConsumption;
|
energy_actor.send(energy_actor::RelaysConsumption).await? as EnergyConsumption;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().json(Consumption { consumption }))
|
Ok(HttpResponse::Ok().json(Consumption {
|
||||||
|
consumption: Some(consumption),
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn relays_consumption_history() -> HttpResult {
|
pub async fn relays_consumption_history() -> HttpResult {
|
||||||
|
|||||||
Reference in New Issue
Block a user