Mercurial > templog
annotate rust/src/sensor.rs @ 602:613f114feb4b rust
missing file
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 16 Feb 2017 23:17:51 +0800 |
parents | 8c21df3711e2 |
children | b45b8b4cf0f5 |
rev | line source |
---|---|
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
1 extern crate tokio_core; |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
2 extern crate futures; |
601 | 3 extern crate futures_cpupool; |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
4 |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
5 use std::time::Duration; |
590 | 6 use std::io; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
7 use std::fs::File; |
601 | 8 use std::io::{Read,BufReader,BufRead}; |
9 use std::path::PathBuf; | |
10 use std::error::Error; | |
587 | 11 |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
12 use tokio_core::reactor::Interval; |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
13 use tokio_core::reactor::Handle; |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
14 use futures::Stream; |
592
03b48ec0bb03
fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
591
diff
changeset
|
15 use types::*; |
601 | 16 |
17 use ::rigid_config; | |
587 | 18 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
19 pub trait Sensor { |
601 | 20 fn stream(handle: &Handle) |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
21 -> Box<Stream<Item=Readings, Error=io::Error>>; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
22 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
23 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
24 pub struct OneWireSensor { |
587 | 25 } |
26 | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
27 impl OneWireSensor { |
601 | 28 fn new() -> OneWireSensor { |
595 | 29 OneWireSensor { |
30 } | |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
31 } |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
32 |
601 | 33 fn step() -> Readings { |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
34 let mut r = Readings::new(); |
601 | 35 |
36 | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
37 r.add("ambient", Some(31.2)); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
38 r.add("wort_todo", Some(8.0)); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
39 debug!("sensor step {:?}", r); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
40 r |
590 | 41 } |
595 | 42 |
601 | 43 fn sensor_names(&self) -> Result<Vec<String>, Box<Error>> { |
44 let mut path = PathBuf::from(&rigid_config.SENSOR_BASE_DIR); | |
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) | |
595 | 50 } |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
51 } |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
52 |
601 | 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 | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
58 impl Sensor for OneWireSensor { |
601 | 59 fn stream(handle: &Handle) |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
60 -> Box<Stream<Item=Readings, Error=io::Error>> { |
601 | 61 let mut s = OneWireSensor::new(); |
590 | 62 |
601 | 63 let dur = Duration::new(rigid_config.SENSOR_SLEEP,0); |
64 Interval::new(dur, handle).unwrap() | |
65 .and_then(|()| { | |
66 thread_pool.spawn_fn(|| Ok(Self::step())) | |
67 }).boxed() | |
587 | 68 } |
69 } | |
70 | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
71 pub struct TestSensor { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
72 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
73 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
74 impl TestSensor { |
601 | 75 pub fn new() -> Self { |
76 TestSensor {} | |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
77 } |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
78 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
79 fn step(&mut self) -> Readings { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
80 let mut r = Readings::new(); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
81 r.add("ambient", Some(31.2)); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
82 r.add("wort", Some(Self::try_read("test_wort.txt").unwrap_or_else(|_| 18.0))); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
83 r.add("fridge", Some(Self::try_read("test_fridge.txt").unwrap_or_else(|_| 20.0))); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
84 r |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
85 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
86 |
601 | 87 fn try_read(filename: &str) -> Result<f32, Box<Error>> { |
88 let mut s = String::new(); | |
89 File::open(filename)?.read_to_string(&mut s)?; | |
90 Ok(s.trim().parse::<f32>()?) | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
91 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
92 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
93 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
94 impl Sensor for TestSensor { |
601 | 95 fn stream(handle: &Handle) |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
96 -> Box<Stream<Item=Readings, Error=io::Error>> { |
601 | 97 let mut s = TestSensor::new(); |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
98 |
601 | 99 let dur = Duration::new(rigid_config.SENSOR_SLEEP,0); |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
100 Interval::new(dur, handle).unwrap().map(move |()| { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
101 s.step() |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
102 }).boxed() |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
103 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
104 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
105 |