changeset 593:bf138339d20a rust

fiddling with timeouts and closures
author Matt Johnston <matt@ucc.asn.au>
date Tue, 27 Dec 2016 00:51:28 +0800
parents 03b48ec0bb03
children aff50ee77252
files rust/src/configwaiter.rs rust/src/fridge.rs rust/src/main.rs rust/src/paramwaiter.rs rust/src/types.rs
diffstat 5 files changed, 113 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/rust/src/configwaiter.rs	Sat Dec 24 00:14:58 2016 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-extern crate tokio_core;
-extern crate futures;
-extern crate rand;
-
-use std::time::Duration;
-use std::io;
-
-use tokio_core::reactor::Interval;
-use tokio_core::reactor::Handle;
-use futures::Stream;
-use types::*;
-
-pub struct ConfigWaiter {
-}
-
-impl ConfigWaiter {
-    fn step(&mut self) -> ParamHolder {
-        let mut p = ParamHolder::new();
-        let mut rng = rand::thread_rng();
-        p.p.fridge_setpoint = rand::random::<f32>();
-        p
-    }
-
-    pub fn new() -> Self {
-        ConfigWaiter {}
-    }
-
-    pub fn run(handle: &Handle, rate: u64) -> Box<Stream<Item=ParamHolder, Error=io::Error>> {
-        let mut s = ConfigWaiter::new();
-
-        let dur = Duration::from_millis(rate);
-        Interval::new(dur, handle).unwrap().map(move |()| {
-            s.step()
-        }).boxed()
-    }
-}
-
--- a/rust/src/fridge.rs	Sat Dec 24 00:14:58 2016 +0800
+++ b/rust/src/fridge.rs	Tue Dec 27 00:51:28 2016 +0800
@@ -1,16 +1,65 @@
+use std::io;
+use std::time::Duration;
+
+use futures::Future;
+use tokio_core::reactor::Timeout;
+use tokio_core::reactor::Handle;
+
 use types::*;
 
-pub struct Fridge<'a> {
-    params: &'a Params,
+pub struct Fridge {
+    params: Params,
+    temp_wort: Option<f32>,
+    temp_fridge: Option<f32>,
+
+    // timeouts to wake ourself up again
+    //overshoot_timeout: Option<Future<Item=(), Error=io::Error>>,
+    //fridgeoff_timeout: Option<Future<Item=(), Error=io::Error>>,
+    wortvalid_timeout: Option<Timeout>,
 }
 
