changeset 592:03b48ec0bb03 rust

fridge, types, configwaiter
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Dec 2016 00:14:58 +0800
parents 4a944663fa8d
children bf138339d20a
files rust/Cargo.lock rust/Cargo.toml rust/src/configwaiter.rs rust/src/fridge.rs rust/src/main.rs rust/src/sensor.rs rust/src/types.rs
diffstat 7 files changed, 159 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/rust/Cargo.lock	Fri Dec 23 00:33:19 2016 +0800
+++ b/rust/Cargo.lock	Sat Dec 24 00:14:58 2016 +0800
@@ -3,6 +3,7 @@
 version = "0.1.0"
 dependencies = [
  "futures 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
  "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
  "tokio-core 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
@@ -102,6 +103,14 @@
 ]
 
 [[package]]
+name = "rand"
+version = "0.3.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
 name = "rustc-serialize"
 version = "0.3.22"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -177,6 +186,7 @@
 "checksum miow 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d5bfc6782530ac8ace97af10a540054a37126b63b0702ddaaa243b73b5745b9a"
 "checksum net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)" = "5edf9cb6be97212423aed9413dd4729d62b370b5e1c571750e882cebbbc1e3e2"
 "checksum nix 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a0d95c5fa8b641c10ad0b8887454ebaafa3c92b5cd5350f8fc693adafd178e7b"
+"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
 "checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b"
 "checksum rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "c5f5376ea5e30ce23c03eb77cbe4962b988deead10910c372b226388b594c084"
 "checksum scoped-tls 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f417c22df063e9450888a7561788e9bd46d3bb3c1466435b4eccb903807f147d"
--- a/rust/Cargo.toml	Fri Dec 23 00:33:19 2016 +0800
+++ b/rust/Cargo.toml	Sat Dec 24 00:14:58 2016 +0800
@@ -6,5 +6,5 @@
 [dependencies]
 futures = "0.1.6"
 tokio-core = "0.1.1"
-rustc-serialize = "0.3.*"
-
+rustc-serialize = "0.3"
+rand = "0.3"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/src/configwaiter.rs	Sat Dec 24 00:14:58 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 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()
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/src/fridge.rs	Sat Dec 24 00:14:58 2016 +0800
@@ -0,0 +1,16 @@
+use types::*;
+
+pub struct Fridge<'a> {
+    params: &'a Params,
+}
+
+impl<'a> Fridge<'a> {
+    pub fn new(p: &'a Params) -> Fridge<'a> {
+        Fridge { params: p }
+    }
+
+    pub fn set_params(&mut self, p: &'a Params) {
+        self.params = p;
+    }
+
+}
--- a/rust/src/main.rs	Fri Dec 23 00:33:19 2016 +0800
+++ b/rust/src/main.rs	Sat Dec 24 00:14:58 2016 +0800
@@ -7,74 +7,28 @@
 use rustc_serialize::json;
 
 mod sensor;
-
-#[derive(RustcDecodable, RustcEncodable)]
-struct Params {
-    fridge_setpoint: f32,
-    fridge_difference: f32,
-    overshoot_delay: u32,
-    overshoot_factor: f32,
-    disabled: bool,
-    nowort: bool,
-    fridge_range_lower: f32,
-    fridge_range_upper: f32,
-}
-
-struct ParamHolder {
-    p: Params,
-    epoch: String, // XXX or a byte array?
-}
+mod fridge;
+mod types;
+mod configwaiter;
 
-impl ParamHolder {
-    fn new() -> ParamHolder {
-        ParamHolder {
-            p: Params {
-                fridge_setpoint: 16.0,
-                fridge_difference: 0.2,
-                overshoot_delay: 720, // 12 minutes
-                overshoot_factor: 1.0,
-                disabled: false,
-                nowort: false,
-                fridge_range_lower: 3.0,
-                fridge_range_upper: 3.0,
-            },
-            epoch: String::new(),
-        }
-    }
-}
-
-struct Readings {
-    temps: Vec<Vec<sensor::Reading>>,
-}
-
-impl Readings {
-    fn new() -> Readings {
-        Readings {
-            temps: Vec::new(),
-        }
-    }
-    fn fridge() -> Option<f32> {
-        unimplemented!();
-    }
-
-    fn wort() -> Option<f32> {
-        unimplemented!();
-    }
-}
+use types::*;
 
 fn main() {
     println!("Wort Templog");
 
-    let param = ParamHolder::new();
+    let paramh = ParamHolder::new();
     let mut readings = Readings::new();
+    let mut fridge = fridge::Fridge::new(&paramh.p);
 
     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 h = s.for_each(|r| {
+        fridge.set_params(&paramh.p);
         println!("readings {:?}", r);
         Ok(())
     });
@@ -84,7 +38,13 @@
         Ok(())
     });
 
