view rust/src/main.rs @ 594:aff50ee77252 rust

rust working better now with streams and sinks.
author Matt Johnston <matt@ucc.asn.au>
date Wed, 04 Jan 2017 17:18:44 +0800
parents bf138339d20a
children e87655ed8429
line wrap: on
line source

extern crate tokio_core;
extern crate futures;
extern crate rustc_serialize;
#[macro_use]
extern crate log;
extern crate env_logger;

use std::io;

use tokio_core::reactor::Core;
use futures::{Stream,Sink,Future};
use futures::sync::{mpsc};
use sensor::Sensor;

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

use types::*;

fn main() {
    env_logger::init().unwrap();

    println!("Wort Templog");
    debug!("debug log level");

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

    let mut paramh = ParamHolder::new();
    let mut fridge = fridge::Fridge::new(paramh.p, &handle);

    let (sensor_s, sensor_r) = mpsc::channel(1);
    let sensor_r = sensor_r.map_err(|_| io::Error::new(io::ErrorKind::Other, "Problem with sensor_r channel"));

    let sensor_stream = if cfg!(feature = "testmode") {
        sensor::TestSensor::stream(&handle)
    } else {
        sensor::OneWireSensor::stream(&handle)
    };

    // Send the sensors of interest to the fridge (sensor_s),
    // while streaming them all to the web sender.
    let s = sensor_stream.map(|r| {
        debug!("sensors {:?}", r);
        let t = sensor_s.clone().send(fridge::Message::Sensor{wort: r.wort(), fridge: r.fridge()})
                    .map(|_| ())
                    .map_err(|_| ());
        handle.spawn(t);
        r
    });

    let param_stream = paramwaiter::ParamWaiter::stream(&handle);
    let p = param_stream.map(|p| {
            fridge::Message::Params(p)
        });

    let timeouts = fridge.timeouts();

    let all_readings = s.for_each(|_| Ok(()));
    let all_fridge = p.select(timeouts).select(sensor_r).forward(fridge)
        .map(|_| () );

    let all = all_fridge.select(all_readings);

    core.run(all).ok();
}