comparison 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
comparison
equal deleted inserted replaced
600:9c76f3cf01ea 601:8c21df3711e2
1 extern crate tokio_core; 1 extern crate tokio_core;
2 extern crate futures; 2 extern crate futures;
3 extern crate futures_cpupool;
3 4
4 use std::time::Duration; 5 use std::time::Duration;
5 use std::io; 6 use std::io;
6 use std::fs::File; 7 use std::fs::File;
7 use std::io::Read; 8 use std::io::{Read,BufReader,BufRead};
9 use std::path::PathBuf;
10 use std::error::Error;
8 11
9 use tokio_core::reactor::Interval; 12 use tokio_core::reactor::Interval;
10 use tokio_core::reactor::Handle; 13 use tokio_core::reactor::Handle;
11 use futures::Stream; 14 use futures::Stream;
12 use types::*; 15 use types::*;
13 use config::Config; 16
17 use ::rigid_config;
14 18
15 pub trait Sensor { 19 pub trait Sensor {
16 fn stream(handle: &Handle, config: &Config) 20 fn stream(handle: &Handle)
17 -> Box<Stream<Item=Readings, Error=io::Error>>; 21 -> Box<Stream<Item=Readings, Error=io::Error>>;
18 } 22 }
19 23
20 pub struct OneWireSensor { 24 pub struct OneWireSensor {
21 config: Config,
22 } 25 }
23 26
24 impl OneWireSensor { 27 impl OneWireSensor {
25 fn new(config: &Config) -> OneWireSensor { 28 fn new() -> OneWireSensor {
26 OneWireSensor { 29 OneWireSensor {
27 config: config.clone(),
28 } 30 }
29 } 31 }
30 32
31 fn step(&mut self) -> Readings { 33 fn step() -> Readings {
32 let mut r = Readings::new(); 34 let mut r = Readings::new();
35
36
33 r.add("ambient", Some(31.2)); 37 r.add("ambient", Some(31.2));
34 r.add("wort_todo", Some(8.0)); 38 r.add("wort_todo", Some(8.0));
35 debug!("sensor step {:?}", r); 39 debug!("sensor step {:?}", r);
36 r 40 r
37 } 41 }
38 42
39 fn sensor_names(&self) -> Vec<String> { 43 fn sensor_names(&self) -> Result<Vec<String>, Box<Error>> {
40 let mut names = vec![]; 44 let mut path = PathBuf::from(&rigid_config.SENSOR_BASE_DIR);
41 names 45 path.push("w1_master_slaves");
46
47 let f = BufReader::new(File::open(path)?);
48 let s = f.lines().collect::<Result<Vec<String>, io::Error>>()?;
49 Ok(s)
42 } 50 }
43 } 51 }
44 52
53 // does this need to be static?
54 lazy_static! {
55 static ref thread_pool : futures_cpupool::CpuPool = futures_cpupool::CpuPool::new(3); // TODO: how many?
56 }
57
45 impl Sensor for OneWireSensor { 58 impl Sensor for OneWireSensor {
46 fn stream(handle: &Handle, config: &Config) 59 fn stream(handle: &Handle)
47 -> Box<Stream<Item=Readings, Error=io::Error>> { 60 -> Box<Stream<Item=Readings, Error=io::Error>> {
48 let mut s = OneWireSensor::new(config); 61 let mut s = OneWireSensor::new();
49 62
50 let dur = Duration::new(s.config.SENSOR_SLEEP,0); 63 let dur = Duration::new(rigid_config.SENSOR_SLEEP,0);
51 Interval::new(dur, handle).unwrap().map(move |()| { 64 Interval::new(dur, handle).unwrap()
52 s.step() 65 .and_then(|()| {
53 }).boxed() 66 thread_pool.spawn_fn(|| Ok(Self::step()))
67 }).boxed()
54 } 68 }
55 } 69 }
56 70
57 pub struct TestSensor { 71 pub struct TestSensor {
58 config: Config,
59 } 72 }
60 73
61 impl TestSensor { 74 impl TestSensor {
62 pub fn new(config: &Config) -> Self { 75 pub fn new() -> Self {
63 TestSensor { 76 TestSensor {}
64 config: config.clone(),
65 }
66 } 77 }
67 78
68 fn step(&mut self) -> Readings { 79 fn step(&mut self) -> Readings {
69 let mut r = Readings::new(); 80 let mut r = Readings::new();
70 r.add("ambient", Some(31.2)); 81 r.add("ambient", Some(31.2));
71 r.add("wort", Some(Self::try_read("test_wort.txt").unwrap_or_else(|_| 18.0))); 82 r.add("wort", Some(Self::try_read("test_wort.txt").unwrap_or_else(|_| 18.0)));
72 r.add("fridge", Some(Self::try_read("test_fridge.txt").unwrap_or_else(|_| 20.0))); 83 r.add("fridge", Some(Self::try_read("test_fridge.txt").unwrap_or_else(|_| 20.0)));
73 r 84 r
74 } 85 }
75 86
76 fn try_read(filename: &str) -> Result<f32, String> { 87 fn try_read(filename: &str) -> Result<f32, Box<Error>> {
77 File::open(filename) 88 let mut s = String::new();
78 .map_err(|e| e.to_string()) 89 File::open(filename)?.read_to_string(&mut s)?;
79 .and_then(|mut f| { 90 Ok(s.trim().parse::<f32>()?)
80 let mut s = String::new();
81 f.read_to_string(&mut s)
82 .map_err(|e| e.to_string())
83 .map(|_| s)
84 })
85 .and_then(|s| {
86 s.trim().parse::<f32>()
87 .map_err(|e| e.to_string())
88 })
89 } 91 }
90 } 92 }
91 93
92 impl Sensor for TestSensor { 94 impl Sensor for TestSensor {
93 fn stream(handle: &Handle, config: &Config) 95 fn stream(handle: &Handle)
94 -> Box<Stream<Item=Readings, Error=io::Error>> { 96 -> Box<Stream<Item=Readings, Error=io::Error>> {
95 let mut s = TestSensor::new(config); 97 let mut s = TestSensor::new();
96 98
97 let dur = Duration::new(s.config.SENSOR_SLEEP,0); 99 let dur = Duration::new(rigid_config.SENSOR_SLEEP,0);
98 Interval::new(dur, handle).unwrap().map(move |()| { 100 Interval::new(dur, handle).unwrap().map(move |()| {
99 s.step() 101 s.step()
100 }).boxed() 102 }).boxed()
101 } 103 }
102 } 104 }