From 45029f24cc2fe66a195c354a4e88a568e0957b5c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 18 Oct 2024 21:18:00 +0200 Subject: [PATCH] Can use cURL to request fronius production --- central_backend/src/app_config.rs | 8 ++++++-- central_backend/src/energy/consumption.rs | 20 +++++++++++++++++--- docs/SETUP_PROD.md | 4 ++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/central_backend/src/app_config.rs b/central_backend/src/app_config.rs index faecf0b..818e7d0 100644 --- a/central_backend/src/app_config.rs +++ b/central_backend/src/app_config.rs @@ -39,8 +39,12 @@ pub enum ConsumptionBackend { /// Fronius inverter consumption Fronius { /// The origin of the domain where the webserver of the Fronius Symo can be reached - #[clap(short, long, env = "FRONIUS_ORIG")] - origin: String, + #[clap(short, long, env)] + fronius_orig: String, + + /// Use cURL instead of reqwest to perform request + #[clap(short, long)] + curl: bool, }, } diff --git a/central_backend/src/energy/consumption.rs b/central_backend/src/energy/consumption.rs index ae7c0b3..f9ae070 100644 --- a/central_backend/src/energy/consumption.rs +++ b/central_backend/src/energy/consumption.rs @@ -9,6 +9,8 @@ pub enum ConsumptionError { NonExistentFile, #[error("The file that should contain the consumption has an invalid content!")] FileInvalidContent(#[source] ParseIntError), + #[error("Failed to execute cURL request!")] + CurlReqFailed, } pub type EnergyConsumption = i32; @@ -63,9 +65,21 @@ pub async fn get_curr_consumption() -> anyhow::Result { .map_err(ConsumptionError::FileInvalidContent)?) } - ConsumptionBackend::Fronius { origin } => { - let url = format!("{origin}/solar_api/v1/GetPowerFlowRealtimeData.fcgi"); - let response = reqwest::get(url).await?.json::().await?; + ConsumptionBackend::Fronius { fronius_orig, curl } => { + let url = format!("{fronius_orig}/solar_api/v1/GetPowerFlowRealtimeData.fcgi"); + + let response = match curl { + false => reqwest::get(url).await?.json::().await?, + true => { + let res = std::process::Command::new("curl").arg(url).output()?; + + if !res.status.success() { + return Err(ConsumptionError::CurlReqFailed.into()); + } + + serde_json::from_slice::(&res.stdout)? + } + }; Ok(response.body.data.site.grid_production as i32) } diff --git a/docs/SETUP_PROD.md b/docs/SETUP_PROD.md index b7233fa..91443de 100644 --- a/docs/SETUP_PROD.md +++ b/docs/SETUP_PROD.md @@ -91,7 +91,7 @@ FRONIUS_ORIG=http://10.0.0.10 Run the following command to check if the configuration is working: ```bash -sudo -u central central_backend -c /home/central/config.yaml +sudo -u central central_backend -c /home/central/config.yaml fronius -c ``` ### Create systemd unit file @@ -109,7 +109,7 @@ Type=simple User=central Group=central WorkingDirectory=/home/central -ExecStart=/usr/local/bin/central_backend -c /home/central/config.yaml +ExecStart=/usr/local/bin/central_backend -c /home/central/config.yaml fronius -c Restart=always Environment=USER=central HOME=/home/central