Mercurial > templog
annotate rust/src/main.rs @ 594:aff50ee77252 rust
rust working better now with streams and sinks.
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 04 Jan 2017 17:18:44 +0800 |
parents | bf138339d20a |
children | e87655ed8429 |
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; |
591 | 3 extern crate rustc_serialize; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
4 #[macro_use] |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
5 extern crate log; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
6 extern crate env_logger; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
7 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
8 use std::io; |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
9 |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
10 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
|
11 use futures::{Stream,Sink,Future}; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
12 use futures::sync::{mpsc}; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
13 use sensor::Sensor; |
589
f2508125adf1
Try using traits for periodic stream
Matt Johnston <matt@ucc.asn.au>
parents:
588
diff
changeset
|
14 |
590 | 15 mod sensor; |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
16 mod fridge; |
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
17 mod types; |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
18 mod paramwaiter; |
591 | 19 |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
20 use types::*; |
591 | 21 |
587 | 22 fn main() { |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
23 env_logger::init().unwrap(); |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
24 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
25 println!("Wort Templog"); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
26 debug!("debug log level"); |
591 | 27 |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
28 let mut core = Core::new().unwrap(); |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
29 let handle = core.handle(); |
587 | 30 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
31 let mut paramh = ParamHolder::new(); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
32 let mut fridge = fridge::Fridge::new(paramh.p, &handle); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
33 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
34 let (sensor_s, sensor_r) = mpsc::channel(1); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
35 let sensor_r = sensor_r.map_err(|_| io::Error::new(io::ErrorKind::Other, "Problem with sensor_r channel")); |
587 | 36 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
37 let sensor_stream = if cfg!(feature = "testmode") { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
38 sensor::TestSensor::stream(&handle) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
39 } else { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
40 sensor::OneWireSensor::stream(&handle) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
41 }; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
42 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
43 // Send the sensors of interest to the fridge (sensor_s), |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
44 // 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
|
45 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
|
46 debug!("sensors {:?}", r); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
47 let t = sensor_s.clone().send(fridge::Message::Sensor{wort: r.wort(), fridge: r.fridge()}) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
48 .map(|_| ()) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
49 .map_err(|_| ()); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
50 handle.spawn(t); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
51 r |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
52 }); |
587 | 53 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
54 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
|
55 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
|
56 fridge::Message::Params(p) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
57 }); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
58 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
59 let timeouts = fridge.timeouts(); |
591 | 60 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
61 let all_readings = s.for_each(|_| Ok(())); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
62 let all_fridge = p.select(timeouts).select(sensor_r).forward(fridge) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
63 .map(|_| () ); |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
64 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
65 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
|
66 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
67 core.run(all).ok(); |
587 | 68 } |
69 |