diff --git a/central_backend/Cargo.lock b/central_backend/Cargo.lock index 64e0c01..29293f0 100644 --- a/central_backend/Cargo.lock +++ b/central_backend/Cargo.lock @@ -455,6 +455,7 @@ dependencies = [ "log", "openssl", "openssl-sys", + "rand", "reqwest", "serde", "serde_json", diff --git a/central_backend/Cargo.toml b/central_backend/Cargo.toml index fe0da93..4e6d07b 100644 --- a/central_backend/Cargo.toml +++ b/central_backend/Cargo.toml @@ -19,4 +19,5 @@ actix-web = { version = "4", features = ["openssl"] } futures = "0.3.30" serde = { version = "1.0.203", features = ["derive"] } reqwest = "0.12.5" -serde_json = "1.0.118" \ No newline at end of file +serde_json = "1.0.118" +rand = "0.8.5" \ No newline at end of file diff --git a/central_backend/src/app_config.rs b/central_backend/src/app_config.rs index 089dd82..809daff 100644 --- a/central_backend/src/app_config.rs +++ b/central_backend/src/app_config.rs @@ -1,6 +1,24 @@ -use clap::Parser; +use clap::{Parser, Subcommand}; use std::path::{Path, PathBuf}; +/// Electrical consumption fetcher backend +#[derive(Subcommand, Debug, Clone)] +pub enum ConsumptionBackend { + /// Constant consumption value + Constant { + #[clap(long, default_value_t = 500)] + value: i32, + }, + + /// Generate random consumption value + Random { + #[clap(long, default_value_t = -5000)] + min: i32, + #[clap(long, default_value_t = 20000)] + max: i32, + }, +} + /// Solar system central backend #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -20,6 +38,10 @@ pub struct AppConfig { /// Server storage path #[arg(short, long, env, default_value = "storage")] storage: String, + + /// Consumption backend provider + #[clap(subcommand)] + pub consumption_backend: Option, } lazy_static::lazy_static! { diff --git a/central_backend/src/energy/consumption.rs b/central_backend/src/energy/consumption.rs new file mode 100644 index 0000000..7f09a98 --- /dev/null +++ b/central_backend/src/energy/consumption.rs @@ -0,0 +1,18 @@ +use crate::app_config::{AppConfig, ConsumptionBackend}; +use rand::{thread_rng, Rng}; + +pub type EnergyConsumption = i32; + +/// Get current electrical energy consumption +pub async fn get_curr_consumption() -> anyhow::Result { + let backend = AppConfig::get() + .consumption_backend + .as_ref() + .unwrap_or(&ConsumptionBackend::Constant { value: 300 }); + + match backend { + ConsumptionBackend::Constant { value } => Ok(*value), + + ConsumptionBackend::Random { min, max } => Ok(thread_rng().gen_range(*min..*max)), + } +} diff --git a/central_backend/src/energy/mod.rs b/central_backend/src/energy/mod.rs new file mode 100644 index 0000000..f1682c6 --- /dev/null +++ b/central_backend/src/energy/mod.rs @@ -0,0 +1 @@ +pub mod consumption; diff --git a/central_backend/src/lib.rs b/central_backend/src/lib.rs index 7b2645a..dc205ba 100644 --- a/central_backend/src/lib.rs +++ b/central_backend/src/lib.rs @@ -1,4 +1,5 @@ pub mod app_config; pub mod crypto; +pub mod energy; pub mod server; pub mod utils; diff --git a/central_backend/src/server/energy_controller.rs b/central_backend/src/server/energy_controller.rs new file mode 100644 index 0000000..c7c4252 --- /dev/null +++ b/central_backend/src/server/energy_controller.rs @@ -0,0 +1,15 @@ +use crate::energy::consumption; +use crate::server::custom_error::HttpResult; +use actix_web::HttpResponse; + +#[derive(serde::Serialize)] +struct CurrConsumption { + consumption: i32, +} + +/// Get current energy consumption +pub async fn curr_consumption() -> HttpResult { + let consumption = consumption::get_curr_consumption().await?; + + Ok(HttpResponse::Ok().json(CurrConsumption { consumption })) +} diff --git a/central_backend/src/server/mod.rs b/central_backend/src/server/mod.rs index 8f0c6e1..8cfbadc 100644 --- a/central_backend/src/server/mod.rs +++ b/central_backend/src/server/mod.rs @@ -6,6 +6,7 @@ use crate::app_config::AppConfig; use crate::crypto::pki; pub mod custom_error; +pub mod energy_controller; pub mod pki_controller; pub mod server_controller; @@ -48,6 +49,10 @@ pub async fn secure_server() -> anyhow::Result<()> { App::new() .wrap(Logger::default()) .route("/", web::get().to(server_controller::secure_home)) + .route( + "/api/energy/curr_consumption", + web::get().to(energy_controller::curr_consumption), + ) }) .bind_openssl(&AppConfig::get().listen_address, builder)? .run()