Mercurial > templog
annotate rust/src/fridge.rs @ 626:efcbe0d3afd6 rust
fix to work with hyper
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 06 Dec 2017 00:09:45 +0800 |
parents | 8136a6b99866 |
children | e1b5938de122 |
rev | line source |
---|---|
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
1 extern crate futures; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
2 extern crate tokio_core; |
626 | 3 #[cfg(target_os = "linux")] |
620 | 4 extern crate sysfs_gpio; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
5 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
6 use std; |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
7 use std::io; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
8 use std::mem; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
9 use std::error::Error; |
597 | 10 use std::time::{Duration,Instant}; |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
11 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
12 use futures::{Future,future,Sink,Stream}; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
13 use tokio_core::reactor::{Timeout,Handle}; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
14 use futures::sync::{mpsc}; |
621 | 15 |
16 #[cfg(target_os = "linux")] | |
626 | 17 use self::sysfs_gpio::{Direction, Pin}; |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
18 |
615 | 19 use config::Config; |
20 use params::Params; | |
592 | 21 use types::*; |
22 | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
23 #[derive(Debug)] |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
24 pub enum Message { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
25 Sensor {wort: Option<f32>, fridge: Option<f32>}, |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
26 Params (Params), |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
27 Tick(u64), |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
28 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
29 |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
30 pub struct Fridge { |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
31 params: Params, |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
32 config: Config, |
620 | 33 |
34 on: bool, | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
35 temp_wort: Option<f32>, |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
36 temp_fridge: Option<f32>, |
620 | 37 last_off_time: Instant, |
38 wort_valid_time: Instant, | |
39 integrator: StepIntegrator, | |
621 | 40 control: FridgeControl, |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
41 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
42 // Timeouts to wake ourselves up again |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
43 handle: Handle, |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
44 timeout_s: mpsc::Sender<u64>, |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
45 timeout_r: Option<mpsc::Receiver<u64>>, |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
46 ticker: u64, |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
47 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
48 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
49 impl Sink for Fridge { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
50 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
51 type SinkItem = Message; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
52 type SinkError = TemplogError; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
53 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
54 fn start_send(&mut self, msg: Message) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
55 -> futures::StartSend<Self::SinkItem, Self::SinkError> { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
56 self.process_msg(msg); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
57 Ok(futures::AsyncSink::Ready) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
58 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
59 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
60 fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
61 Ok(futures::Async::Ready(())) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
62 } |
592 | 63 } |
64 | |
621 | 65 enum FridgeControl { |
66 #[cfg(target_os = "linux")] | |
67 Gpio(Pin), | |
68 Fake, | |
69 } | |
70 | |
71 struct TestControl {} | |
72 | |
620 | 73 impl Drop for Fridge { |
74 fn drop(&mut self) { | |
75 // safety fridge off | |
621 | 76 self.turn(false); |
620 | 77 } |
78 } | |
79 | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
80 impl Fridge { |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
81 pub fn new(config: &Config, nowait: bool, p: Params, handle: &Handle) -> Fridge { |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
82 let (s, r) = mpsc::channel(1); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
83 let mut f = Fridge { |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
84 config: config.clone(), |
615 | 85 params: p.clone(), |
620 | 86 on: false, |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
87 temp_wort: None, |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
88 temp_fridge: None, |
620 | 89 last_off_time: Instant::now(), |
90 wort_valid_time: Instant::now() - Duration::new(config.FRIDGE_WORT_INVALID_TIME, 100), | |
621 | 91 integrator: StepIntegrator::new(Duration::new(p.overshoot_delay, 0)), |
92 control: Self::make_control(config), | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
93 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
94 handle: handle.clone(), |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
95 timeout_s: s, |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
96 timeout_r: Some(r), |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
97 ticker: 0, |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
98 }; |
620 | 99 |
597 | 100 if nowait { |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
606
diff
changeset
|
101 f.last_off_time -= Duration::new(config.FRIDGE_DELAY, 1); |
597 | 102 } |
620 | 103 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
104 f.tick(); |
620 | 105 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
106 f |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
107 } |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
108 |
621 | 109 #[cfg(target_os = "linux")] |
110 fn make_control(config: &Config) -> FridgeControl { | |
111 let mut pin = Pin(config.FRIDGE_GPIO_PIN); | |
112 // XXX better error handling? | |
113 pin.export().expect("Exporting fridge gpio failed"); | |
114 pin.set_direction(Direction::Low).expect("Fridge gpio direction failed"); | |
115 FridgeControl::Gpio(pin) | |
116 } | |
117 | |
118 #[cfg(not(target_os = "linux"))] | |
119 fn make_control(config: &Config) -> FridgeControl { | |
120 FridgeControl::Fake | |
121 } | |
122 | |
123 | |
124 | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
125 fn next_wakeup(&self) -> Duration { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
126 let millis = 400; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
127 let dur = Duration::from_millis(millis); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
128 dur |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
129 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
130 |
606 | 131 /// The fridge needs to periodically wake itself up, the returned |
132 /// stream of Tick messages does so. | |
133 /// Examples of wakeups events are | |
134 /// | |
135 /// * overshoot calculation | |
136 /// * minimum fridge-off time | |
137 /// * invalid wort timeout | |
138 /// All specified in next_wakeup() | |
139 pub fn wakeups(&mut self) | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
140 -> Box<Stream<Item=Message, Error=TemplogError>> { |
626 | 141 Box::new(mem::replace(&mut self.timeout_r, None) |
606 | 142 .expect("Fridge::wakeups() can only be called once") |
143 .map(|v| Message::Tick(v)) | |
626 | 144 .map_err(|e| TemplogError::new("wakeups() receive failed"))) |
606 | 145 } |
146 | |
620 | 147 fn turn_off(&mut self) { |
621 | 148 info!("Turning fridge off"); |
620 | 149 self.turn(false); |
150 } | |
151 | |
152 fn turn_on(&mut self) { | |
621 | 153 info!("Turning fridge on"); |
620 | 154 self.turn(true); |
155 } | |
156 | |
157 fn turn(&mut self, on: bool) { | |
621 | 158 match self.control { |
159 #[cfg(target_os = "linux")] | |
160 Gpio(pin) => pin.set_value(on as u8), | |
161 FridgeControl::Fake => debug!("fridge turns {}", if on {"on"} else {"off"}), | |
162 } | |
620 | 163 self.on = on; |
164 } | |
165 | |
166 /// Turns the fridge off and on | |
167 fn compare_temperatures(&mut self) { | |
168 let fridge_min = self.params.fridge_setpoint - self.params.fridge_range_lower; | |
169 let fridge_max = self.params.fridge_setpoint - self.params.fridge_range_upper; | |
170 let wort_max = self.params.fridge_setpoint + self.params.fridge_difference; | |
171 let off_time = Instant::now() - self.last_off_time; | |
172 | |
173 // Or elsewhere? | |
621 | 174 self.integrator.set_limit(Duration::new(self.params.overshoot_delay, 0)); |
620 | 175 |
176 // Safety to avoid bad things happening to the fridge motor (?) | |
177 // When it turns off don't start up again for at least FRIDGE_DELAY | |
621 | 178 if !self.on && off_time < Duration::new(self.config.FRIDGE_DELAY, 0) { |
179 info!("fridge skipping, too early"); | |
620 | 180 return; |
181 } | |
182 | |
183 if self.params.disabled { | |
184 if self.on { | |
621 | 185 info!("Disabled, turning fridge off"); |
620 | 186 self.turn_off(); |
187 } | |
188 return; | |
189 } | |
190 | |
191 // handle broken wort sensor | |
192 if self.temp_wort.is_none() { | |
193 let invalid_time = Instant::now() - self.wort_valid_time; | |
194 warn!("Invalid wort sensor for {:?} secs", invalid_time); | |
621 | 195 if invalid_time < Duration::new(self.config.FRIDGE_WORT_INVALID_TIME, 0) { |
620 | 196 warn!("Has only been invalid for {:?}, waiting", invalid_time); |
197 return; | |
198 } | |
199 } | |
200 | |
201 if self.temp_fridge.is_none() { | |
202 warn!("Invalid fridge sensor"); | |
203 } | |
204 | |
205 if self.on { | |
206 debug!("fridge is on"); | |
207 let on_time = self.integrator.integrate().as_secs() as f32; | |
621 | 208 let on_ratio = on_time / self.params.overshoot_delay as f32; |
620 | 209 |
621 | 210 let overshoot = self.params.overshoot_factor as f32 * on_ratio; |
211 debug!("on_percent {}, overshoot {}", on_ratio * 100.0, overshoot); | |
620 | 212 |
213 let mut turn_off = false; | |
621 | 214 if self.temp_wort.is_some() && !self.params.nowort { |
215 let t = self.temp_wort.unwrap(); | |
620 | 216 // use the wort temperature |
621 | 217 if t - overshoot < self.params.fridge_setpoint { |
218 info!("wort has cooled enough, {temp}º (overshoot {overshoot}º = {factor} × {percent}%)", | |
620 | 219 temp = t, overshoot = overshoot, |
621 | 220 factor = self.params.overshoot_factor, |
221 percent = on_ratio*100.0); | |
620 | 222 turn_off = true; |
223 } | |
621 | 224 } else if let Some(t) = self.temp_fridge { |
620 | 225 // use the fridge temperature |
226 if t < fridge_min { | |
227 warn!("fridge off fallback, fridge {}, min {}", t, fridge_min); | |
228 if self.temp_wort.is_none() { | |
621 | 229 warn!("wort has been invalid for {:?}", Instant::now() - self.wort_valid_time); |
620 | 230 } |
231 turn_off = true; | |
232 } | |
233 } | |
234 if turn_off { | |
235 self.turn_off(); | |
236 } | |
237 } else { | |
238 debug!("fridge is off. fridge {:?} max {:?}. wort {:?} max {:?}", | |
239 self.temp_fridge, fridge_max, self.temp_wort, wort_max); | |
240 let mut turn_on = false; | |
621 | 241 if self.temp_wort.is_some() && !self.params.nowort { |
620 | 242 // use the wort temperature |
621 | 243 let t = self.temp_wort.unwrap(); |
620 | 244 if t >= wort_max { |
621 | 245 info!("Wort is too hot {}°, max {}°", t, wort_max); |
620 | 246 turn_on = true; |
247 } | |
248 } | |
249 | |
250 if let Some(t) = self.temp_fridge { | |
251 if t >= fridge_max { | |
621 | 252 warn!("fridge too hot fallback, fridge {}°, max {}°", t, fridge_max); |
620 | 253 turn_on = true; |
254 } | |
255 } | |
256 | |
257 if turn_on { | |
258 self.turn_on() | |
259 } | |
260 } | |
261 } | |
262 | |
606 | 263 /// Must be called after every state change. Turns the fridge on/off as required and |
264 /// schedules any future wakeups based on the present (new) state | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
265 fn tick(&mut self) { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
266 debug!("tick"); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
267 |
621 | 268 self.compare_temperatures(); |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
269 self.send_next_timeout(); |
592 | 270 } |
271 | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
272 /// Sets the next self-wakeup timeout |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
273 fn send_next_timeout(&mut self) { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
274 let waker = self.timeout_s.clone(); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
275 let dur = self.next_wakeup(); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
276 debug!("fridge next wakeup {:?}", dur); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
277 self.ticker += 1; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
278 let v = self.ticker; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
279 let t = Timeout::new(dur, &self.handle).unwrap() |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
280 .map_err(|_| ()) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
281 .and_then(move |_| { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
282 waker.send(v) |
595 | 283 .map_err(|e| { |
284 warn!("Send error in tick(): {}", e.to_string()); | |
285 () | |
286 }) | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
287 }) |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
288 .map(|_| ()); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
289 self.handle.spawn(t); |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
290 } |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
291 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
606
diff
changeset
|
292 fn process_msg(&mut self, msg: Message) { |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
293 debug!("process_msg {:?}", msg); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
294 match msg { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
295 Message::Sensor{wort, fridge} => self.update_sensor(wort, fridge), |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
296 Message::Params(p) => self.update_params(p), |
606 | 297 Message::Tick(v) => if v == self.ticker {self.tick()}, // schedule a timeout if there are none pending |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
298 }; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
299 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
300 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
301 pub fn update_params(&mut self, p: Params) { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
302 self.params = p; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
303 println!("fridge set_params {:?}", self.params); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
304 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
305 self.tick(); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
306 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
307 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
308 pub fn update_sensor(&mut self, wort: Option<f32>, fridge: Option<f32>) { |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
309 self.temp_wort = wort; |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
310 self.temp_fridge = fridge; |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
311 |
620 | 312 if self.temp_wort.is_some() { |
606 | 313 self.wort_valid_time = Instant::now(); |
620 | 314 } |
606 | 315 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
316 self.tick(); |
592 | 317 } |
318 | |
319 } |