Can use cURL to request fronius production

This commit is contained in:
Pierre HUBERT 2024-10-18 21:18:00 +02:00
parent ec594c0e4d
commit 45029f24cc
3 changed files with 25 additions and 7 deletions

View File

@ -39,8 +39,12 @@ pub enum ConsumptionBackend {
/// Fronius inverter consumption /// Fronius inverter consumption
Fronius { Fronius {
/// The origin of the domain where the webserver of the Fronius Symo can be reached /// The origin of the domain where the webserver of the Fronius Symo can be reached
#[clap(short, long, env = "FRONIUS_ORIG")] #[clap(short, long, env)]
origin: String, fronius_orig: String,
/// Use cURL instead of reqwest to perform request
#[clap(short, long)]
curl: bool,
}, },
} }

View File

@ -9,6 +9,8 @@ pub enum ConsumptionError {
NonExistentFile, NonExistentFile,
#[error("The file that should contain the consumption has an invalid content!")] #[error("The file that should contain the consumption has an invalid content!")]
FileInvalidContent(#[source] ParseIntError), FileInvalidContent(#[source] ParseIntError),
#[error("Failed to execute cURL request!")]
CurlReqFailed,
} }
pub type EnergyConsumption = i32; pub type EnergyConsumption = i32;
@ -63,9 +65,21 @@ pub async fn get_curr_consumption() -> anyhow::Result<EnergyConsumption> {
.map_err(ConsumptionError::FileInvalidContent)?) .map_err(ConsumptionError::FileInvalidContent)?)
} }
ConsumptionBackend::Fronius { origin } => { ConsumptionBackend::Fronius { fronius_orig, curl } => {
let url = format!("{origin}/solar_api/v1/GetPowerFlowRealtimeData.fcgi"); let url = format!("{fronius_orig}/solar_api/v1/GetPowerFlowRealtimeData.fcgi");
let response = reqwest::get(url).await?.json::<FroniusResponse>().await?;
let response = match curl {
false => reqwest::get(url).await?.json::<FroniusResponse>().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::<FroniusResponse>(&res.stdout)?
}
};
Ok(response.body.data.site.grid_production as i32) Ok(response.body.data.site.grid_production as i32)
} }

View File

@ -91,7 +91,7 @@ FRONIUS_ORIG=http://10.0.0.10
Run the following command to check if the configuration is working: Run the following command to check if the configuration is working:
```bash ```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 ### Create systemd unit file
@ -109,7 +109,7 @@ Type=simple
User=central User=central
Group=central Group=central
WorkingDirectory=/home/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 Restart=always
Environment=USER=central Environment=USER=central
HOME=/home/central HOME=/home/central