Mercurial > templog
annotate rust/src/sensor.rs @ 590:dccd8504aa38 rust
it runs
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 21 Dec 2016 21:40:32 +0800 |
parents | 038734052b20 |
children | 4a944663fa8d |
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; |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
3 |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
4 use std::time::Duration; |
590 | 5 use std::io; |
587 | 6 |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
7 use tokio_core::reactor::Interval; |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
8 use tokio_core::reactor::Handle; |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
9 use futures::Stream; |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
10 |
590 | 11 #[derive(Debug)] |
12 pub struct Reading { | |
13 name: String, | |
14 value: Option<f32>, | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
15 } |
587 | 16 |
590 | 17 pub type Readings = Vec<Reading>; |
18 | |
587 | 19 pub struct Sensor { |
590 | 20 current: f32, |
587 | 21 } |
22 | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
23 impl Sensor { |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
24 |
590 | 25 fn step(&mut self) -> Readings { |
26 let mut r = Vec::new(); | |
27 self.current = self.current + 0.1; | |
28 r.push(Reading { name: "aaa".to_string(), value: Some(self.current) }); | |
29 r | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
30 } |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
31 |
590 | 32 fn new() -> Self { |
33 Sensor { current: 22.0 } | |
34 } | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
35 |
590 | 36 pub fn run(handle: &Handle) -> Box<Stream<Item=Readings, Error=io::Error>> { |
37 let mut s = Sensor::new(); | |
38 | |
39 let dur = Duration::from_millis(400); | |
40 Interval::new(dur, handle).unwrap().map(move |()| { | |
41 s.step() | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
42 }).boxed() |
587 | 43 } |
44 } | |
45 |