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