view rust/src/main.rs @ 596:ca8102feaca6 rust

sensor takes config parameter
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 Jan 2017 22:04:10 +0800
parents e87655ed8429
children a440eafa84a9
line wrap: on
line source

#![feature(proc_macro)]

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

#[macro_use]
extern crate serde_derive;
extern crate serde;

extern crate toml;


use std::io;

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

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

use types::*;

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

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

    let config = config::Config::new();
    println!("{}", config.to_toml_string());

    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, &config)
    } else {
        sensor::OneWireSensor::stream(&handle, &config)
    };

    // 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(|e| {
                        warn!("Send error in sensor_s: {}", e.to_string());
                        ()
                    });
        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();
}