Mercurial > templog
annotate 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 |
rev | line source |
---|---|
597 | 1 #![feature(plugin)] |
2 #![plugin(docopt_macros)] | |
3 | |
595 | 4 #![feature(proc_macro)] |
5 | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
6 extern crate tokio_core; |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
7 extern crate futures; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
8 #[macro_use] |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
9 extern crate log; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
10 extern crate env_logger; |
597 | 11 extern crate rustc_serialize; |
12 extern crate time; | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
13 |
595 | 14 #[macro_use] |
15 extern crate serde_derive; | |
16 extern crate serde; | |
17 | |
18 extern crate toml; | |
19 | |
597 | 20 extern crate docopt; |
21 | |
595 | 22 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
23 use std::io; |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
24 |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
25 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
|
26 use futures::{Stream,Sink,Future}; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
27 use futures::sync::{mpsc}; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
28 use sensor::Sensor; |
589
f2508125adf1
Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents:
588
diff
changeset
|
29 |
595 | 30 mod config; |
590 | 31 mod sensor; |
597 | 32 pub mod fridge; |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
33 mod types; |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
34 mod paramwaiter; |
591 | 35 |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
36 use types::*; |
597 | 37 use config::Config; |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
38 |
597 | 39 fn run(config: &Config, nowait: bool, testmode: bool) { |
595 | 40 |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
41 let mut core = Core::new().unwrap(); |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
42 let handle = core.handle(); |
587 | 43 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
44 let mut paramh = ParamHolder::new(); |
597 | 45 let mut fridge = fridge::Fridge::new(config, nowait, paramh.p, &handle); |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
46 |
597 | 47 let (fridge_reading_s, fridge_reading_r) = mpsc::channel(1); |
48 let fridge_reading_r = fridge_reading_r.map_err(|_| io::Error::new(io::ErrorKind::Other, "Problem with fridge_reading_r channel")); | |
587 | 49 |
597 | 50 let sensor_stream = if testmode { |
51 sensor::TestSensor::stream(&handle, config) | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
52 } else { |
597 | 53 sensor::OneWireSensor::stream(&handle, config) |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
54 }; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
55 |
597 | 56 // 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
|
57 // 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
|
58 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
|
59 debug!("sensors {:?}", r); |
597 | 60 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
|
61 .map(|_| ()) |
595 | 62 .map_err(|e| { |
597 | 63 warn!("Send error in fridge_reading_s: {}", e.to_string()); |
595 | 64 () |
65 }); | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
66 handle.spawn(t); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
67 r |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
68 }); |
587 | 69 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
70 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
|
71 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
|
72 fridge::Message::Params(p) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
73 }); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
74 |
597 | 75 let timeouts = fridge.wakeups(); |
591 | 76 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
77 let all_readings = s.for_each(|_| Ok(())); |
597 | 78 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
|
79 .map(|_| () ); |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
80 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
81 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
|
82 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
83 core.run(all).ok(); |
587 | 84 } |
85 | |
597 | 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 |