Mercurial > templog
diff 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 |
line wrap: on
line diff
--- a/rust/src/sensor.rs Wed Dec 21 21:40:32 2016 +0800 +++ b/rust/src/sensor.rs Fri Dec 23 00:33:19 2016 +0800 @@ -14,29 +14,28 @@ value: Option<f32>, } -pub type Readings = Vec<Reading>; - pub struct Sensor { current: f32, + suf: String, } impl Sensor { - - fn step(&mut self) -> Readings { + fn step(&mut self) -> Vec<Reading> { let mut r = Vec::new(); self.current = self.current + 0.1; - r.push(Reading { name: "aaa".to_string(), value: Some(self.current) }); + r.push(Reading { name: "aaa".to_string() + &self.suf, value: Some(self.current) }); + r.push(Reading { name: "b".to_string() + &self.suf, value: Some(self.current/3.0) }); r } - fn new() -> Self { - Sensor { current: 22.0 } + fn new(suffix: String) -> Self { + Sensor { current: 22.0, suf: suffix } } - pub fn run(handle: &Handle) -> Box<Stream<Item=Readings, Error=io::Error>> { - let mut s = Sensor::new(); + pub fn run(handle: &Handle, rate: u64, suffix: String) -> Box<Stream<Item=Vec<Reading>, Error=io::Error>> { + let mut s = Sensor::new(suffix); - let dur = Duration::from_millis(400); + let dur = Duration::from_millis(rate); Interval::new(dur, handle).unwrap().map(move |()| { s.step() }).boxed()