Mercurial > templog
annotate rust/src/main.rs @ 627:d5075136442f rust
futures await
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 06 Feb 2018 22:16:44 +0800 |
parents | efcbe0d3afd6 |
children | c57821a60e51 |
rev | line source |
---|---|
627 | 1 #![feature(proc_macro, conservative_impl_trait, generators)] |
2 | |
3 | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
4 extern crate tokio_core; |
627 | 5 extern crate futures_await as futures; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
6 #[macro_use] |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
7 extern crate log; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
8 extern crate env_logger; |
597 | 9 extern crate rustc_serialize; |
10 extern crate time; | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
11 extern crate serde_json; |
623 | 12 extern crate libc; |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
623
diff
changeset
|
13 extern crate atomicwrites; |
626 | 14 extern crate hyper; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
15 |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
16 #[macro_use] |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
17 extern crate lazy_static; |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
18 |
595 | 19 #[macro_use] |
20 extern crate serde_derive; | |
21 extern crate serde; | |
22 | |
23 extern crate toml; | |
24 | |
597 | 25 extern crate docopt; |
26 | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
27 use std::io; |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
28 |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
29 use tokio_core::reactor::Core; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
30 use futures::{Stream,Sink,Future}; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
31 use futures::sync::{mpsc}; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
32 use sensor::Sensor; |
589
f2508125adf1
Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents:
588
diff
changeset
|
33 |
595 | 34 mod config; |
590 | 35 mod sensor; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
607
diff
changeset
|
36 mod fridge; |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
37 mod types; |
615 | 38 mod params; |
591 | 39 |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
40 use types::*; |
597 | 41 use config::Config; |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
42 |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
43 fn run(config: &Config, nowait: bool, testmode: bool) { |
595 | 44 |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
45 let mut core = Core::new().unwrap(); |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
46 let handle = core.handle(); |
587 | 47 |
615 | 48 let params = params::Params::load(&config); |
49 let mut fridge = fridge::Fridge::new(&config, nowait, params, &handle); | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
50 |
597 | 51 let sensor_stream = if testmode { |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
52 sensor::TestSensor::new(config).stream(&handle) |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
53 } else { |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
54 sensor::OneWireSensor::new(config).stream(&handle) |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
55 }; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
56 |
597 | 57 // Send the sensors of interest to the fridge (fridge_reading_s), |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
58 // while streaming them all to the web sender. |
620 | 59 let (fridge_reading_s, fridge_reading_r) = mpsc::channel(1); |
60 let fridge_reading_r = fridge_reading_r.map_err(|e| TemplogError::new("Problem with fridge_reading_r channel")); | |
61 let sensor_stream = sensor_stream.map(|r| { | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
62 debug!("sensors {:?}", r); |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
63 let msg = fridge::Message::Sensor { |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
64 wort: r.get_temp(&config.WORT_NAME), |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
65 fridge: r.get_temp(&config.FRIDGE_NAME) |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
66 }; |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
67 let t = fridge_reading_s.clone().send(msg) |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
68 .map(|_| ()) |
595 | 69 .map_err(|e| { |
597 | 70 warn!("Send error in fridge_reading_s: {}", e.to_string()); |
595 | 71 () |
72 }); | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
73 handle.spawn(t); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
74 r |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
75 }); |
587 | 76 |
627 | 77 let param_stream = params::ParamWaiter::stream(config.clone(), handle.clone()); |
620 | 78 let param_stream = param_stream.map(|p| { |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
79 fridge::Message::Params(p) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
80 }); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
81 |
597 | 82 let timeouts = fridge.wakeups(); |
591 | 83 |
620 | 84 // forward all the different types of messages to the fridge |
85 let all_fridge = param_stream.select(timeouts).select(fridge_reading_r).forward(fridge) .map(|_| () ); | |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
86 |
620 | 87 let all_readings = sensor_stream.for_each(|_| Ok(())); |
88 | |
89 // run forever | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
90 let all = all_fridge.select(all_readings); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
91 core.run(all).ok(); |
587 | 92 } |
93 | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
94 const USAGE: &'static str = "\ |
597 | 95 Wort Temperature |
96 Matt Johnston 2017 [email protected] | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
97 Usage: wort-templog [--help] [--new] [--daemon] [--debug] [--test] [--defconf] [--thisconf] [--nowait] |
597 | 98 |
99 Options: | |
100 -h, --help | |
101 --new Replace existing running instance | |
102 -D, --daemon Run in background | |
103 -d, --debug | |
104 -t, --test Use fake sensors etc | |
105 --nowait Skip initial fridge wait | |
615 | 106 --defconf Print default config (customise in local.conf) |
597 | 107 --thisconf Print used config |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
108 "; |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
109 |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
110 #[derive(RustcDecodable)] |
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
111 struct Args { |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
112 flag_new: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
113 flag_daemon: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
114 flag_debug: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
115 flag_test: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
116 flag_defconf: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
117 flag_thisconf: bool, |
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
118 flag_nowait: bool, |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
119 } |
597 | 120 |
121 fn setup_log(debug: bool) { | |
122 let loglevel = if debug { | |
123 log::LogLevelFilter::Debug | |
124 } else { | |
125 log::LogLevelFilter::Info | |
126 }; | |
127 | |
128 let format = |record: &log::LogRecord| { | |
129 let datefmt = "%Y-%m-%d %I:%M:%S %p"; | |
130 let ts = time::strftime(datefmt, &time::now()).unwrap(); | |
131 format!("{}: {} - {}", ts, record.level(), record.args()) | |
132 }; | |
133 | |
134 | |
135 let mut builder = env_logger::LogBuilder::new(); | |
136 builder.format(format).filter(Some("wort_templog"), loglevel); | |
137 builder.init().unwrap(); | |
138 } | |
139 | |
140 fn handle_args() -> Args { | |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
141 let args: Args = docopt::Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit()); |
597 | 142 |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
143 if args.flag_defconf { |
597 | 144 println!("Default configuration:\n{}\n\n{}", |
615 | 145 "(custom options go in local.conf)", |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
146 config::Config::default().to_toml_string()); |
597 | 147 std::process::exit(0); |
148 } | |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
149 args |
597 | 150 } |
151 | |
601 | 152 fn load_config() -> Config { |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
153 let nconfig = config::Config::default(); |
601 | 154 |
615 | 155 let conf_filename = "local.conf"; |
601 | 156 nconfig.merge_file(conf_filename) |
157 .unwrap_or_else(|e| { | |
623 | 158 if let TemplogErrorKind::Io(ref ioe) = *e.kind() { |
159 if let Some(errno) = ioe.raw_os_error() { | |
160 if errno == libc::ENOENT { | |
161 return nconfig; | |
162 } | |
163 } | |
164 } | |
165 | |
601 | 166 println!("Couldn't parse {}: {}", conf_filename, e); |
167 std::process::exit(1); | |
168 }) | |
169 } | |
170 | |
597 | 171 fn main() { |
172 | |
173 let args = handle_args(); | |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
174 setup_log(args.flag_debug); |
597 | 175 //env_logger::init().unwrap(); |
176 | |
177 info!("wort-templog"); | |
178 debug!("debug mode"); | |
179 | |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
180 let config = load_config(); |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
181 |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
182 if args.flag_thisconf { |
598
d4fbfb5c46ff
broken update of versions of things
Matt Johnston <matt@ucc.asn.au>
parents:
597
diff
changeset
|
183 println!("Current configuration:\n\n{}", |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
184 config.to_toml_string()); |
599
f71cf1ad745f
updated toml serde works OK
Matt Johnston <matt@ucc.asn.au>
parents:
598
diff
changeset
|
185 std::process::exit(0); |
597 | 186 } |
187 | |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
188 run(&config, args.flag_nowait, args.flag_test); |
597 | 189 } |