From f594ebfbaab822e0f7aa74bde734adf40db4e045 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 19 Oct 2024 15:49:10 +0200 Subject: [PATCH] Don't block home widget if live consumption is unavailable --- .../src/server/web_api/energy_controller.rs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/central_backend/src/server/web_api/energy_controller.rs b/central_backend/src/server/web_api/energy_controller.rs index 86c19ed..e6d280f 100644 --- a/central_backend/src/server/web_api/energy_controller.rs +++ b/central_backend/src/server/web_api/energy_controller.rs @@ -9,14 +9,20 @@ use actix_web::HttpResponse; #[derive(serde::Serialize)] struct Consumption { - consumption: i32, + consumption: Option, } /// Get current energy consumption pub async fn curr_consumption() -> HttpResult { - let consumption = consumption::get_curr_consumption().await?; - - Ok(HttpResponse::Ok().json(Consumption { consumption })) + 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 @@ -34,7 +40,9 @@ pub async fn curr_consumption_history() -> HttpResult { pub async fn cached_consumption(energy_actor: WebEnergyActor) -> HttpResult { 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 @@ -42,7 +50,9 @@ 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 })) + Ok(HttpResponse::Ok().json(Consumption { + consumption: Some(consumption), + })) } pub async fn relays_consumption_history() -> HttpResult {