diff 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
line wrap: on
line diff
--- a/rust/src/fridge.rs	Fri Feb 17 21:38:36 2017 +0800
+++ b/rust/src/fridge.rs	Fri Feb 17 21:38:45 2017 +0800
@@ -32,6 +32,7 @@
     timeout_r: Option<mpsc::Receiver<u64>>,
     ticker: u64,
     last_off_time: Instant,
+    wort_valid_time: Instant,
 }
 
 impl Sink for Fridge {
@@ -64,6 +65,7 @@
             timeout_r: Some(r),
             ticker: 0,
             last_off_time: Instant::now(),
+            wort_valid_time: Instant::now() - Duration::new(config.FRIDGE_WORT_INVALID_TIME, 100),
         };
         if nowait {
             f.last_off_time -= Duration::new(config.FRIDGE_DELAY, 100);
@@ -72,28 +74,31 @@
         f
     }
 
-    /// The fridge needs to periodically wake itself up, the returned
-    // stream of Tick messages does so.
-    /// Examples of wakeups events are
-    /// 
-    ///  * overshoot calculation
-    ///  * minimum fridge-off time
-    ///  * invalid wort timeout
-    pub fn wakeups(&mut self)
-            -> Box<Stream<Item=Message, Error=io::Error>> {
-        mem::replace(&mut self.timeout_r, None)
-            .expect("NumberWatcher::timeouts() can only be called once")
-            .map(|v| Message::Tick(v))
-            .map_err(|_| io::Error::new(io::ErrorKind::Other, "Something wrong with watcher timeout channel"))
-            .boxed()
-    }
-
     fn next_wakeup(&self) -> Duration {
         let millis = 400;
         let dur = Duration::from_millis(millis);
         dur
     }
 
+    /// The fridge needs to periodically wake itself up, the returned
+    /// stream of Tick messages does so.
+    /// Examples of wakeups events are
+    /// 
+    ///  * overshoot calculation
+    ///  * minimum fridge-off time
+    ///  * invalid wort timeout
+    /// All specified in next_wakeup()
+    pub fn wakeups(&mut self)
+            -> Box<Stream<Item=Message, Error=io::Error>> {
+        mem::replace(&mut self.timeout_r, None)
+            .expect("Fridge::wakeups() can only be called once")
+            .map(|v| Message::Tick(v))
+            .map_err(|_| io::Error::new(io::ErrorKind::Other, "Something wrong with watcher timeout channel"))
+            .boxed()
+    }
+
+    /// Must be called after every state change. Turns the fridge on/off as required and
+    /// schedules any future wakeups based on the present (new) state
     fn tick(&mut self) {
         debug!("tick");
 
@@ -126,7 +131,7 @@
         match msg {
             Message::Sensor{wort, fridge} => self.update_sensor(wort, fridge),
             Message::Params(p) => self.update_params(p),
-            Message::Tick(v) => if v == self.ticker {self.tick()},
+            Message::Tick(v) => if v == self.ticker {self.tick()}, // schedule a timeout if there are none pending
         };
         future::ok::<(),()>(()).boxed()
     }
@@ -142,6 +147,10 @@
         self.temp_wort = wort;
         self.temp_fridge = fridge;
 
+	if self.temp_wort.is_some() {
+            self.wort_valid_time = Instant::now();
+	}
+
         self.tick();
     }