-    let all = h.select(i);
+    let j = w.for_each(move |ph| {
+        let paramh = ph;
+        fridge.set_params(&ph.p);
+        Ok(())
+    });
+
+    let all = h.select(j);
 
     core.run(all);
 }
--- a/rust/src/sensor.rs	Fri Dec 23 00:33:19 2016 +0800
+++ b/rust/src/sensor.rs	Sat Dec 24 00:14:58 2016 +0800
@@ -7,12 +7,7 @@
 use tokio_core::reactor::Interval;
 use tokio_core::reactor::Handle;
 use futures::Stream;
-
-#[derive(Debug)]
-pub struct Reading {
-    name: String,
-    value: Option<f32>,
-}
+use types::*;
 
 pub struct Sensor {
     current: f32,
@@ -23,8 +18,8 @@
     fn step(&mut self) -> Vec<Reading> {
         let mut r = Vec::new();
         self.current = self.current + 0.1;
-        r.push(Reading { name: "aaa".to_string() + &self.suf, value: Some(self.current) });
-        r.push(Reading { name: "b".to_string() + &self.suf, value: Some(self.current/3.0) });
+        r.push(Reading::new("aaa".to_string() + &self.suf, self.current));
+        r.push(Reading::new("b".to_string() + &self.suf, self.current/3.0));
         r
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/src/types.rs	Sat Dec 24 00:14:58 2016 +0800
@@ -0,0 +1,76 @@
+#[derive(RustcDecodable, RustcEncodable, Debug)]
+pub struct Params {
+    pub fridge_setpoint: f32,
+    pub fridge_difference: f32,
+    pub overshoot_delay: u32,
+    pub overshoot_factor: f32,
+    pub disabled: bool,
+    pub nowort: bool,
+    pub fridge_range_lower: f32,
+    pub fridge_range_upper: f32,
+}
+
+impl Params {
+    pub fn defaults() -> Params {
+        Params {
+            fridge_setpoint: 16.0,
+            fridge_difference: 0.2,
+            overshoot_delay: 720, // 12 minutes
+            overshoot_factor: 1.0,
+            disabled: false,
+            nowort: false,
+            fridge_range_lower: 3.0,
+            fridge_range_upper: 3.0,
+            }
+    }
+}
+
+#[derive(Debug)]
+pub struct ParamHolder {
+    pub p: Params,
+    epoch: String, // XXX or a byte array?
+}
+
+impl ParamHolder {
+    pub fn new() -> ParamHolder {
+        ParamHolder {
+            p: Params::defaults(),
+            epoch: String::new(),
+        }
+    }
+}
+
+#[derive(Debug)]
+pub struct Reading {
+    name: String,
+    value: Option<f32>,
+}
+
+impl Reading {
+    pub fn new(name: String, value: f32) -> Reading {
+        Reading { name: name, value: Some(value) }
+    }
+    pub fn new_none(name: String) -> Reading {
+        Reading { name: name, value: None }
+    }
+}
+
+pub struct Readings {
+    temps: Vec<Vec<Reading>>,
+}
+
+impl Readings {
+    pub fn new() -> Readings {
+        Readings {
+            temps: Vec::new(),
+        }
+    }
+    pub fn fridge(&self) -> Option<f32> {
+        unimplemented!();
+    }
+
+    pub fn wort(&self) -> Option<f32> {
+        unimplemented!();
+    }
+}
+