annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
8 use std::io::{Read,BufReader,BufRead};
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
9 use std::path::PathBuf;
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
10 use std::error::Error;
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
16
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
17 use ::rigid_config;
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
28 fn new() -> OneWireSensor {
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
29 OneWireSensor {
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
35
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
41 }
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
42
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
43 fn sensor_names(&self) -> Result<Vec<String>, Box<Error>> {
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
44 let mut path = PathBuf::from(&rigid_config.SENSOR_BASE_DIR);
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
45 path.push("w1_master_slaves");
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
46
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
47 let f = BufReader::new(File::open(path)?);
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
48 let s = f.lines().collect::<Result<Vec<String>, io::Error>>()?;
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
49 Ok(s)
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
53 // does this need to be static?
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
54 lazy_static! {
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
55 static ref thread_pool : futures_cpupool::CpuPool = futures_cpupool::CpuPool::new(3); // TODO: how many?
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
56 }
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
61 let mut s = OneWireSensor::new();
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
62
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
63 let dur = Duration::new(rigid_config.SENSOR_SLEEP,0);
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
64 Interval::new(dur, handle).unwrap()
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
65 .and_then(|()| {
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
66 thread_pool.spawn_fn(|| Ok(Self::step()))
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
67 }).boxed()
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
75 pub fn new() -> Self {
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
87 fn try_read(filename: &str) -> Result<f32, Box<Error>> {
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
88 let mut s = String::new();
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
89 File::open(filename)?.read_to_string(&mut s)?;
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
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