comparison 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
comparison
equal deleted inserted replaced
608:71f045231a07 609:7bda01659426
5 use std::time::Duration; 5 use std::time::Duration;
6 use std::io; 6 use std::io;
7 7
8 use tokio_core::reactor::Interval; 8 use tokio_core::reactor::Interval;
9 use tokio_core::reactor::Handle; 9 use tokio_core::reactor::Handle;
10 use futures::Stream; 10 use tokio_curl::Session;
11 use futures::{Stream,Future,future};
11 use types::*; 12 use types::*;
13 use curl::Easy;
14 use ::Config;
12 15
13 pub struct ParamWaiter { 16 pub struct ParamWaiter {
17 limitlog: NotTooOften,
18 epoch_tag: String,
19 session: Option<Session>,
20 config: Config,
14 } 21 }
15 22
23 const LOGMINUTES: u64 = 15;
24
16 impl ParamWaiter { 25 impl ParamWaiter {
17 fn step(&mut self) -> Params { 26 pub fn new(config: &Config) -> Self {
27 ParamWaiter {
28 limitlog: NotTooOften::new(LOGMINUTES*60),
29 epoch_tag: String::new(),
30 session: None,
31 config: config.clone(),
32 }
33 }
34
35 fn make_req(&self) -> Easy {
36 let mut req = Easy::new();
37 req.get(true).unwrap();
38 req.url(config.SETTINGS_URL);
39 }
40
41 fn step(&mut self, handle: &Handle) -> Box<Future<Item=Option<Params>, Error=io::Error>> {
42 if self.session.is_none() {
43 self.session = Some(Session::new(handle.clone()))
44 }
45
46 let req = self.make_req();
47 /*
48 self.session.unwrap().perform(self.make_req())
49 .and_then(||)
50 */
51
18 let mut p = Params::defaults(); 52 let mut p = Params::defaults();
19 p.fridge_setpoint = 17.0 + 4.0*rand::random::<f32>(); 53 p.fridge_setpoint = 17.0 + 4.0*rand::random::<f32>();
20 p 54 future::ok(p).boxed()
21 } 55 }
22 56
23 pub fn new() -> Self { 57 pub fn stream(&mut self, handle: &Handle) -> Box<Stream<Item=Params, Error=io::Error>> {
24 ParamWaiter {}
25 }
26
27 pub fn stream(handle: &Handle) -> Box<Stream<Item=Params, Error=io::Error>> {
28 let mut s = ParamWaiter::new();
29 58
30 let dur = Duration::from_millis(4000); 59 let dur = Duration::from_millis(4000);
31 Interval::new(dur, handle).unwrap().map(move |()| { 60 let i = Interval::new(dur, handle).unwrap()
32 s.step() 61 .and_then(move |()| {
33 }).boxed() 62 s.step()
63 })
64 // throw away None params
65 .filter_map(|p| p);
66 Box::new(i)
34 } 67 }
35 } 68 }
36 69