comparison 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
comparison
equal deleted inserted replaced
590:dccd8504aa38 591:4a944663fa8d
12 pub struct Reading { 12 pub struct Reading {
13 name: String, 13 name: String,
14 value: Option<f32>, 14 value: Option<f32>,
15 } 15 }
16 16
17 pub type Readings = Vec<Reading>;
18
19 pub struct Sensor { 17 pub struct Sensor {
20 current: f32, 18 current: f32,
19 suf: String,
21 } 20 }
22 21
23 impl Sensor { 22 impl Sensor {
24 23 fn step(&mut self) -> Vec<Reading> {
25 fn step(&mut self) -> Readings {
26 let mut r = Vec::new(); 24 let mut r = Vec::new();
27 self.current = self.current + 0.1; 25 self.current = self.current + 0.1;
28 r.push(Reading { name: "aaa".to_string(), value: Some(self.current) }); 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) });
29 r 28 r
30 } 29 }
31 30
32 fn new() -> Self { 31 fn new(suffix: String) -> Self {
33 Sensor { current: 22.0 } 32 Sensor { current: 22.0, suf: suffix }
34 } 33 }
35 34
36 pub fn run(handle: &Handle) -> Box<Stream<Item=Readings, Error=io::Error>> { 35 pub fn run(handle: &Handle, rate: u64, suffix: String) -> Box<Stream<Item=Vec<Reading>, Error=io::Error>> {
37 let mut s = Sensor::new(); 36 let mut s = Sensor::new(suffix);
38 37
39 let dur = Duration::from_millis(400); 38 let dur = Duration::from_millis(rate);
40 Interval::new(dur, handle).unwrap().map(move |()| { 39 Interval::new(dur, handle).unwrap().map(move |()| {
41 s.step() 40 s.step()
42 }).boxed() 41 }).boxed()
43 } 42 }
44 } 43 }