Add more flexibility when fetching production value

This commit is contained in:
Pierre HUBERT 2024-10-19 15:39:30 +02:00
parent 7b9db9c7c3
commit 57a9c03308
2 changed files with 13 additions and 2 deletions

View File

@ -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());

View File

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