Mercurial > templog
annotate rust/src/sensor.rs @ 591:4a944663fa8d rust
more skeleton
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 23 Dec 2016 00:33:19 +0800 |
parents | dccd8504aa38 |
children | 03b48ec0bb03 |
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 |
17 pub struct Sensor { | |
590 | 18 current: f32, |
591 | 19 suf: String, |
587 | 20 } |
21 | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
22 impl Sensor { |
591 | 23 fn step(&mut self) -> Vec<Reading> { |
590 | 24 let mut r = Vec::new(); |
25 self.current = self.current + 0.1; | |
591 | 26 r.push(Reading { name: "aaa".to_string() + &self.suf, value: Some(self.current) }); |
27 r.push(Reading { name: "b".to_string() + &self.suf, value: Some(self.current/3.0) }); | |
590 | 28 r |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
29 } |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
30 |
591 | 31 fn new(suffix: String) -> Self { |
32 Sensor { current: 22.0, suf: suffix } | |
590 | 33 } |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
34 |
591 | 35 pub fn run(handle: &Handle, rate: u64, suffix: String) -> Box<Stream<Item=Vec<Reading>, Error=io::Error>> { |
36 let mut s = Sensor::new(suffix); | |
590 | 37 |
591 | 38 let dur = Duration::from_millis(rate); |
590 | 39 Interval::new(dur, handle).unwrap().map(move |()| { |
40 s.step() | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
41 }).boxed() |
587 | 42 } |
43 } | |
44 |