view 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 source

extern crate tokio_core;
extern crate futures;

use std::time::Duration;
use std::io;

use tokio_core::reactor::Interval;
use tokio_core::reactor::Handle;
use futures::Stream;

#[derive(Debug)]
pub struct Reading {
    name: String,
    value: Option<f32>,
}

pub type Readings = Vec<Reading>;

pub struct Sensor {
    current: f32,
}

impl 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
    }

    fn new() -> Self {
        Sensor { current: 22.0 }
    }

    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()
    }
}