Start to implement energy consumption backend
This commit is contained in:
parent
9d3e2beb81
commit
49a3e3a669
1
central_backend/Cargo.lock
generated
1
central_backend/Cargo.lock
generated
@ -455,6 +455,7 @@ dependencies = [
|
|||||||
"log",
|
"log",
|
||||||
"openssl",
|
"openssl",
|
||||||
"openssl-sys",
|
"openssl-sys",
|
||||||
|
"rand",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
@ -19,4 +19,5 @@ actix-web = { version = "4", features = ["openssl"] }
|
|||||||
futures = "0.3.30"
|
futures = "0.3.30"
|
||||||
serde = { version = "1.0.203", features = ["derive"] }
|
serde = { version = "1.0.203", features = ["derive"] }
|
||||||
reqwest = "0.12.5"
|
reqwest = "0.12.5"
|
||||||
serde_json = "1.0.118"
|
serde_json = "1.0.118"
|
||||||
|
rand = "0.8.5"
|
@ -1,6 +1,24 @@
|
|||||||
use clap::Parser;
|
use clap::{Parser, Subcommand};
|
||||||
use std::path::{Path, PathBuf};
|
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
|
/// Solar system central backend
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
@ -20,6 +38,10 @@ pub struct AppConfig {
|
|||||||
/// Server storage path
|
/// Server storage path
|
||||||
#[arg(short, long, env, default_value = "storage")]
|
#[arg(short, long, env, default_value = "storage")]
|
||||||
storage: String,
|
storage: String,
|
||||||
|
|
||||||
|
/// Consumption backend provider
|
||||||
|
#[clap(subcommand)]
|
||||||
|
pub consumption_backend: Option<ConsumptionBackend>,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
|
18
central_backend/src/energy/consumption.rs
Normal file
18
central_backend/src/energy/consumption.rs
Normal file
@ -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<EnergyConsumption> {
|
||||||
|
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)),
|
||||||
|
}
|
||||||
|
}
|
1
central_backend/src/energy/mod.rs
Normal file
1
central_backend/src/energy/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod consumption;
|
@ -1,4 +1,5 @@
|
|||||||
pub mod app_config;
|
pub mod app_config;
|
||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
|
pub mod energy;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
15
central_backend/src/server/energy_controller.rs
Normal file
15
central_backend/src/server/energy_controller.rs
Normal file
@ -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 }))
|
||||||
|
}
|
@ -6,6 +6,7 @@ use crate::app_config::AppConfig;
|
|||||||
use crate::crypto::pki;
|
use crate::crypto::pki;
|
||||||
|
|
||||||
pub mod custom_error;
|
pub mod custom_error;
|
||||||
|
pub mod energy_controller;
|
||||||
pub mod pki_controller;
|
pub mod pki_controller;
|
||||||
pub mod server_controller;
|
pub mod server_controller;
|
||||||
|
|
||||||
@ -48,6 +49,10 @@ pub async fn secure_server() -> anyhow::Result<()> {
|
|||||||
App::new()
|
App::new()
|
||||||
.wrap(Logger::default())
|
.wrap(Logger::default())
|
||||||
.route("/", web::get().to(server_controller::secure_home))
|
.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)?
|
.bind_openssl(&AppConfig::get().listen_address, builder)?
|
||||||
.run()
|
.run()
|
||||||
|
Loading…
Reference in New Issue
Block a user