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 {