Mercurial > templog
comparison 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 |
comparison
equal
deleted
inserted
replaced
595:e87655ed8429 | 596:ca8102feaca6 |
---|---|
8 | 8 |
9 use tokio_core::reactor::Interval; | 9 use tokio_core::reactor::Interval; |
10 use tokio_core::reactor::Handle; | 10 use tokio_core::reactor::Handle; |
11 use futures::Stream; | 11 use futures::Stream; |
12 use types::*; | 12 use types::*; |
13 use config::Config; | |
13 | 14 |
14 pub trait Sensor { | 15 pub trait Sensor { |
15 fn stream(handle: &Handle) | 16 fn stream(handle: &Handle, config: &Config) |
16 -> Box<Stream<Item=Readings, Error=io::Error>>; | 17 -> Box<Stream<Item=Readings, Error=io::Error>>; |
17 } | 18 } |
18 | 19 |
19 pub struct OneWireSensor { | 20 pub struct OneWireSensor { |
20 master_dir: String, | 21 config: Config, |
21 } | 22 } |
22 | 23 |
23 impl OneWireSensor { | 24 impl OneWireSensor { |
24 fn new() -> OneWireSensor { | 25 fn new(config: &Config) -> OneWireSensor { |
25 OneWireSensor { | 26 OneWireSensor { |
26 master_dir: String::new(), // XXX | 27 config: config.clone(), |
27 | |
28 } | 28 } |
29 // todo | |
30 } | 29 } |
31 | 30 |
32 fn step(&mut self) -> Readings { | 31 fn step(&mut self) -> Readings { |
33 let mut r = Readings::new(); | 32 let mut r = Readings::new(); |
34 r.add("ambient", Some(31.2)); | 33 r.add("ambient", Some(31.2)); |
35 r.add("wort_todo", Some(8.0)); | 34 r.add("wort_todo", Some(8.0)); |
36 debug!("sensor step {:?}", r); | 35 debug!("sensor step {:?}", r); |
37 r | 36 r |
38 } | 37 } |
39 | 38 |
40 fn sensor_names(self) -> Vec<String> { | 39 fn sensor_names(&self) -> Vec<String> { |
41 let mut names = vec![]; | 40 let mut names = vec![]; |
42 names | 41 names |
43 } | 42 } |
44 } | 43 } |
45 | 44 |
46 impl Sensor for OneWireSensor { | 45 impl Sensor for OneWireSensor { |
47 fn stream(handle: &Handle) | 46 fn stream(handle: &Handle, config: &Config) |
48 -> Box<Stream<Item=Readings, Error=io::Error>> { | 47 -> Box<Stream<Item=Readings, Error=io::Error>> { |
49 let mut s = OneWireSensor::new(); | 48 let mut s = OneWireSensor::new(config); |
50 | 49 |
51 let dur = Duration::from_millis(600); | 50 let dur = Duration::new(s.config.SENSOR_SLEEP,0); |
52 Interval::new(dur, handle).unwrap().map(move |()| { | 51 Interval::new(dur, handle).unwrap().map(move |()| { |
53 s.step() | 52 s.step() |
54 }).boxed() | 53 }).boxed() |
55 } | 54 } |
56 } | 55 } |
57 | 56 |
58 pub struct TestSensor { | 57 pub struct TestSensor { |
58 config: Config, | |
59 } | 59 } |
60 | 60 |
61 impl TestSensor { | 61 impl TestSensor { |
62 pub fn new(config: &Config) -> Self { | |
63 TestSensor { | |
64 config: config.clone(), | |
65 } | |
66 } | |
67 | |
62 fn step(&mut self) -> Readings { | 68 fn step(&mut self) -> Readings { |
63 let mut r = Readings::new(); | 69 let mut r = Readings::new(); |
64 r.add("ambient", Some(31.2)); | 70 r.add("ambient", Some(31.2)); |
65 r.add("wort", Some(Self::try_read("test_wort.txt").unwrap_or_else(|_| 18.0))); | 71 r.add("wort", Some(Self::try_read("test_wort.txt").unwrap_or_else(|_| 18.0))); |
66 r.add("fridge", Some(Self::try_read("test_fridge.txt").unwrap_or_else(|_| 20.0))); | 72 r.add("fridge", Some(Self::try_read("test_fridge.txt").unwrap_or_else(|_| 20.0))); |
82 }) | 88 }) |
83 } | 89 } |
84 } | 90 } |
85 | 91 |
86 impl Sensor for TestSensor { | 92 impl Sensor for TestSensor { |
87 fn stream(handle: &Handle) | 93 fn stream(handle: &Handle, config: &Config) |
88 -> Box<Stream<Item=Readings, Error=io::Error>> { | 94 -> Box<Stream<Item=Readings, Error=io::Error>> { |
89 let mut s = TestSensor {}; | 95 let mut s = TestSensor::new(config); |
90 | 96 |
91 let dur = Duration::new(1,0); | 97 let dur = Duration::new(s.config.SENSOR_SLEEP,0); |
92 Interval::new(dur, handle).unwrap().map(move |()| { | 98 Interval::new(dur, handle).unwrap().map(move |()| { |
93 s.step() | 99 s.step() |
94 }).boxed() | 100 }).boxed() |
95 } | 101 } |
96 } | 102 } |