view rust/src/paramwaiter.rs @ 609:7bda01659426 rust

not building, paramwaiter work
author Matt Johnston <matt@ucc.asn.au>
date Sat, 18 Feb 2017 00:21:10 +0800
parents aff50ee77252
children f3e39e2107fd
line wrap: on
line source

extern crate tokio_core;
extern crate futures;
extern crate rand;

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

use tokio_core::reactor::Interval;
use tokio_core::reactor::Handle;
use tokio_curl::Session;
use futures::{Stream,Future,future};
use types::*;
use curl::Easy;
use ::Config;

pub struct ParamWaiter {
    limitlog: NotTooOften,
    epoch_tag: String,
    session: Option<Session>,
    config: Config,
}

const LOGMINUTES: u64 = 15;

impl ParamWaiter {
    pub fn new(config: &Config) -> Self {
        ParamWaiter {
            limitlog: NotTooOften::new(LOGMINUTES*60),
            epoch_tag: String::new(),
            session: None,
            config: config.clone(),
        }
    }

    fn make_req(&self) -> Easy {
        let mut req = Easy::new();
        req.get(true).unwrap();
        req.url(config.SETTINGS_URL);
    }

    fn step(&mut self, handle: &Handle) -> Box<Future<Item=Option<Params>, Error=io::Error>> {
        if self.session.is_none() {
            self.session = Some(Session::new(handle.clone()))
        }

        let req = self.make_req();
        /*
        self.session.unwrap().perform(self.make_req())
            .and_then(||)
            */

        let mut p = Params::defaults();
        p.fridge_setpoint = 17.0 + 4.0*rand::random::<f32>();
        future::ok(p).boxed()
    }

    pub fn stream(&mut self, handle: &Handle) -> Box<Stream<Item=Params, Error=io::Error>> {

        let dur = Duration::from_millis(4000);
        let i = Interval::new(dur, handle).unwrap()
            .and_then(move |()| {
                s.step()
            })
            // throw away None params
            .filter_map(|p| p);
        Box::new(i)
    }
}