Mercurial > templog
changeset 635:4424a8b30f9c rust
config crate wants everything to be lower case
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 22 Sep 2019 22:06:46 +0800 |
parents | a5721c02d3ee |
children | 43eb3cfdf769 |
files | rust/src/config.rs rust/src/fridge.rs rust/src/main.rs rust/src/params.rs rust/src/sensor.rs |
diffstat | 5 files changed, 55 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/src/config.rs Sun Sep 22 20:35:40 2019 +0800 +++ b/rust/src/config.rs Sun Sep 22 22:06:46 2019 +0800 @@ -3,30 +3,29 @@ use super::types::*; #[derive(Deserialize,Serialize,Debug,Clone)] -#[allow(non_snake_case)] pub struct Config { - pub SENSOR_SLEEP: u64, - pub UPLOAD_SLEEP: u64, + pub sensor_sleep: u64, + pub upload_sleep: u64, - pub FRIDGE_DELAY: u64, - pub FRIDGE_WORT_INVALID_TIME: u64, + pub fridge_delay: u64, + pub fridge_wort_invalid_time: u64, - pub MAX_READINGS: u32, + pub max_readings: u32, - pub PARAMS_FILE: String, + pub params_file: String, - pub SENSOR_BASE_DIR: String, - pub FRIDGE_GPIO_PIN: u32, + pub sensor_base_dir: String, + pub fridge_gpio_pin: u32, - pub AMBIENT_NAME: String, - pub FRIDGE_NAME: String, - pub WORT_NAME: String, - pub INTERNAL_TEMPERATURE: String, + pub ambient_name: String, + pub fridge_name: String, + pub wort_name: String, + pub internal_temperature: String, - pub HMAC_KEY: String, - pub SERVER_URL: String, - pub UPDATE_URL: String, - pub SETTINGS_URL: String, + pub hmac_key: String, + pub server_url: String, + pub update_url: String, + pub settings_url: String, } impl Config { @@ -34,11 +33,17 @@ include_str!("defconfig.toml") } - pub fn load() -> Result<Self, TemplogError> { + pub fn load(conf_file: &str) -> Result<Self, TemplogError> { let mut c = config::Config::default(); - c.merge(config::File::from_str(Self::default_toml(), config::FileFormat::Toml)); - c.merge(config::File::with_name("local.conf")).unwrap(); + c.merge(config::File::from_str(Self::default_toml(), config::FileFormat::Toml)).expect("Bad default config"); + c.merge(config::File::with_name(conf_file)).or_else(|e| { + Err(match e { + config::ConfigError::NotFound(_) => TemplogError::new(&format!("Missing config {}", conf_file)), + _ => TemplogError::new(&format!("Problem parsing {}: {}", conf_file, e)), + }) + })?; c.merge(config::Environment::with_prefix("TEMPLOG")).unwrap(); + println!("c is {:?}", c); Ok(c.try_into().unwrap()) } }
--- a/rust/src/fridge.rs Sun Sep 22 20:35:40 2019 +0800 +++ b/rust/src/fridge.rs Sun Sep 22 22:06:46 2019 +0800 @@ -61,8 +61,8 @@ ctx: &Context<Self::Msg>, r: Readings, _sender: Sender) { - self.temp_wort = r.get_temp(&self.config.WORT_NAME); - self.temp_fridge = r.get_temp(&self.config.FRIDGE_NAME); + self.temp_wort = r.get_temp(&self.config.wort_name); + self.temp_fridge = r.get_temp(&self.config.fridge_name); if self.temp_wort.is_some() { self.wort_valid_time = Instant::now(); @@ -121,13 +121,13 @@ temp_wort: None, temp_fridge: None, last_off_time: Instant::now(), - wort_valid_time: Instant::now() - Duration::new(config.FRIDGE_WORT_INVALID_TIME, 100), + wort_valid_time: Instant::now() - Duration::new(config.fridge_wort_invalid_time, 100), integrator: StepIntegrator::new(Duration::new(1, 0)), control: Self::make_control(&config), }; if nowait { - f.last_off_time -= Duration::new(config.FRIDGE_DELAY, 1); + f.last_off_time -= Duration::new(config.fridge_delay, 1); } f @@ -135,7 +135,7 @@ #[cfg(target_os = "linux")] fn make_control(config: &Config) -> FridgeControl { - let mut pin = Pin(config.FRIDGE_GPIO_PIN); + let mut pin = Pin(config.fridge_gpio_pin); // XXX better error handling? pin.export().expect("Exporting fridge gpio failed"); pin.set_direction(Direction::Low).expect("Fridge gpio direction failed"); @@ -185,7 +185,7 @@ // Safety to avoid bad things happening to the fridge motor (?) // When it turns off don't start up again for at least FRIDGE_DELAY - if !self.on && off_time < Duration::new(self.config.FRIDGE_DELAY, 0) { + if !self.on && off_time < Duration::new(self.config.fridge_delay, 0) { info!("fridge skipping, too early"); return; } @@ -202,7 +202,7 @@ if self.temp_wort.is_none() { let invalid_time = Instant::now() - self.wort_valid_time; warn!("Invalid wort sensor for {:?} secs", invalid_time); - if invalid_time < Duration::new(self.config.FRIDGE_WORT_INVALID_TIME, 0) { + if invalid_time < Duration::new(self.config.fridge_wort_invalid_time, 0) { warn!("Has only been invalid for {:?}, waiting", invalid_time); return; }
--- a/rust/src/main.rs Sun Sep 22 20:35:40 2019 +0800 +++ b/rust/src/main.rs Sun Sep 22 22:06:46 2019 +0800 @@ -12,13 +12,16 @@ mod types; mod params; -use crate::config::Config; use riker::actors::*; use structopt::StructOpt; -fn run(cf: Config, nowait: bool, testmode: bool) { +use types::TemplogError; + +fn run(conf_file: &str, nowait: bool, testmode: bool) -> Result<(), TemplogError> { + + let cf = config::Config::load(conf_file)?; let sys = ActorSystem::new().unwrap(); let props = Props::new_args(params::ParamWaiter::new, cf.clone()); @@ -34,6 +37,7 @@ let props = Props::new_args(fridge::Fridge::new_actor, (cf.clone(), nowait)); sys.actor_of(props, "fridge").unwrap(); + Ok(()) } #[derive(Debug, StructOpt)] @@ -58,9 +62,13 @@ #[structopt(long)] nowait: bool, - /// Print default config (customise in local.conf) + /// Print default config (customise in local.toml) #[structopt(long)] defconf: bool, + + /// Config file + #[structopt(short = "c", long, default_value = "local.toml")] + config: String, } fn handle_args() -> Opt { @@ -68,7 +76,7 @@ if args.defconf { println!("Default configuration:\n{}\n\n{}", - "(custom options go in local.conf)", + "(custom options go in local.toml)", config::Config::default_toml()); std::process::exit(0); } @@ -103,7 +111,8 @@ info!("wort-templog"); debug!("debug mode"); - let config = config::Config::load().unwrap(); - - run(config, args.nowait, args.test); + let r = run(&args.config, args.nowait, args.test); + if let Err(e) = r { + println!("Error running: {}", e); + } }
--- a/rust/src/params.rs Sun Sep 22 20:35:40 2019 +0800 +++ b/rust/src/params.rs Sun Sep 22 22:06:46 2019 +0800 @@ -60,7 +60,7 @@ } pub fn load(config: &Config) -> Params { - Self::try_load(&config.PARAMS_FILE) + Self::try_load(&config.params_file) .unwrap_or_else(|_| Params::defaults()) } @@ -148,14 +148,14 @@ } fn write_params(&self, params: &Params) { - let p = atomicwrites::AtomicFile::new(&self.config.PARAMS_FILE, atomicwrites::AllowOverwrite); + let p = atomicwrites::AtomicFile::new(&self.config.params_file, atomicwrites::AllowOverwrite); p.write(|f| { serde_json::to_writer(f, params) }); } fn do_poll(&mut self, ctx: &Context<<Self as Actor>::Msg>) -> Result<(), TemplogError> { - let url = self.config.SETTINGS_URL.clone(); + let url = self.config.settings_url.clone(); let etag = self.last_etag.borrow().clone(); let h = ctx.run(async move { Self::wait_updates(&url, &etag).await
--- a/rust/src/sensor.rs Sun Sep 22 20:35:40 2019 +0800 +++ b/rust/src/sensor.rs Sun Sep 22 22:06:46 2019 +0800 @@ -37,7 +37,7 @@ fn pre_start(&mut self, ctx: &Context<Self::Msg>) { self.chan = Some(channel("readings", &ctx.system).unwrap()); - let dur = Duration::new(self.config.SENSOR_SLEEP,0); + let dur = Duration::new(self.config.sensor_sleep,0); ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading); } } @@ -71,7 +71,7 @@ // multiline static ref THERM_RE: regex::Regex = regex::Regex::new("(?m).* YES\n.*t=(.*)\n").unwrap(); } - let mut path = PathBuf::from(&self.config.SENSOR_BASE_DIR); + let mut path = PathBuf::from(&self.config.sensor_base_dir); path.push(n); path.push("w1_slave"); let mut s = String::new(); @@ -88,7 +88,7 @@ fn sensor_names(&self) -> Result<Vec<String>, TemplogError> { // TODO: needs to handle multiple busses. - let mut path = PathBuf::from(&self.config.SENSOR_BASE_DIR); + let mut path = PathBuf::from(&self.config.sensor_base_dir); path.push("w1_master_slaves"); let f = BufReader::new(File::open(path)?); @@ -109,7 +109,7 @@ fn pre_start(&mut self, ctx: &Context<Self::Msg>) { self.chan = Some(channel("readings", &ctx.system).unwrap()); - let dur = Duration::new(self.config.SENSOR_SLEEP,0); + let dur = Duration::new(self.config.sensor_sleep,0); ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading); } }