From 57a9c03308df19351d8f0330b53b63bbd3dbfb92 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 19 Oct 2024 15:39:30 +0200 Subject: [PATCH] Add more flexibility when fetching production value --- central_backend/src/energy/consumption.rs | 6 +++++- central_backend/src/energy/energy_actor.rs | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/central_backend/src/energy/consumption.rs b/central_backend/src/energy/consumption.rs index f9ae070..d24b6ca 100644 --- a/central_backend/src/energy/consumption.rs +++ b/central_backend/src/energy/consumption.rs @@ -71,7 +71,11 @@ pub async fn get_curr_consumption() -> anyhow::Result { let response = match curl { false => reqwest::get(url).await?.json::().await?, 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() { return Err(ConsumptionError::CurlReqFailed.into()); diff --git a/central_backend/src/energy/energy_actor.rs b/central_backend/src/energy/energy_actor.rs index 0f58c71..b586ecd 100644 --- a/central_backend/src/energy/energy_actor.rs +++ b/central_backend/src/energy/energy_actor.rs @@ -25,7 +25,14 @@ impl EnergyActor { pub async fn new() -> anyhow::Result { let consumption_cache_size = 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); consumption_cache.add_value(curr_consumption);