SolarEnergy/central_backend/src/energy/consumption.rs

19 lines
573 B
Rust
Raw Normal View History

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