Start to implement energy consumption backend
This commit is contained in:
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)),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user