diff rust/src/main.rs @ 597:a440eafa84a9 rust

progress for debug
author Matt Johnston <matt@ucc.asn.au>
date Sat, 07 Jan 2017 00:56:39 +0800
parents ca8102feaca6
children d4fbfb5c46ff
line wrap: on
line diff
--- a/rust/src/main.rs	Fri Jan 06 22:04:10 2017 +0800
+++ b/rust/src/main.rs	Sat Jan 07 00:56:39 2017 +0800
@@ -1,3 +1,6 @@
+#![feature(plugin)]
+#![plugin(docopt_macros)]
+
 #![feature(proc_macro)]
 
 extern crate tokio_core;
@@ -5,6 +8,8 @@
 #[macro_use]
 extern crate log;
 extern crate env_logger;
+extern crate rustc_serialize;
+extern crate time;
 
 #[macro_use]
 extern crate serde_derive;
@@ -12,6 +17,8 @@
 
 extern crate toml;
 
+extern crate docopt;
+
 
 use std::io;
 
@@ -22,44 +29,38 @@
 
 mod config;
 mod sensor;
-mod fridge;
+pub mod fridge;
 mod types;
 mod paramwaiter;
 
 use types::*;
-
-fn main() {
-    env_logger::init().unwrap();
+use config::Config;
 
-    println!("Wort Templog");
-    debug!("debug log level");
-
-    let config = config::Config::new();
-    println!("{}", config.to_toml_string());
+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(paramh.p, &handle);
+    let mut fridge = fridge::Fridge::new(config, nowait, paramh.p, &handle);
 
-    let (sensor_s, sensor_r) = mpsc::channel(1);
-    let sensor_r = sensor_r.map_err(|_| io::Error::new(io::ErrorKind::Other, "Problem with sensor_r channel"));
+    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 cfg!(feature = "testmode") {
-        sensor::TestSensor::stream(&handle, &config)
+    let sensor_stream = if testmode {
+        sensor::TestSensor::stream(&handle, config)
     } else {
-        sensor::OneWireSensor::stream(&handle, &config)
+        sensor::OneWireSensor::stream(&handle, config)
     };
 
-    // Send the sensors of interest to the fridge (sensor_s),
+    // 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 = sensor_s.clone().send(fridge::Message::Sensor{wort: r.wort(), fridge: r.fridge()})
+        let t = fridge_reading_s.clone().send(fridge::Message::Sensor{wort: r.wort(), fridge: r.fridge()})
                     .map(|_| ())
                     .map_err(|e| {
-                        warn!("Send error in sensor_s: {}", e.to_string());
+                        warn!("Send error in fridge_reading_s: {}", e.to_string());
                         ()
                     });
         handle.spawn(t);
@@ -71,10 +72,10 @@
             fridge::Message::Params(p)
         });
 
-    let timeouts = fridge.timeouts();
+    let timeouts = fridge.wakeups();
 
     let all_readings = s.for_each(|_| Ok(()));
-    let all_fridge = p.select(timeouts).select(sensor_r).forward(fridge)
+    let all_fridge = p.select(timeouts).select(fridge_reading_r).forward(fridge)
         .map(|_| () );
 
     let all = all_fridge.select(all_readings);
@@ -82,3 +83,78 @@
     core.run(all).ok();
 }
 
+docopt!(Args, "
+Wort Temperature
+Matt Johnston 2017 [email protected]
+Usage: wort-templog [--help] [--new] [--daemon] [--debug] [--test] [--defconf] [--nowait]
+
+Options:
+  -h, --help
+  --new         Replace existing running instance
+  -D, --daemon  Run in background
+  -d, --debug
+  -t, --test    Use fake sensors etc
+  --nowait      Skip initial fridge wait
+  --defconf     Print default config (customise in tempserver.conf)
+  --thisconf    Print used config
+");
+
+fn setup_log(debug: bool) {
+    let loglevel = if debug {
+       log::LogLevelFilter::Debug
+    } else {
+       log::LogLevelFilter::Info
+    };
+
+    let format = |record: &log::LogRecord| {
+        let datefmt = "%Y-%m-%d %I:%M:%S %p";
+        let ts = time::strftime(datefmt, &time::now()).unwrap();
+        format!("{}: {} - {}", ts, record.level(), record.args())
+    };
+
+
+    let mut builder = env_logger::LogBuilder::new();
+    builder.format(format).filter(Some("wort_templog"), loglevel);
+    builder.init().unwrap();
+}
+
+fn handle_args() -> Args {
+    let args: Args = Args::docopt().decode().unwrap_or_else(|e| e.exit());
+
+    if args.flag_defconf {
+        println!("Default configuration:\n{}\n\n{}",
+            "(custom options go in tempserver.conf)",
+            config::Config::new().to_toml_string());
+        std::process::exit(0);
+    }
+
+    args
+}
+
+fn load_config(config: &mut Config) {
+
+}
+
+fn main() {
+    let mut config = config::Config::new();
+
+    let args = handle_args();
+    setup_log(args.flag_debug);
+    //env_logger::init().unwrap();
+
+    info!("wort-templog");
+    debug!("debug mode");
+
+    load_config(&mut config);
+
+    let config = config;
+
+    if args.flag_thisconf {
+        println!("current configuration:\n\n{}",
+            config.to_toml_string());
+    }
+
+
+    run(&config, args.flag_nowait, args.flag_test);
+}
+