Start to implement energy consumption backend

This commit is contained in:
Pierre HUBERT 2024-06-29 10:11:31 +02:00
parent 9d3e2beb81
commit 49a3e3a669
8 changed files with 66 additions and 2 deletions

View File

@ -455,6 +455,7 @@ dependencies = [
"log",
"openssl",
"openssl-sys",
"rand",
"reqwest",
"serde",
"serde_json",

View File

@ -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"
serde_json = "1.0.118"
rand = "0.8.5"

View File

@ -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<ConsumptionBackend>,
}
lazy_static::lazy_static! {

View 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)),
}
}

View File

@ -0,0 +1 @@
pub mod consumption;

View File

@ -1,4 +1,5 @@
pub mod app_config;
pub mod crypto;
pub mod energy;
pub mod server;
pub mod utils;

View 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 }))
}

View File

@ -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()