diff rust/src/main.rs @ 601:8c21df3711e2 rust

rigid_config more on sensors
author Matt Johnston <matt@ucc.asn.au>
date Wed, 15 Feb 2017 23:58:02 +0800
parents f71cf1ad745f
children b45b8b4cf0f5
line wrap: on
line diff
--- a/rust/src/main.rs	Tue Feb 07 22:57:29 2017 +0800
+++ b/rust/src/main.rs	Wed Feb 15 23:58:02 2017 +0800
@@ -7,6 +7,9 @@
 extern crate time;
 
 #[macro_use]
+extern crate lazy_static;
+
+#[macro_use]
 extern crate serde_derive;
 extern crate serde;
 
@@ -30,21 +33,21 @@
 use types::*;
 use config::Config;
 
-fn run(config: &Config, nowait: bool, testmode: bool) {
+fn run(nowait: bool, testmode: bool) {
 
     let mut core = Core::new().unwrap();
     let handle = core.handle();
 
     let mut paramh = ParamHolder::new();
-    let mut fridge = fridge::Fridge::new(config, nowait, paramh.p, &handle);
+    let mut fridge = fridge::Fridge::new(nowait, paramh.p, &handle);
 
     let (fridge_reading_s, fridge_reading_r) = mpsc::channel(1);
     let fridge_reading_r = fridge_reading_r.map_err(|_| io::Error::new(io::ErrorKind::Other, "Problem with fridge_reading_r channel"));
 
     let sensor_stream = if testmode {
-        sensor::TestSensor::stream(&handle, config)
+        sensor::TestSensor::stream(&handle)
     } else {
-        sensor::OneWireSensor::stream(&handle, config)
+        sensor::OneWireSensor::stream(&handle)
     };
 
     // Send the sensors of interest to the fridge (fridge_reading_s),
@@ -135,8 +138,22 @@
     args
 }
 
+fn load_config() -> Config {
+    let mut nconfig = config::Config::default();
+
+    let conf_filename = "tempserver.conf";
+    nconfig.merge_file(conf_filename)
+        .unwrap_or_else(|e| {
+            println!("Couldn't parse {}: {}", conf_filename, e);
+            std::process::exit(1);
+    })
+}
+
+lazy_static! {
+    static ref rigid_config: Config = load_config();
+}
+
 fn main() {
-    let mut config = config::Config::default();
 
     let args = handle_args();
     setup_log(args.flag_debug);
@@ -145,18 +162,14 @@
     info!("wort-templog");
     debug!("debug mode");
 
-    let conf_filename = "tempserver.conf";
-    config = config.merge_file(conf_filename)
-        .unwrap_or_else(|e| {
-            panic!("Couldn't parse {}: {}", conf_filename, e);
-    });
-
     if args.flag_thisconf {
         println!("Current configuration:\n\n{}",
-            config.to_toml_string());
+            rigid_config.to_toml_string());
         std::process::exit(0);
     }
 
-    run(&config, args.flag_nowait, args.flag_test);
+
+
+    run(args.flag_nowait, args.flag_test);
 }