Mercurial > templog
comparison 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 |
comparison
equal
deleted
inserted
replaced
602:613f114feb4b | 603:b45b8b4cf0f5 |
---|---|
3 #[macro_use] | 3 #[macro_use] |
4 extern crate log; | 4 extern crate log; |
5 extern crate env_logger; | 5 extern crate env_logger; |
6 extern crate rustc_serialize; | 6 extern crate rustc_serialize; |
7 extern crate time; | 7 extern crate time; |
8 | |
9 #[macro_use] | |
10 extern crate lazy_static; | |
11 | 8 |
12 #[macro_use] | 9 #[macro_use] |
13 extern crate serde_derive; | 10 extern crate serde_derive; |
14 extern crate serde; | 11 extern crate serde; |
15 | 12 |
31 mod paramwaiter; | 28 mod paramwaiter; |
32 | 29 |
33 use types::*; | 30 use types::*; |
34 use config::Config; | 31 use config::Config; |
35 | 32 |
36 fn run(nowait: bool, testmode: bool) { | 33 fn run(config: &Config, nowait: bool, testmode: bool) { |
37 | 34 |
38 let mut core = Core::new().unwrap(); | 35 let mut core = Core::new().unwrap(); |
39 let handle = core.handle(); | 36 let handle = core.handle(); |
40 | 37 |
41 let mut paramh = ParamHolder::new(); | 38 let mut paramh = ParamHolder::new(); |
42 let mut fridge = fridge::Fridge::new(nowait, paramh.p, &handle); | 39 let mut fridge = fridge::Fridge::new(&config, nowait, paramh.p, &handle); |
43 | 40 |
44 let (fridge_reading_s, fridge_reading_r) = mpsc::channel(1); | 41 let (fridge_reading_s, fridge_reading_r) = mpsc::channel(1); |
45 let fridge_reading_r = fridge_reading_r.map_err(|_| io::Error::new(io::ErrorKind::Other, "Problem with fridge_reading_r channel")); | 42 let fridge_reading_r = fridge_reading_r.map_err(|_| io::Error::new(io::ErrorKind::Other, "Problem with fridge_reading_r channel")); |
46 | 43 |
47 let sensor_stream = if testmode { | 44 let sensor_stream = if testmode { |
48 sensor::TestSensor::stream(&handle) | 45 sensor::TestSensor::new(config).stream(&handle) |
49 } else { | 46 } else { |
50 sensor::OneWireSensor::stream(&handle) | 47 sensor::OneWireSensor::new(config).stream(&handle) |
51 }; | 48 }; |
52 | 49 |
53 // Send the sensors of interest to the fridge (fridge_reading_s), | 50 // Send the sensors of interest to the fridge (fridge_reading_s), |
54 // while streaming them all to the web sender. | 51 // while streaming them all to the web sender. |
55 let s = sensor_stream.map(|r| { | 52 let s = sensor_stream.map(|r| { |
56 debug!("sensors {:?}", r); | 53 debug!("sensors {:?}", r); |
57 let t = fridge_reading_s.clone().send(fridge::Message::Sensor{wort: r.wort(), fridge: r.fridge()}) | 54 let msg = fridge::Message::Sensor { |
55 wort: r.get_temp(&config.WORT_NAME), | |
56 fridge: r.get_temp(&config.FRIDGE_NAME) | |
57 }; | |
58 let t = fridge_reading_s.clone().send(msg) | |
58 .map(|_| ()) | 59 .map(|_| ()) |
59 .map_err(|e| { | 60 .map_err(|e| { |
60 warn!("Send error in fridge_reading_s: {}", e.to_string()); | 61 warn!("Send error in fridge_reading_s: {}", e.to_string()); |
61 () | 62 () |
62 }); | 63 }); |
137 } | 138 } |
138 args | 139 args |
139 } | 140 } |
140 | 141 |
141 fn load_config() -> Config { | 142 fn load_config() -> Config { |
142 let mut nconfig = config::Config::default(); | 143 let nconfig = config::Config::default(); |
143 | 144 |
144 let conf_filename = "tempserver.conf"; | 145 let conf_filename = "tempserver.conf"; |
145 nconfig.merge_file(conf_filename) | 146 nconfig.merge_file(conf_filename) |
146 .unwrap_or_else(|e| { | 147 .unwrap_or_else(|e| { |
147 println!("Couldn't parse {}: {}", conf_filename, e); | 148 println!("Couldn't parse {}: {}", conf_filename, e); |
148 std::process::exit(1); | 149 std::process::exit(1); |
149 }) | 150 }) |
150 } | |
151 | |
152 lazy_static! { | |
153 static ref rigid_config: Config = load_config(); | |
154 } | 151 } |
155 | 152 |
156 fn main() { | 153 fn main() { |
157 | 154 |
158 let args = handle_args(); | 155 let args = handle_args(); |
160 //env_logger::init().unwrap(); | 157 //env_logger::init().unwrap(); |
161 | 158 |
162 info!("wort-templog"); | 159 info!("wort-templog"); |
163 debug!("debug mode"); | 160 debug!("debug mode"); |
164 | 161 |
162 let config = load_config(); | |
163 | |
165 if args.flag_thisconf { | 164 if args.flag_thisconf { |
166 println!("Current configuration:\n\n{}", | 165 println!("Current configuration:\n\n{}", |
167 rigid_config.to_toml_string()); | 166 config.to_toml_string()); |
168 std::process::exit(0); | 167 std::process::exit(0); |
169 } | 168 } |
170 | 169 |
171 | 170 run(&config, args.flag_nowait, args.flag_test); |
172 | |
173 run(args.flag_nowait, args.flag_test); | |
174 } | 171 } |
175 | 172 |