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