diff rust/src/main.rs @ 603:b45b8b4cf0f5 rust

get rid of lazy_static, config is passed around better use of threadpool for sensors readings are no longer options
author Matt Johnston <matt@ucc.asn.au>
date Thu, 16 Feb 2017 23:19:12 +0800
parents 8c21df3711e2
children 278f1002b5c7
line wrap: on
line diff
--- a/rust/src/main.rs	Thu Feb 16 23:17:51 2017 +0800
+++ b/rust/src/main.rs	Thu Feb 16 23:19:12 2017 +0800
@@ -7,9 +7,6 @@
 extern crate time;
 
 #[macro_use]
-extern crate lazy_static;
-
-#[macro_use]
 extern crate serde_derive;
 extern crate serde;
 
@@ -33,28 +30,32 @@
 use types::*;
 use config::Config;
 
-fn run(nowait: bool, testmode: bool) {
+fn run(config: &Config, 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(nowait, paramh.p, &handle);
+    let mut fridge = fridge::Fridge::new(&config, 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)
+        sensor::TestSensor::new(config).stream(&handle)
     } else {
-        sensor::OneWireSensor::stream(&handle)
+        sensor::OneWireSensor::new(config).stream(&handle)
     };
 
     // Send the sensors of interest to the fridge (fridge_reading_s),
     // while streaming them all to the web sender.
     let s = sensor_stream.map(|r| {
         debug!("sensors {:?}", r);
-        let t = fridge_reading_s.clone().send(fridge::Message::Sensor{wort: r.wort(), fridge: r.fridge()})
+        let msg = fridge::Message::Sensor {
+            wort: r.get_temp(&config.WORT_NAME), 
+            fridge: r.get_temp(&config.FRIDGE_NAME)
+        };
+        let t = fridge_reading_s.clone().send(msg)
                     .map(|_| ())
                     .map_err(|e| {
                         warn!("Send error in fridge_reading_s: {}", e.to_string());
@@ -139,7 +140,7 @@
 }
 
 fn load_config() -> Config {
-    let mut nconfig = config::Config::default();
+    let nconfig = config::Config::default();
 
     let conf_filename = "tempserver.conf";
     nconfig.merge_file(conf_filename)
@@ -149,10 +150,6 @@
     })
 }
 
-lazy_static! {
-    static ref rigid_config: Config = load_config();
-}
-
 fn main() {
 
     let args = handle_args();
@@ -162,14 +159,14 @@
     info!("wort-templog");
     debug!("debug mode");
 
+    let config = load_config();
+
     if args.flag_thisconf {
         println!("Current configuration:\n\n{}",
-            rigid_config.to_toml_string());
+            config.to_toml_string());
         std::process::exit(0);
     }
 
-
-
-    run(args.flag_nowait, args.flag_test);
+    run(&config, args.flag_nowait, args.flag_test);
 }