annotate rust/src/fridge.rs @ 620:8fda564cc46f rust

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