comparison rust/src/fridge.rs @ 597:a440eafa84a9 rust

progress for debug
author Matt Johnston <matt@ucc.asn.au>
date Sat, 07 Jan 2017 00:56:39 +0800
parents e87655ed8429
children 8c21df3711e2
comparison
equal deleted inserted replaced
596:ca8102feaca6 597:a440eafa84a9
2 extern crate tokio_core; 2 extern crate tokio_core;
3 3
4 use std; 4 use std;
5 use std::io; 5 use std::io;
6 use std::mem; 6 use std::mem;
7 use std::time::Duration; 7 use std::time::{Duration,Instant};
8 8
9 use futures::{Future,future,Sink,Stream}; 9 use futures::{Future,future,Sink,Stream};
10 use tokio_core::reactor::{Timeout,Handle}; 10 use tokio_core::reactor::{Timeout,Handle};
11 use futures::sync::{mpsc}; 11 use futures::sync::{mpsc};
12 12
13 use config::Config;
13 use types::*; 14 use types::*;
14 15
15 #[derive(Debug)] 16 #[derive(Debug)]
16 pub enum Message { 17 pub enum Message {
17 Sensor {wort: Option<f32>, fridge: Option<f32>}, 18 Sensor {wort: Option<f32>, fridge: Option<f32>},
18 Params (Params), 19 Params (Params),
19 Tick(u64), 20 Tick(u64),
20 } 21 }
21 22
22 pub struct Fridge { 23 pub struct Fridge {
24 config: Config,
23 params: Params, 25 params: Params,
24 temp_wort: Option<f32>, 26 temp_wort: Option<f32>,
25 temp_fridge: Option<f32>, 27 temp_fridge: Option<f32>,
26 28
27 // Timeouts to wake ourselves up again 29 // Timeouts to wake ourselves up again
28 handle: Handle, 30 handle: Handle,
29 timeout_s: mpsc::Sender<u64>, 31 timeout_s: mpsc::Sender<u64>,
30 timeout_r: Option<mpsc::Receiver<u64>>, 32 timeout_r: Option<mpsc::Receiver<u64>>,
31 ticker: u64, 33 ticker: u64,
34 last_off_time: Instant,
32 } 35 }
33 36
34 impl Sink for Fridge { 37 impl Sink for Fridge {
35 38
36 type SinkItem = Message; 39 type SinkItem = Message;
46 Ok(futures::Async::Ready(())) 49 Ok(futures::Async::Ready(()))
47 } 50 }
48 } 51 }
49 52
50 impl Fridge { 53 impl Fridge {
51 pub fn new(p: Params, handle: &Handle) -> Fridge { 54 pub fn new(config: &Config, nowait: bool, p: Params, handle: &Handle) -> Fridge {
52 let (s, r) = mpsc::channel(1); 55 let (s, r) = mpsc::channel(1);
53 let mut f = Fridge { 56 let mut f = Fridge {
57 config: config.clone(),
54 params: p, 58 params: p,
55 temp_wort: None, 59 temp_wort: None,
56 temp_fridge: None, 60 temp_fridge: None,
57 61
58 handle: handle.clone(), 62 handle: handle.clone(),
59 timeout_s: s, 63 timeout_s: s,
60 timeout_r: Some(r), 64 timeout_r: Some(r),
61 ticker: 0, 65 ticker: 0,
66 last_off_time: Instant::now(),
62 }; 67 };
68 if nowait {
69 f.last_off_time -= Duration::new(config.FRIDGE_DELAY, 100);
70 }
63 f.tick(); 71 f.tick();
64 f 72 f
65 } 73 }
66 74
67 /// Returns a stream of timeouts for fridge, waking when next necessary 75 /// The fridge needs to periodically wake itself up, the returned
68 pub fn timeouts(&mut self) 76 // stream of Tick messages does so.
77 /// Examples of wakeups events are
78 ///
79 /// * overshoot calculation
80 /// * minimum fridge-off time
81 /// * invalid wort timeout
82 pub fn wakeups(&mut self)
69 -> Box<Stream<Item=Message, Error=io::Error>> { 83 -> Box<Stream<Item=Message, Error=io::Error>> {
70 mem::replace(&mut self.timeout_r, None) 84 mem::replace(&mut self.timeout_r, None)
71 .expect("NumberWatcher::timeouts() can only be called once") 85 .expect("NumberWatcher::timeouts() can only be called once")
72 .map(|v| Message::Tick(v)) 86 .map(|v| Message::Tick(v))
73 .map_err(|_| io::Error::new(io::ErrorKind::Other, "Something wrong with watcher timeout channel")) 87 .map_err(|_| io::Error::new(io::ErrorKind::Other, "Something wrong with watcher timeout channel"))