Skip to content

Commit

Permalink
✨ Make measurement rate configurable via env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
liamadamson committed Nov 25, 2023
1 parent 11bb7f0 commit b80e788
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
pub mod driver;

use driver::{get_bme280_driver, BME280Driver};
use std::env;
use std::thread::sleep;
use std::time::Duration;

const LOOP_RATE_S: u64 = 5 * 60;
const DEFAULT_MEASUREMENT_RATE_S: u64 = 5 * 60; // 5 minutes.

fn main() {
setup_logging();

log::info!("Application started");

let measurement_rate_s = get_measurement_rate();

let driver = get_bme280_driver().expect("Failed to get BME280 driver");
let mut runner = Runner::new(driver);

Expand All @@ -19,7 +22,32 @@ fn main() {
log::error!("Error steppinf runner: {}", e);
}

sleep(Duration::from_secs(LOOP_RATE_S));
sleep(Duration::from_secs(measurement_rate_s));
}
}

fn get_measurement_rate() -> u64 {
match env::var("MEASUREMENT_RATE_S") {
Ok(rate) => match rate.parse::<u64>() {
Ok(rate) => {
log::info!("Using MEASUREMENT_RATE_S of {}", rate);
rate
},
Err(_) => {
log::warn!(
"MEASUREMENT_RATE_S is not a valid integer, using default of {}",
DEFAULT_MEASUREMENT_RATE_S
);
DEFAULT_MEASUREMENT_RATE_S
}
},
Err(_) => {
log::warn!(
"MEASUREMENT_RATE_S not set, using default of {}",
DEFAULT_MEASUREMENT_RATE_S
);
DEFAULT_MEASUREMENT_RATE_S
}
}
}

Expand Down

0 comments on commit b80e788

Please sign in to comment.