changeset 596:ca8102feaca6 rust

sensor takes config parameter
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 Jan 2017 22:04:10 +0800
parents e87655ed8429
children a440eafa84a9
files rust/src/config.rs rust/src/main.rs rust/src/sensor.rs rust/src/types.rs
diffstat 4 files changed, 43 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/rust/src/config.rs	Thu Jan 05 23:26:00 2017 +0800
+++ b/rust/src/config.rs	Fri Jan 06 22:04:10 2017 +0800
@@ -1,44 +1,41 @@
 extern crate toml;
 
-use toml::{Encoder,Decoder};
-use serde::Serializer;
 use serde::Serialize;
 
-#[derive(Deserialize,Serialize,Debug)]
+#[derive(Deserialize,Serialize,Debug,Clone)]
 #[allow(non_snake_case)]
 pub struct Config {
-    FRIDGE_SLEEP: u32,
-    SENSOR_SLEEP: u32,
-    UPLOAD_SLEEP: u32,
+    pub FRIDGE_SLEEP: u64,
+    pub SENSOR_SLEEP: u64,
+    pub UPLOAD_SLEEP: u64,
 
-    FRIDGE_DELAY: u32,
-    FRIDGE_WORT_INVALID_TIME: u32,
+    pub FRIDGE_DELAY: u64,
+    pub FRIDGE_WORT_INVALID_TIME: u64,
 
-    MAX_READINGS: u32,
+    pub MAX_READINGS: u32,
 
-    PARAMS_FILE: String,
+    pub PARAMS_FILE: String,
 
-    SENSOR_BASE_DIR: String,
-    FRIDGE_GPIO_PIN: u32,
+    pub SENSOR_BASE_DIR: String,
+    pub FRIDGE_GPIO_PIN: u32,
 
-    AMBIENT_NAME: String,
-    FRIDGE_NAME: String,
-    WORT_NAME: String,
-    INTERNAL_TEMPERATURE: String,
+    pub AMBIENT_NAME: String,
+    pub FRIDGE_NAME: String,
+    pub WORT_NAME: String,
+    pub INTERNAL_TEMPERATURE: String,
 
-    HMAC_KEY: String,
-    SERVER_URL: String,
-    UPDATE_URL: String, 
-    SETTINGS_URL: String,
+    pub HMAC_KEY: String,
+    pub SERVER_URL: String,
+    pub UPDATE_URL: String, 
+    pub SETTINGS_URL: String,
 }
 
 impl Config {
 
     pub fn new() -> Self {
         Config {
-            FRIDGE_SLEEP: 60, // this value works. may affect the algorithm
-            SENSOR_SLEEP: 15, // same for this.
-            UPLOAD_SLEEP: 83, // nice and prime
+            SENSOR_SLEEP: 5,
+            UPLOAD_SLEEP: 83,
 
             FRIDGE_DELAY: 600, // 10 mins, to avoid fridge damage from frequent cycling off/on
             FRIDGE_WORT_INVALID_TIME: 300, // 5 mins
@@ -66,7 +63,7 @@
         }
     }
 
-    pub fn to_toml_string(self) -> String {
+    pub fn to_toml_string(&self) -> String {
         let mut e = toml::Encoder::new();
         self.serialize(&mut e).unwrap();
         toml::Value::Table(e.toml).to_string()
--- a/rust/src/main.rs	Thu Jan 05 23:26:00 2017 +0800
+++ b/rust/src/main.rs	Fri Jan 06 22:04:10 2017 +0800
@@ -47,9 +47,9 @@
     let sensor_r = sensor_r.map_err(|_| io::Error::new(io::ErrorKind::Other, "Problem with sensor_r channel"));
 
     let sensor_stream = if cfg!(feature = "testmode") {
-        sensor::TestSensor::stream(&handle)
+        sensor::TestSensor::stream(&handle, &config)
     } else {
-        sensor::OneWireSensor::stream(&handle)
+        sensor::OneWireSensor::stream(&handle, &config)
     };
 
     // Send the sensors of interest to the fridge (sensor_s),
--- a/rust/src/sensor.rs	Thu Jan 05 23:26:00 2017 +0800
+++ b/rust/src/sensor.rs	Fri Jan 06 22:04:10 2017 +0800
@@ -10,23 +10,22 @@
 use tokio_core::reactor::Handle;
 use futures::Stream;
 use types::*;
+use config::Config;
 
 pub trait Sensor {
-    fn stream(handle: &Handle)
+    fn stream(handle: &Handle, config: &Config)
         -> Box<Stream<Item=Readings, Error=io::Error>>;
 }
 
 pub struct OneWireSensor {
-    master_dir: String,
+    config: Config,
 }
 
 impl OneWireSensor {
-    fn new() -> OneWireSensor {
+    fn new(config: &Config) -> OneWireSensor {
         OneWireSensor {
-            master_dir: String::new(), // XXX
-            
+            config: config.clone(),
         }
-        // todo
     }
 
     fn step(&mut self) -> Readings {
@@ -37,18 +36,18 @@
         r
     }
 
-    fn sensor_names(self) -> Vec<String> {
+    fn sensor_names(&self) -> Vec<String> {
         let mut names = vec![];
         names
     }
 }
 
 impl Sensor for OneWireSensor {
-    fn stream(handle: &Handle)
+    fn stream(handle: &Handle, config: &Config)
         -> Box<Stream<Item=Readings, Error=io::Error>> {
-        let mut s = OneWireSensor::new();
+        let mut s = OneWireSensor::new(config);
 
-        let dur = Duration::from_millis(600);
+        let dur = Duration::new(s.config.SENSOR_SLEEP,0);
         Interval::new(dur, handle).unwrap().map(move |()| {
             s.step()
         }).boxed()
@@ -56,9 +55,16 @@
 }
 
 pub struct TestSensor {
+    config: Config,
 }
 
 impl TestSensor {
+    pub fn new(config: &Config) -> Self {
+        TestSensor {
+            config: config.clone(),
+        }
+    }
+
     fn step(&mut self) -> Readings {
         let mut r = Readings::new();
         r.add("ambient", Some(31.2));
@@ -84,11 +90,11 @@
 }
 
 impl Sensor for TestSensor {
-    fn stream(handle: &Handle)
+    fn stream(handle: &Handle, config: &Config)
         -> Box<Stream<Item=Readings, Error=io::Error>> {
-        let mut s = TestSensor {};
+        let mut s = TestSensor::new(config);
 
-        let dur = Duration::new(1,0);
+        let dur = Duration::new(s.config.SENSOR_SLEEP,0);
         Interval::new(dur, handle).unwrap().map(move |()| {
             s.step()
         }).boxed()
--- a/rust/src/types.rs	Thu Jan 05 23:26:00 2017 +0800
+++ b/rust/src/types.rs	Fri Jan 06 22:04:10 2017 +0800
@@ -2,7 +2,7 @@
 use std::time::Duration;
 use serde::{Deserialize,Serialize};
 
-#[derive(Deserialize, Serialize, Debug, Clone)]
+#[derive(Deserialize, Serialize, Debug)]
 pub struct Params {
     pub fridge_setpoint: f32,
     pub fridge_difference: f32,