Mercurial > templog
diff 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 |
line wrap: on
line diff
--- a/rust/src/sensor.rs Wed Dec 21 08:16:13 2016 +0800 +++ b/rust/src/sensor.rs Wed Dec 21 21:40:32 2016 +0800 @@ -2,39 +2,43 @@ extern crate futures; use std::time::Duration; -use std; -use std::cell::RefCell; +use std::io; use tokio_core::reactor::Interval; -use tokio_core::reactor::Core; use tokio_core::reactor::Handle; use futures::Stream; -use futures::Future; -pub struct Readings { - +#[derive(Debug)] +pub struct Reading { + name: String, + value: Option<f32>, } +pub type Readings = Vec<Reading>; + pub struct Sensor { + current: f32, } impl Sensor { - fn step(self) -> Readings { - return Readings {} - } - - pub fn new() -> Sensor { - Sensor {} + fn step(&mut self) -> Readings { + let mut r = Vec::new(); + self.current = self.current + 0.1; + r.push(Reading { name: "aaa".to_string(), value: Some(self.current) }); + r } - pub fn run(handle: &Handle) -> Box<Future<Item=Readings, Error = std::io::Error>> { - let s = Sensor::new(); + fn new() -> Self { + Sensor { current: 22.0 } + } - Interval::new(Duration::from_millis(400), handle).map(|()| { - println!("each one"); - // s.step() - Readings {} + pub fn run(handle: &Handle) -> Box<Stream<Item=Readings, Error=io::Error>> { + let mut s = Sensor::new(); + + let dur = Duration::from_millis(400); + Interval::new(dur, handle).unwrap().map(move |()| { + s.step() }).boxed() } }