comparison 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
comparison
equal deleted inserted replaced
589:f2508125adf1 590:dccd8504aa38
1 extern crate tokio_core; 1 extern crate tokio_core;
2 extern crate futures; 2 extern crate futures;
3 3
4 use std::time::Duration; 4 use std::time::Duration;
5 use std; 5 use std::io;
6 use std::cell::RefCell;
7 6
8 use tokio_core::reactor::Interval; 7 use tokio_core::reactor::Interval;
9 use tokio_core::reactor::Core;
10 use tokio_core::reactor::Handle; 8 use tokio_core::reactor::Handle;
11 use futures::Stream; 9 use futures::Stream;
12 use futures::Future;
13 10
14 pub struct Readings { 11 #[derive(Debug)]
15 12 pub struct Reading {
13 name: String,
14 value: Option<f32>,
16 } 15 }
17 16
17 pub type Readings = Vec<Reading>;
18
18 pub struct Sensor { 19 pub struct Sensor {
20 current: f32,
19 } 21 }
20 22
21 impl Sensor { 23 impl Sensor {
22 24
23 fn step(self) -> Readings { 25 fn step(&mut self) -> Readings {
24 return Readings {} 26 let mut r = Vec::new();
27 self.current = self.current + 0.1;
28 r.push(Reading { name: "aaa".to_string(), value: Some(self.current) });
29 r
25 } 30 }
26 31
27 pub fn new() -> Sensor { 32 fn new() -> Self {
28 Sensor {} 33 Sensor { current: 22.0 }
29 } 34 }
30 35
31 pub fn run(handle: &Handle) -> Box<Future<Item=Readings, Error = std::io::Error>> { 36 pub fn run(handle: &Handle) -> Box<Stream<Item=Readings, Error=io::Error>> {
32 let s = Sensor::new(); 37 let mut s = Sensor::new();
33 38
34 Interval::new(Duration::from_millis(400), handle).map(|()| { 39 let dur = Duration::from_millis(400);
35 println!("each one"); 40 Interval::new(dur, handle).unwrap().map(move |()| {
36 // s.step() 41 s.step()
37 Readings {}
38 }).boxed() 42 }).boxed()
39 } 43 }
40 } 44 }
41 45