annotate rust/src/configwaiter.rs @ 607:282fae1c12e4 rust

NotTooOften
author Matt Johnston <matt@ucc.asn.au>
date Fri, 17 Feb 2017 22:27:44 +0800
parents 613f114feb4b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
602
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 extern crate tokio_core;
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 extern crate futures;
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 extern crate rand;
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 use std::time::Duration;
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 use std::io;
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 use tokio_core::reactor::Interval;
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 use tokio_core::reactor::Handle;
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 use futures::Stream;
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 use types::*;
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 pub struct ParamWaiter {
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 }
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 impl ParamWaiter {
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 fn step(&mut self) -> Params {
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 let mut p = Params::defaults();
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 let mut rng = rand::thread_rng();
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 p.fridge_setpoint = 17.0 + 4.0*rand::random::<f32>();
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 p
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 }
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 pub fn new() -> Self {
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 ParamWaiter {}
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 }
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 pub fn run(handle: &Handle, rate: u64) -> Box<Stream<Item=Params, Error=io::Error>> {
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 let mut s = ParamWaiter::new();
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 let dur = Duration::from_millis(rate);
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 Interval::new(dur, handle).unwrap().map(move |()| {
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 s.step()
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 }).boxed()
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 }
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 }
613f114feb4b missing file
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37