view rust/src/fridge.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

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

use futures::Future;
use tokio_core::reactor::Timeout;
use tokio_core::reactor::Handle;

use types::*;

pub struct Fridge {
    params: Params,
    temp_wort: Option<f32>,
    temp_fridge: Option<f32>,

    // timeouts to wake ourself up again
    //overshoot_timeout: Option<Future<Item=(), Error=io::Error>>,
    //fridgeoff_timeout: Option<Future<Item=(), Error=io::Error>>,
    wortvalid_timeout: Option<Timeout>,
}

impl Fridge {
    pub fn new(p: Params) -> Fridge {
        Fridge { 
            params: p,
            temp_wort: None,
            temp_fridge: None,
            //overshoot_timeout: None,
            //fridgeoff_timeout: None,
            wortvalid_timeout: None,
        }
    }

    fn tick(&mut self, handle: &Handle) {

    }

    pub fn set_params(&mut self, handle: &Handle, p: Params) {
        self.params = p;
        println!("params {:?}", self.params);

        self.tick(handle);
    }

    pub fn set_temps(&mut self, handle: &Handle, 
                wort: Option<f32>, fridge: Option<f32>) {
        self.temp_wort = wort;
        self.temp_fridge = fridge;

        if let Some(_) = self.temp_wort {
            // set a new timeout, replacing any existing
            let dur = Duration::new(10, 0); // XXX
            let t = Timeout::new(dur, handle).unwrap();
            /*
            handle.spawn(t.and_then(|_| {
                                self.tick(handle);
                                Ok(())
                            }).map_err(|x| ()));
                            */
            self.wortvalid_timeout = Some(t);
        }

        self.tick(handle);
    }

}