diff rust/src/sensor.rs @ 601:8c21df3711e2 rust

rigid_config more on sensors
author Matt Johnston <matt@ucc.asn.au>
date Wed, 15 Feb 2017 23:58:02 +0800
parents ca8102feaca6
children b45b8b4cf0f5
line wrap: on
line diff
--- a/rust/src/sensor.rs	Tue Feb 07 22:57:29 2017 +0800
+++ b/rust/src/sensor.rs	Wed Feb 15 23:58:02 2017 +0800
@@ -1,68 +1,79 @@
 extern crate tokio_core;
 extern crate futures;
+extern crate futures_cpupool;
 
 use std::time::Duration;
 use std::io;
 use std::fs::File;
-use std::io::Read;
+use std::io::{Read,BufReader,BufRead};
+use std::path::PathBuf;
+use std::error::Error;
 
 use tokio_core::reactor::Interval;
 use tokio_core::reactor::Handle;
 use futures::Stream;
 use types::*;
-use config::Config;
+
+use ::rigid_config;
 
 pub trait Sensor {
-    fn stream(handle: &Handle, config: &Config)
+    fn stream(handle: &Handle)
         -> Box<Stream<Item=Readings, Error=io::Error>>;
 }
 
 pub struct OneWireSensor {
-    config: Config,
 }
 
 impl OneWireSensor {
-    fn new(config: &Config) -> OneWireSensor {
+    fn new() -> OneWireSensor {
         OneWireSensor {
-            config: config.clone(),
         }
     }
 
-    fn step(&mut self) -> Readings {
+    fn step() -> Readings {
         let mut r = Readings::new();
+
+
         r.add("ambient", Some(31.2));
         r.add("wort_todo", Some(8.0));
         debug!("sensor step {:?}", r);
         r
     }
 
-    fn sensor_names(&self) -> Vec<String> {
-        let mut names = vec![];
-        names
+    fn sensor_names(&self) -> Result<Vec<String>, Box<Error>> {
+        let mut path = PathBuf::from(&rigid_config.SENSOR_BASE_DIR);
+        path.push("w1_master_slaves");
+
+        let f = BufReader::new(File::open(path)?);
+        let s = f.lines().collect::<Result<Vec<String>, io::Error>>()?;
+        Ok(s)
     }
 }
 
+// does this need to be static?
+lazy_static! {
+    static ref thread_pool : futures_cpupool::CpuPool = futures_cpupool::CpuPool::new(3); // TODO: how many?
+}
+
 impl Sensor for OneWireSensor {
-    fn stream(handle: &Handle, config: &Config)
+    fn stream(handle: &Handle)
         -> Box<Stream<Item=Readings, Error=io::Error>> {
-        let mut s = OneWireSensor::new(config);
+        let mut s = OneWireSensor::new();
 
-        let dur = Duration::new(s.config.SENSOR_SLEEP,0);
-        Interval::new(dur, handle).unwrap().map(move |()| {
-            s.step()
-        }).boxed()
+        let dur = Duration::new(rigid_config.SENSOR_SLEEP,0);
+        Interval::new(dur, handle).unwrap()
+            .and_then(|()| {
+                thread_pool.spawn_fn(|| Ok(Self::step()))
+            }).boxed()
     }
 }
 
 pub struct TestSensor {
-    config: Config,
 }
 
 impl TestSensor {
-    pub fn new(config: &Config) -> Self {
-        TestSensor {
-            config: config.clone(),
-        }
+    pub fn new() -> Self {
+        TestSensor {}
     }
 
     fn step(&mut self) -> Readings {
@@ -73,28 +84,19 @@
         r
     }
 
-    fn try_read(filename: &str) -> Result<f32, String> {
-        File::open(filename)
-            .map_err(|e| e.to_string())
-            .and_then(|mut f| {
-                let mut s = String::new();
-                f.read_to_string(&mut s)
-                    .map_err(|e| e.to_string())
-                    .map(|_| s)
-            })
-            .and_then(|s| {
-                    s.trim().parse::<f32>()
-                        .map_err(|e| e.to_string())
-            })
+    fn try_read(filename: &str) -> Result<f32, Box<Error>> {
+        let mut s = String::new();
+        File::open(filename)?.read_to_string(&mut s)?;
+        Ok(s.trim().parse::<f32>()?)
     }
 }
 
 impl Sensor for TestSensor {
-    fn stream(handle: &Handle, config: &Config)
+    fn stream(handle: &Handle)
         -> Box<Stream<Item=Readings, Error=io::Error>> {
-        let mut s = TestSensor::new(config);
+        let mut s = TestSensor::new();
 
-        let dur = Duration::new(s.config.SENSOR_SLEEP,0);
+        let dur = Duration::new(rigid_config.SENSOR_SLEEP,0);
         Interval::new(dur, handle).unwrap().map(move |()| {
             s.step()
         }).boxed()