Mercurial > templog
comparison rust/src/sensor.rs @ 594:aff50ee77252 rust
rust working better now with streams and sinks.
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 04 Jan 2017 17:18:44 +0800 |
parents | 03b48ec0bb03 |
children | e87655ed8429 |
comparison
equal
deleted
inserted
replaced
593:bf138339d20a | 594:aff50ee77252 |
---|---|
1 extern crate tokio_core; | 1 extern crate tokio_core; |
2 extern crate futures; | 2 extern crate futures; |
3 | 3 |
4 use std::time::Duration; | 4 use std::time::Duration; |
5 use std::io; | 5 use std::io; |
6 use std::fs::File; | |
7 use std::io::Read; | |
6 | 8 |
7 use tokio_core::reactor::Interval; | 9 use tokio_core::reactor::Interval; |
8 use tokio_core::reactor::Handle; | 10 use tokio_core::reactor::Handle; |
9 use futures::Stream; | 11 use futures::Stream; |
10 use types::*; | 12 use types::*; |
11 | 13 |
12 pub struct Sensor { | 14 pub trait Sensor { |
13 current: f32, | 15 fn stream(handle: &Handle) |
14 suf: String, | 16 -> Box<Stream<Item=Readings, Error=io::Error>>; |
15 } | 17 } |
16 | 18 |
17 impl Sensor { | 19 pub struct OneWireSensor { |
18 fn step(&mut self) -> Vec<Reading> { | 20 } |
19 let mut r = Vec::new(); | 21 |
20 self.current = self.current + 0.1; | 22 impl OneWireSensor { |
21 r.push(Reading::new("aaa".to_string() + &self.suf, self.current)); | 23 fn new() -> OneWireSensor { |
22 r.push(Reading::new("b".to_string() + &self.suf, self.current/3.0)); | 24 OneWireSensor {} |
25 // todo | |
26 } | |
27 | |
28 fn step(&mut self) -> Readings { | |
29 let mut r = Readings::new(); | |
30 r.add("ambient", Some(31.2)); | |
31 r.add("wort_todo", Some(8.0)); | |
32 debug!("sensor step {:?}", r); | |
23 r | 33 r |
24 } | 34 } |
35 } | |
25 | 36 |
26 fn new(suffix: String) -> Self { | 37 impl Sensor for OneWireSensor { |
27 Sensor { current: 22.0, suf: suffix } | 38 fn stream(handle: &Handle) |
28 } | 39 -> Box<Stream<Item=Readings, Error=io::Error>> { |
40 let mut s = OneWireSensor::new(); | |
29 | 41 |
30 pub fn run(handle: &Handle, rate: u64, suffix: String) -> Box<Stream<Item=Vec<Reading>, Error=io::Error>> { | 42 let dur = Duration::from_millis(600); |
31 let mut s = Sensor::new(suffix); | |
32 | |
33 let dur = Duration::from_millis(rate); | |
34 Interval::new(dur, handle).unwrap().map(move |()| { | 43 Interval::new(dur, handle).unwrap().map(move |()| { |
35 s.step() | 44 s.step() |
36 }).boxed() | 45 }).boxed() |
37 } | 46 } |
38 } | 47 } |
39 | 48 |
49 pub struct TestSensor { | |
50 } | |
51 | |
52 impl TestSensor { | |
53 fn step(&mut self) -> Readings { | |
54 let mut r = Readings::new(); | |
55 r.add("ambient", Some(31.2)); | |
56 r.add("wort", Some(Self::try_read("test_wort.txt").unwrap_or_else(|_| 18.0))); | |
57 r.add("fridge", Some(Self::try_read("test_fridge.txt").unwrap_or_else(|_| 20.0))); | |
58 r | |
59 } | |
60 | |
61 fn try_read(filename: &str) -> Result<f32, String> { | |
62 File::open(filename) | |
63 .map_err(|e| e.to_string()) | |
64 .and_then(|mut f| { | |
65 let mut s = String::new(); | |
66 f.read_to_string(&mut s) | |
67 .map_err(|e| e.to_string()) | |
68 .map(|_| s) | |
69 }) | |
70 .and_then(|s| { | |
71 s.trim().parse::<f32>() | |
72 .map_err(|e| e.to_string()) | |
73 }) | |
74 } | |
75 } | |
76 | |
77 impl Sensor for TestSensor { | |
78 fn stream(handle: &Handle) | |
79 -> Box<Stream<Item=Readings, Error=io::Error>> { | |
80 let mut s = TestSensor {}; | |
81 | |
82 let dur = Duration::new(1,0); | |
83 Interval::new(dur, handle).unwrap().map(move |()| { | |
84 s.step() | |
85 }).boxed() | |
86 } | |
87 } | |
88 |