changeset 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
files rust/Cargo.lock rust/Cargo.toml rust/src/config.rs rust/src/fridge.rs rust/src/main.rs
diffstat 5 files changed, 269 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/rust/Cargo.lock	Fri Jan 06 22:04:10 2017 +0800
+++ b/rust/Cargo.lock	Sat Jan 07 00:56:39 2017 +0800
@@ -2,12 +2,16 @@
 name = "wort-templog"
 version = "0.1.0"
 dependencies = [
+ "docopt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "docopt_macros 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "futures 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde 0.8.21 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_derive 0.8.21 (registry+https://github.com/rust-lang/crates.io-index)",
+ "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
  "tokio-core 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
@@ -21,6 +25,14 @@
 ]
 
 [[package]]
+name = "aho-corasick"
+version = "0.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "bitflags"
 version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -31,6 +43,25 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
+name = "docopt"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
+ "strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "docopt_macros"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "docopt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "env_logger"
 version = "0.3.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -57,6 +88,11 @@
 ]
 
 [[package]]
+name = "lazy_static"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
 name = "lazycell"
 version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -80,6 +116,14 @@
 ]
 
 [[package]]
+name = "memchr"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "mio"
 version = "0.6.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -157,11 +201,28 @@
 ]
 
 [[package]]
+name = "regex"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "aho-corasick 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "thread_local 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "regex-syntax"
 version = "0.3.9"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
+name = "regex-syntax"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
 name = "rustc-serialize"
 version = "0.3.22"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -221,6 +282,11 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
+name = "strsim"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
 name = "syn"
 version = "0.10.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -239,6 +305,15 @@
 ]
 
 [[package]]
+name = "thread-id"
+version = "3.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "thread_local"
 version = "0.2.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -247,6 +322,25 @@
 ]
 
 [[package]]
+name = "thread_local"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "time"
+version = "0.1.35"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "tokio-core"
 version = "0.1.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -273,11 +367,24 @@
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
+name = "unreachable"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "utf8-ranges"
 version = "0.1.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
+name = "utf8-ranges"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
 name = "void"
 version = "1.0.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -303,15 +410,20 @@
 
 [metadata]
 "checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
+"checksum aho-corasick 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4f660b942762979b56c9f07b4b36bb559776fbad102f05d6771e1b629e8fd5bf"
 "checksum bitflags 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dead7461c1127cf637931a1e50934eb6eee8bff2f74433ac7909e9afcee04a3"
 "checksum cfg-if 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de1e760d7b6535af4241fca8bd8adf68e2e7edacc6b29f5d399050c5e48cf88c"
+"checksum docopt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ab32ea6e284d87987066f21a9e809a73c14720571ef34516f0890b3d355ccfd8"
+"checksum docopt_macros 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac439b88ab0d3926c9852dbf49daa4ac40cc0cf229589ef12baa6394a5cd012f"
 "checksum env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "15abd780e45b3ea4f76b4e9a26ff4843258dd8a3eed2775a0e7368c2e7936c2f"
 "checksum futures 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "177a82a61dd7e528022ce97f24e54b499dd2fee4d4646a0f283c5fb500dbfe20"
 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
+"checksum lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6abe0ee2e758cd6bc8a2cd56726359007748fbf4128da998b65d0b70f881e19b"
 "checksum lazycell 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ce12306c4739d86ee97c23139f3a34ddf0387bbf181bc7929d287025a8c3ef6b"
 "checksum libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)" = "9e030dc72013ed68994d1b2cbf36a94dd0e58418ba949c4b0db7eeb70a7a6352"
 "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054"
 "checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
+"checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4"
 "checksum mio 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5b493dc9fd96bd2077f2117f178172b0765db4dfda3ea4d8000401e6d65d3e80"
 "checksum miow 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3e690c5df6b2f60acd45d56378981e827ff8295562fc8d34f573deb267a59cd1"
 "checksum net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)" = "5edf9cb6be97212423aed9413dd4729d62b370b5e1c571750e882cebbbc1e3e2"
@@ -319,7 +431,9 @@
 "checksum quote 0.3.10 (registry+https://github.com/rust-lang/crates.io-index)" = "6732e32663c9c271bfc7c1823486b471f18c47a2dbf87c066897b7b51afc83be"
 "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
 "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
+"checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01"
 "checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
+"checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457"
 "checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b"
 "checksum rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c5f5376ea5e30ce23c03eb77cbe4962b988deead10910c372b226388b594c084"
 "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d"
@@ -329,13 +443,19 @@
 "checksum serde_codegen_internals 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "afad7924a009f859f380e4a2e3a509a845c2ac66435fcead74a4d983b21ae806"
 "checksum serde_derive 0.8.21 (registry+https://github.com/rust-lang/crates.io-index)" = "6e7ad1e74679b92730ca39c361ea125e2846df337c5d94d084eb2f7837c1843d"
 "checksum slab 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b4fcaed89ab08ef143da37bc52adbcc04d4a69014f4c1208d6b51f0c47bc23"
+"checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694"
 "checksum syn 0.10.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1a437f8b4353179418870f014113876cd4cd4f642e42dbc5ed4f328d5f808246"
 "checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
