diff --git a/central_backend/Cargo.toml b/central_backend/Cargo.toml index fc08e82..d5afc00 100644 --- a/central_backend/Cargo.toml +++ b/central_backend/Cargo.toml @@ -18,7 +18,7 @@ asn1 = "0.17" actix-web = { version = "4", features = ["openssl"] } futures = "0.3.30" serde = { version = "1.0.210", features = ["derive"] } -reqwest = "0.12.7" +reqwest = { version = "0.12.7", features = ["json"] } serde_json = "1.0.128" rand = "0.8.5" actix = "0.13.5" diff --git a/central_backend/src/app_config.rs b/central_backend/src/app_config.rs index 364bcbd..705429b 100644 --- a/central_backend/src/app_config.rs +++ b/central_backend/src/app_config.rs @@ -35,6 +35,13 @@ pub enum ConsumptionBackend { #[clap(short, long, default_value = "/dev/shm/consumption.txt")] path: String, }, + + /// Fronius inverter consumption + Fronius { + /// The origin of the domain where the webserver of the Fronius Symo can be reacher + #[clap(short, long)] + origin: String, + }, } /// Solar system central backend diff --git a/central_backend/src/energy/consumption.rs b/central_backend/src/energy/consumption.rs index b0173b0..ae7c0b3 100644 --- a/central_backend/src/energy/consumption.rs +++ b/central_backend/src/energy/consumption.rs @@ -13,6 +13,30 @@ pub enum ConsumptionError { pub type EnergyConsumption = i32; +#[derive(serde::Deserialize)] +struct FroniusResponse { + #[serde(rename = "Body")] + body: FroniusResponseBody, +} + +#[derive(serde::Deserialize)] +struct FroniusResponseBody { + #[serde(rename = "Data")] + data: FroniusResponseBodyData, +} + +#[derive(serde::Deserialize)] +struct FroniusResponseBodyData { + #[serde(rename = "Site")] + site: FroniusResponseSite, +} + +#[derive(serde::Deserialize)] +struct FroniusResponseSite { + #[serde(rename = "P_Grid")] + grid_production: f64, +} + /// Get current electrical energy consumption pub async fn get_curr_consumption() -> anyhow::Result { let backend = AppConfig::get() @@ -38,5 +62,12 @@ pub async fn get_curr_consumption() -> anyhow::Result { .parse() .map_err(ConsumptionError::FileInvalidContent)?) } + + ConsumptionBackend::Fronius { origin } => { + let url = format!("{origin}/solar_api/v1/GetPowerFlowRealtimeData.fcgi"); + let response = reqwest::get(url).await?.json::().await?; + + Ok(response.body.data.site.grid_production as i32) + } } }