view rust/src/sensor.rs @ 592:03b48ec0bb03 rust

fridge, types, configwaiter
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Dec 2016 00:14:58 +0800
parents 4a944663fa8d
children aff50ee77252
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;
use types::*;

pub struct Sensor {
    current: f32,
    suf: String,
}

impl Sensor {
    fn step(&mut self) -> Vec<Reading> {
        let mut r = Vec::new();
        self.current = self.current + 0.1;
        r.push(Reading::new("aaa".to_string() + &self.suf, self.current));
        r.push(Reading::new("b".to_string() + &self.suf, self.current/3.0));
        r
    }

    fn new(suffix: String) -> Self {
        Sensor { current: 22.0, suf: suffix }
    }

    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(rate);
        Interval::new(dur, handle).unwrap().map(move |()| {
            s.step()
        }).boxed()
    }
}