comparison rust/src/fridge.rs @ 606:3c1d37d78415 rust

untested wort_valid_time
author Matt Johnston <matt@ucc.asn.au>
date Fri, 17 Feb 2017 21:38:45 +0800
parents b45b8b4cf0f5
children 7bda01659426
comparison
equal deleted inserted replaced
605:8dd63473b6d8 606:3c1d37d78415
30 handle: Handle, 30 handle: Handle,
31 timeout_s: mpsc::Sender<u64>, 31 timeout_s: mpsc::Sender<u64>,
32 timeout_r: Option<mpsc::Receiver<u64>>, 32 timeout_r: Option<mpsc::Receiver<u64>>,
33 ticker: u64, 33 ticker: u64,
34 last_off_time: Instant, 34 last_off_time: Instant,
35 wort_valid_time: Instant,
35 } 36 }
36 37
37 impl Sink for Fridge { 38 impl Sink for Fridge {
38 39
39 type SinkItem = Message; 40 type SinkItem = Message;
62 handle: handle.clone(), 63 handle: handle.clone(),
63 timeout_s: s, 64 timeout_s: s,
64 timeout_r: Some(r), 65 timeout_r: Some(r),
65 ticker: 0, 66 ticker: 0,
66 last_off_time: Instant::now(), 67 last_off_time: Instant::now(),
68 wort_valid_time: Instant::now() - Duration::new(config.FRIDGE_WORT_INVALID_TIME, 100),
67 }; 69 };
68 if nowait { 70 if nowait {
69 f.last_off_time -= Duration::new(config.FRIDGE_DELAY, 100); 71 f.last_off_time -= Duration::new(config.FRIDGE_DELAY, 100);
70 } 72 }
71 f.tick(); 73 f.tick();
72 f 74 f
73 } 75 }
74 76
75 /// The fridge needs to periodically wake itself up, the returned
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)
83 -> Box<Stream<Item=Message, Error=io::Error>> {
84 mem::replace(&mut self.timeout_r, None)
85 .expect("NumberWatcher::timeouts() can only be called once")
86 .map(|v| Message::Tick(v))
87 .map_err(|_| io::Error::new(io::ErrorKind::Other, "Something wrong with watcher timeout channel"))
88 .boxed()
89 }
90
91 fn next_wakeup(&self) -> Duration { 77 fn next_wakeup(&self) -> Duration {
92 let millis = 400; 78 let millis = 400;
93 let dur = Duration::from_millis(millis); 79 let dur = Duration::from_millis(millis);
94 dur 80 dur
95 } 81 }
96 82
83 /// The fridge needs to periodically wake itself up, the returned
84 /// stream of Tick messages does so.
85 /// Examples of wakeups events are
86 ///
87 /// * overshoot calculation
88 /// * minimum fridge-off time
89 /// * invalid wort timeout
90 /// All specified in next_wakeup()
91 pub fn wakeups(&mut self)
92 -> Box<Stream<Item=Message, Error=io::Error>> {
93 mem::replace(&mut self.timeout_r, None)
94 .expect("Fridge::wakeups() can only be called once")
95 .map(|v| Message::Tick(v))
96 .map_err(|_| io::Error::new(io::ErrorKind::Other, "Something wrong with watcher timeout channel"))
97 .boxed()
98 }
99
100 /// Must be called after every state change. Turns the fridge on/off as required and
101 /// schedules any future wakeups based on the present (new) state
97 fn tick(&mut self) { 102 fn tick(&mut self) {
98 debug!("tick"); 103 debug!("tick");
99 104
100 self.send_next_timeout(); 105 self.send_next_timeout();
101 } 106 }
124 -> Box<Future<Item=(), Error=()>> { 129 -> Box<Future<Item=(), Error=()>> {
125 debug!("process_msg {:?}", msg); 130 debug!("process_msg {:?}", msg);
126 match msg { 131 match msg {
127 Message::Sensor{wort, fridge} => self.update_sensor(wort, fridge), 132 Message::Sensor{wort, fridge} => self.update_sensor(wort, fridge),
128 Message::Params(p) => self.update_params(p), 133 Message::Params(p) => self.update_params(p),
129 Message::Tick(v) => if v == self.ticker {self.tick()}, 134 Message::Tick(v) => if v == self.ticker {self.tick()}, // schedule a timeout if there are none pending
130 }; 135 };
131 future::ok::<(),()>(()).boxed() 136 future::ok::<(),()>(()).boxed()
132 } 137 }
133 138
134 pub fn update_params(&mut self, p: Params) { 139 pub fn update_params(&mut self, p: Params) {
140 145
141 pub fn update_sensor(&mut self, wort: Option<f32>, fridge: Option<f32>) { 146 pub fn update_sensor(&mut self, wort: Option<f32>, fridge: Option<f32>) {
142 self.temp_wort = wort; 147 self.temp_wort = wort;
143 self.temp_fridge = fridge; 148 self.temp_fridge = fridge;
144 149
150 if self.temp_wort.is_some() {
151 self.wort_valid_time = Instant::now();
152 }
153
145 self.tick(); 154 self.tick();
146 } 155 }
147 156
148 } 157 }