-impl<'a> Fridge<'a> {
-    pub fn new(p: &'a Params) -> Fridge<'a> {
-        Fridge { params: p }
+impl Fridge {
+    pub fn new(p: Params) -> Fridge {
+        Fridge { 
+            params: p,
+            temp_wort: None,
+            temp_fridge: None,
+            //overshoot_timeout: None,
+            //fridgeoff_timeout: None,
+            wortvalid_timeout: None,
+        }
+    }
+
+    fn tick(&mut self, handle: &Handle) {
+
     }
 
-    pub fn set_params(&mut self, p: &'a Params) {
+    pub fn set_params(&mut self, handle: &Handle, p: Params) {
         self.params = p;
+        println!("params {:?}", self.params);
+
+        self.tick(handle);
+    }
+
+    pub fn set_temps(&mut self, handle: &Handle, 
+                wort: Option<f32>, fridge: Option<f32>) {
+        self.temp_wort = wort;
+        self.temp_fridge = fridge;
+
+        if let Some(_) = self.temp_wort {
+            // set a new timeout, replacing any existing
+            let dur = Duration::new(10, 0); // XXX
+            let t = Timeout::new(dur, handle).unwrap();
+            /*
+            handle.spawn(t.and_then(|_| {
+                                self.tick(handle);
+                                Ok(())
+                            }).map_err(|x| ()));
+                            */
+            self.wortvalid_timeout = Some(t);
+        }
+
+        self.tick(handle);
     }
 
 }
--- a/rust/src/main.rs	Sat Dec 24 00:14:58 2016 +0800
+++ b/rust/src/main.rs	Tue Dec 27 00:51:28 2016 +0800
@@ -9,43 +9,39 @@
 mod sensor;
 mod fridge;
 mod types;
-mod configwaiter;
+mod paramwaiter;
 
 use types::*;
 
 fn main() {
     println!("Wort Templog");
 
-    let paramh = ParamHolder::new();
+    let mut paramh = ParamHolder::new();
     let mut readings = Readings::new();
-    let mut fridge = fridge::Fridge::new(&paramh.p);
+    let mut fridge = fridge::Fridge::new(paramh.p.clone());
 
     let mut core = Core::new().unwrap();
     let handle = core.handle();
 
     let s = sensor::Sensor::run(&handle, 400, "sens1".to_string());
-    let t = sensor::Sensor::run(&handle, 747, "frid".to_string());
-    let w = configwaiter::ConfigWaiter::run(&handle, 3000);
+    let w = paramwaiter::ParamWaiter::run(&handle, 3000);
 
-    let h = s.for_each(|r| {
-        fridge.set_params(&paramh.p);
-        println!("readings {:?}", r);
+    let h = s.for_each(move |r| {
+        readings.push(r);
+        println!("readings {:?}", readings);
         Ok(())
     });
 
-    let i = t.for_each(|r| {
-        println!("fridgereadings {:?}", r);
+    let j = w.for_each(move |p| {
+        fridge.set_params(&handle, p.clone());
+        paramh.p = p;
         Ok(())
     });
 
-    let j = w.for_each(move |ph| {
-        let paramh = ph;
-        fridge.set_params(&ph.p);
-        Ok(())
-    });
+    handle.spawn(h.map_err(|x| ()));
+    handle.spawn(j.map_err(|x| ()));
 
-    let all = h.select(j);
-
-    core.run(all);
+    let forever = futures::empty::<(),()>();
+    core.run(forever);
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/src/paramwaiter.rs	Tue Dec 27 00:51:28 2016 +0800
@@ -0,0 +1,37 @@
+extern crate tokio_core;
+extern crate futures;
+extern crate rand;
+
+use std::time::Duration;
+use std::io;
+
+use tokio_core::reactor::Interval;
+use tokio_core::reactor::Handle;
+use futures::Stream;
+use types::*;
+
+pub struct ParamWaiter {
+}
+
+impl ParamWaiter {
+    fn step(&mut self) -> Params {
+        let mut p = Params::defaults();
+        let mut rng = rand::thread_rng();
+        p.fridge_setpoint = 17.0 + 4.0*rand::random::<f32>();
+        p
+    }
+
+    pub fn new() -> Self {
+        ParamWaiter {}
+    }
+
+    pub fn run(handle: &Handle, rate: u64) -> Box<Stream<Item=Params, Error=io::Error>> {
+        let mut s = ParamWaiter::new();
+
+        let dur = Duration::from_millis(rate);
+        Interval::new(dur, handle).unwrap().map(move |()| {
+            s.step()
+        }).boxed()
+    }
+}
+
--- a/rust/src/types.rs	Sat Dec 24 00:14:58 2016 +0800
+++ b/rust/src/types.rs	Tue Dec 27 00:51:28 2016 +0800
@@ -1,4 +1,4 @@
-#[derive(RustcDecodable, RustcEncodable, Debug)]
+#[derive(RustcDecodable, RustcEncodable, Debug, Clone)]
 pub struct Params {
     pub fridge_setpoint: f32,
     pub fridge_difference: f32,
@@ -55,6 +55,7 @@
     }
 }
 
+#[derive(Debug)]
 pub struct Readings {
     temps: Vec<Vec<Reading>>,
 }
@@ -65,6 +66,11 @@
             temps: Vec::new(),
         }
     }
+
+    pub fn push(&mut self, vals: Vec<Reading>) {
+        self.temps.push(vals);
+    }
+
     pub fn fridge(&self) -> Option<f32> {
         unimplemented!();
     }