# HG changeset patch # User Matt Johnston # Date 1482771088 -28800 # Node ID bf138339d20a0e090c3b805e66ad56e7e56616b2 # Parent 03b48ec0bb03d25fbcd5fb1bda3db03a8eefc612 fiddling with timeouts and closures diff -r 03b48ec0bb03 -r bf138339d20a rust/src/configwaiter.rs --- 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::(); - p - } - - pub fn new() -> Self { - ConfigWaiter {} - } - - pub fn run(handle: &Handle, rate: u64) -> Box> { - let mut s = ConfigWaiter::new(); - - let dur = Duration::from_millis(rate); - Interval::new(dur, handle).unwrap().map(move |()| { - s.step() - }).boxed() - } -} - diff -r 03b48ec0bb03 -r bf138339d20a rust/src/fridge.rs --- 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, + temp_fridge: Option, + + // timeouts to wake ourself up again + //overshoot_timeout: Option>, + //fridgeoff_timeout: Option>, + wortvalid_timeout: Option, } -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, fridge: Option) { + 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); } } diff -r 03b48ec0bb03 -r bf138339d20a rust/src/main.rs --- 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(¶mh.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(¶mh.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); } diff -r 03b48ec0bb03 -r bf138339d20a rust/src/paramwaiter.rs --- /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::(); + p + } + + pub fn new() -> Self { + ParamWaiter {} + } + + pub fn run(handle: &Handle, rate: u64) -> Box> { + let mut s = ParamWaiter::new(); + + let dur = Duration::from_millis(rate); + Interval::new(dur, handle).unwrap().map(move |()| { + s.step() + }).boxed() + } +} + diff -r 03b48ec0bb03 -r bf138339d20a rust/src/types.rs --- 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>, } @@ -65,6 +66,11 @@ temps: Vec::new(), } } + + pub fn push(&mut self, vals: Vec) { + self.temps.push(vals); + } + pub fn fridge(&self) -> Option { unimplemented!(); }