Mercurial > templog
comparison 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 |
comparison
equal
deleted
inserted
replaced
596:ca8102feaca6 | 597:a440eafa84a9 |
---|---|
1 #![feature(plugin)] | |
2 #![plugin(docopt_macros)] | |
3 | |
1 #![feature(proc_macro)] | 4 #![feature(proc_macro)] |
2 | 5 |
3 extern crate tokio_core; | 6 extern crate tokio_core; |
4 extern crate futures; | 7 extern crate futures; |
5 #[macro_use] | 8 #[macro_use] |
6 extern crate log; | 9 extern crate log; |
7 extern crate env_logger; | 10 extern crate env_logger; |
11 extern crate rustc_serialize; | |
12 extern crate time; | |
8 | 13 |
9 #[macro_use] | 14 #[macro_use] |
10 extern crate serde_derive; | 15 extern crate serde_derive; |
11 extern crate serde; | 16 extern crate serde; |
12 | 17 |
13 extern crate toml; | 18 extern crate toml; |
19 | |
20 extern crate docopt; | |
14 | 21 |
15 | 22 |
16 use std::io; | 23 use std::io; |
17 | 24 |
18 use tokio_core::reactor::Core; | 25 use tokio_core::reactor::Core; |
20 use futures::sync::{mpsc}; | 27 use futures::sync::{mpsc}; |
21 use sensor::Sensor; | 28 use sensor::Sensor; |
22 | 29 |
23 mod config; | 30 mod config; |
24 mod sensor; | 31 mod sensor; |
25 mod fridge; | 32 pub mod fridge; |
26 mod types; | 33 mod types; |
27 mod paramwaiter; | 34 mod paramwaiter; |
28 | 35 |
29 use types::*; | 36 use types::*; |
37 use config::Config; | |
30 | 38 |
31 fn main() { | 39 fn run(config: &Config, nowait: bool, testmode: bool) { |
32 env_logger::init().unwrap(); | |
33 | |
34 println!("Wort Templog"); | |
35 debug!("debug log level"); | |
36 | |
37 let config = config::Config::new(); | |
38 println!("{}", config.to_toml_string()); | |
39 | 40 |
40 let mut core = Core::new().unwrap(); | 41 let mut core = Core::new().unwrap(); |
41 let handle = core.handle(); | 42 let handle = core.handle(); |
42 | 43 |
43 let mut paramh = ParamHolder::new(); | 44 let mut paramh = ParamHolder::new(); |
44 let mut fridge = fridge::Fridge::new(paramh.p, &handle); | 45 let mut fridge = fridge::Fridge::new(config, nowait, paramh.p, &handle); |
45 | 46 |
46 let (sensor_s, sensor_r) = mpsc::channel(1); | 47 let (fridge_reading_s, fridge_reading_r) = mpsc::channel(1); |
47 let sensor_r = sensor_r.map_err(|_| io::Error::new(io::ErrorKind::Other, "Problem with sensor_r channel")); | 48 let fridge_reading_r = fridge_reading_r.map_err(|_| io::Error::new(io::ErrorKind::Other, "Problem with fridge_reading_r channel")); |
48 | 49 |
49 let sensor_stream = if cfg!(feature = "testmode") { | 50 let sensor_stream = if testmode { |
50 sensor::TestSensor::stream(&handle, &config) | 51 sensor::TestSensor::stream(&handle, config) |
51 } else { | 52 } else { |
52 sensor::OneWireSensor::stream(&handle, &config) | 53 sensor::OneWireSensor::stream(&handle, config) |
53 }; | 54 }; |
54 | 55 |
55 // Send the sensors of interest to the fridge (sensor_s), | 56 // Send the sensors of interest to the fridge (fridge_reading_s), |
56 // while streaming them all to the web sender. | 57 // while streaming them all to the web sender. |
57 let s = sensor_stream.map(|r| { | 58 let s = sensor_stream.map(|r| { |
58 debug!("sensors {:?}", r); | 59 debug!("sensors {:?}", r); |
59 let t = sensor_s.clone().send(fridge::Message::Sensor{wort: r.wort(), fridge: r.fridge()}) | 60 let t = fridge_reading_s.clone().send(fridge::Message::Sensor{wort: r.wort(), fridge: r.fridge()}) |
60 .map(|_| ()) | 61 .map(|_| ()) |
61 .map_err(|e| { | 62 .map_err(|e| { |
62 warn!("Send error in sensor_s: {}", e.to_string()); | 63 warn!("Send error in fridge_reading_s: {}", e.to_string()); |
63 () | 64 () |
64 }); | 65 }); |
65 handle.spawn(t); | 66 handle.spawn(t); |
66 r | 67 r |
67 }); | 68 }); |
69 let param_stream = paramwaiter::ParamWaiter::stream(&handle); | 70 let param_stream = paramwaiter::ParamWaiter::stream(&handle); |
70 let p = param_stream.map(|p| { | 71 let p = param_stream.map(|p| { |
71 fridge::Message::Params(p) | 72 fridge::Message::Params(p) |
72 }); | 73 }); |
73 | 74 |
74 let timeouts = fridge.timeouts(); | 75 let timeouts = fridge.wakeups(); |
75 | 76 |
76 let all_readings = s.for_each(|_| Ok(())); | 77 let all_readings = s.for_each(|_| Ok(())); |
77 let all_fridge = p.select(timeouts).select(sensor_r).forward(fridge) | 78 let all_fridge = p.select(timeouts).select(fridge_reading_r).forward(fridge) |
78 .map(|_| () ); | 79 .map(|_| () ); |
79 | 80 |
80 let all = all_fridge.select(all_readings); | 81 let all = all_fridge.select(all_readings); |
81 | 82 |
82 core.run(all).ok(); | 83 core.run(all).ok(); |
83 } | 84 } |
84 | 85 |
86 docopt!(Args, " | |
87 Wort Temperature | |
88 Matt Johnston 2017 [email protected] | |
89 Usage: wort-templog [--help] [--new] [--daemon] [--debug] [--test] [--defconf] [--nowait] | |
90 | |
91 Options: | |
92 -h, --help | |
93 --new Replace existing running instance | |
94 -D, --daemon Run in background | |
95 -d, --debug | |
96 -t, --test Use fake sensors etc | |
97 --nowait Skip initial fridge wait | |
98 --defconf Print default config (customise in tempserver.conf) | |
99 --thisconf Print used config | |
100 "); | |
101 | |
102 fn setup_log(debug: bool) { | |
103 let loglevel = if debug { | |
104 log::LogLevelFilter::Debug | |
105 } else { | |
106 log::LogLevelFilter::Info | |
107 }; | |
108 | |
109 let format = |record: &log::LogRecord| { | |
110 let datefmt = "%Y-%m-%d %I:%M:%S %p"; | |
111 let ts = time::strftime(datefmt, &time::now()).unwrap(); | |
112 format!("{}: {} - {}", ts, record.level(), record.args()) | |
113 }; | |
114 | |
115 | |
116 let mut builder = env_logger::LogBuilder::new(); | |
117 builder.format(format).filter(Some("wort_templog"), loglevel); | |
118 builder.init().unwrap(); | |
119 } | |
120 | |
121 fn handle_args() -> Args { | |
122 let args: Args = Args::docopt().decode().unwrap_or_else(|e| e.exit()); | |
123 | |
124 if args.flag_defconf { | |
125 println!("Default configuration:\n{}\n\n{}", | |
126 "(custom options go in tempserver.conf)", | |
127 config::Config::new().to_toml_string()); | |
128 std::process::exit(0); | |
129 } | |
130 | |
131 args | |
132 } | |
133 | |
134 fn load_config(config: &mut Config) { | |
135 | |
136 } | |
137 | |
138 fn main() { | |
139 let mut config = config::Config::new(); | |
140 | |
141 let args = handle_args(); | |
142 setup_log(args.flag_debug); | |
143 //env_logger::init().unwrap(); | |
144 | |
145 info!("wort-templog"); | |
146 debug!("debug mode"); | |
147 | |
148 load_config(&mut config); | |
149 | |
150 let config = config; | |
151 | |
152 if args.flag_thisconf { | |
153 println!("current configuration:\n\n{}", | |
154 config.to_toml_string()); | |
155 } | |
156 | |
157 | |
158 run(&config, args.flag_nowait, args.flag_test); | |
159 } | |
160 |