+"checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a"
 "checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
+"checksum thread_local 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7793b722f0f77ce716e7f1acf416359ca32ff24d04ffbac4269f44a4a83be05d"
+"checksum time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "3c7ec6d62a20df54e07ab3b78b9a3932972f4b7981de295563686849eb3989af"
 "checksum tokio-core 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "52416b3e937abac22a543a7f1c66bd37feb60137ff1ab42390fa02df85347e58"
 "checksum toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "736b60249cb25337bc196faa43ee12c705e426f3d55c214d73a4e7be06f92cb4"
 "checksum unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "36dff09cafb4ec7c8cf0023eb0b686cb6ce65499116a12201c9e11840ca01beb"
+"checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91"
 "checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
+"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
 "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
 "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
 "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
--- a/rust/Cargo.toml	Fri Jan 06 22:04:10 2017 +0800
+++ b/rust/Cargo.toml	Sat Jan 07 00:56:39 2017 +0800
@@ -11,9 +11,10 @@
 rand = "0.3"
 log = "0.3"
 env_logger = "0.3"
-
-[features]
-testmode = []
+docopt = "0.7"
+docopt_macros = "0.7"
+rustc-serialize = "0.3"
+time = "0.1"
 
 [dependencies.toml]
 version = "0.2"
--- a/rust/src/config.rs	Fri Jan 06 22:04:10 2017 +0800
+++ b/rust/src/config.rs	Sat Jan 07 00:56:39 2017 +0800
@@ -5,7 +5,6 @@
 #[derive(Deserialize,Serialize,Debug,Clone)]
 #[allow(non_snake_case)]
 pub struct Config {
-    pub FRIDGE_SLEEP: u64,
     pub SENSOR_SLEEP: u64,
     pub UPLOAD_SLEEP: u64,
 
@@ -68,4 +67,35 @@
         self.serialize(&mut e).unwrap();
         toml::Value::Table(e.toml).to_string()
     }
+
+    /*
+    pub fn parse(&mut self, s: &str) {
+        let filename = "tempserver.conf";
+
+        let f = File::open(filename)
+            .map_err(|e| e.to_string())
+            .and_then(|mut f| {
+                let mut s = String::new();
+                f.read_to_string(&mut s)
+                    .map_err(|e| e.to_string())
+                    .map(|_| s)
+            })
+            .and_then(|s| {
+                    
+                        .map_err(|e| e.to_string())
+            });
+
+        match f {
+            Ok() => {
+
+            },
+            Err(e) => {
+                debug!("Error loading config file {}: {}",
+                    filename, e);
+
+            }
+        }
+
+    }
+    */
 }
--- a/rust/src/fridge.rs	Fri Jan 06 22:04:10 2017 +0800
+++ b/rust/src/fridge.rs	Sat Jan 07 00:56:39 2017 +0800
@@ -4,12 +4,13 @@
 use std;
 use std::io;
 use std::mem;
-use std::time::Duration;
+use std::time::{Duration,Instant};
 
 use futures::{Future,future,Sink,Stream};
 use tokio_core::reactor::{Timeout,Handle};
 use futures::sync::{mpsc};
 
+use config::Config;
 use types::*;
 
 #[derive(Debug)]
@@ -20,6 +21,7 @@
 }
 
 pub struct Fridge {
+    config: Config,
     params: Params,
     temp_wort: Option<f32>,
     temp_fridge: Option<f32>,
@@ -29,6 +31,7 @@
     timeout_s: mpsc::Sender<u64>,
     timeout_r: Option<mpsc::Receiver<u64>>,
     ticker: u64,
+    last_off_time: Instant,
 }
 
 impl Sink for Fridge {
@@ -48,9 +51,10 @@
 }
 
 impl Fridge {
-    pub fn new(p: Params, handle: &Handle) -> Fridge {
+    pub fn new(config: &Config, nowait: bool, p: Params, handle: &Handle) -> Fridge {
         let (s, r) = mpsc::channel(1);
         let mut f = Fridge { 
+            config: config.clone(),
             params: p,
             temp_wort: None,
             temp_fridge: None,
@@ -59,13 +63,23 @@
             timeout_s: s,
             timeout_r: Some(r),
             ticker: 0,
+            last_off_time: Instant::now(),
         };
+        if nowait {
+            f.last_off_time -= Duration::new(config.FRIDGE_DELAY, 100);
+        }
         f.tick();
         f
     }
 
-    /// Returns a stream of timeouts for fridge, waking when next necessary
-    pub fn timeouts(&mut self)
+    /// The fridge needs to periodically wake itself up, the returned
+    // stream of Tick messages does so.
+    /// Examples of wakeups events are
+    /// 
+    ///  * overshoot calculation
+    ///  * minimum fridge-off time
+    ///  * invalid wort timeout
+    pub fn wakeups(&mut self)
             -> Box<Stream<Item=Message, Error=io::Error>> {
         mem::replace(&mut self.timeout_r, None)
             .expect("NumberWatcher::timeouts() can only be called once")
--- 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);
+}
+