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