view rust/src/main.rs @ 593:bf138339d20a rust

fiddling with timeouts and closures
author Matt Johnston <matt@ucc.asn.au>
date Tue, 27 Dec 2016 00:51:28 +0800
parents 03b48ec0bb03
children aff50ee77252
line wrap: on
line source

extern crate tokio_core;
extern crate futures;
extern crate rustc_serialize;

use tokio_core::reactor::Core;
use futures::{Future,Stream};
use rustc_serialize::json;

mod sensor;
mod fridge;
mod types;
mod paramwaiter;

use types::*;

fn main() {
    println!("Wort Templog");

    let mut paramh = ParamHolder::new();
    let mut readings = Readings::new();
    let mut fridge = fridge::Fridge::new(paramh.p.clone());

    let mut core = Core::new().unwrap();
    let handle = core.handle();

    let s = sensor::Sensor::run(&handle, 400, "sens1".to_string());
    let w = paramwaiter::ParamWaiter::run(&handle, 3000);

    let h = s.for_each(move |r| {
        readings.push(r);
        println!("readings {:?}", readings);
        Ok(())
    });

    let j = w.for_each(move |p| {
        fridge.set_params(&handle, p.clone());
        paramh.p = p;
        Ok(())
    });

    handle.spawn(h.map_err(|x| ()));
    handle.spawn(j.map_err(|x| ()));

    let forever = futures::empty::<(),()>();
    core.run(forever);
}