diff rust/src/sensor.rs @ 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 8c21df3711e2
line wrap: on
line diff
--- 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()