annotate rust/src/sensor.rs @ 600:9c76f3cf01ea rust

slightly less ugly
author Matt Johnston <matt@ucc.asn.au>
date Tue, 07 Feb 2017 22:57:29 +0800
parents ca8102feaca6
children 8c21df3711e2
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;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
3
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
4 use std::time::Duration;
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
5 use std::io;
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
6 use std::fs::File;
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
7 use std::io::Read;
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
9 use tokio_core::reactor::Interval;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
10 use tokio_core::reactor::Handle;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
11 use futures::Stream;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 591
diff changeset
12 use types::*;
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
13 use config::Config;
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
15 pub trait Sensor {
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
16 fn stream(handle: &Handle, config: &Config)
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
17 -> 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
18 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
19
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
20 pub struct OneWireSensor {
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
21 config: Config,
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
24 impl OneWireSensor {
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
25 fn new(config: &Config) -> OneWireSensor {
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
26 OneWireSensor {
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
27 config: config.clone(),
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
28 }
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
29 }
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
30
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
31 fn step(&mut self) -> Readings {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
32 let mut r = Readings::new();
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
33 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
34 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
35 debug!("sensor step {:?}", r);
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
36 r
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
37 }
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
38
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
39 fn sensor_names(&self) -> Vec<String> {
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
40 let mut names = vec![];
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
41 names
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
42 }
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
43 }
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
44
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
45 impl Sensor for OneWireSensor {
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
46 fn stream(handle: &Handle, config: &Config)
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
47 -> Box<Stream<Item=Readings, Error=io::Error>> {
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
48 let mut s = OneWireSensor::new(config);
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
49
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
50 let dur = Duration::new(s.config.SENSOR_SLEEP,0);
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
51 Interval::new(dur, handle).unwrap().map(move |()| {
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
52 s.step()
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
53 }).boxed()
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
57 pub struct TestSensor {
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
58 config: Config,
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
59 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
60
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
61 impl TestSensor {
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
62 pub fn new(config: &Config) -> Self {
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
63 TestSensor {
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
64 config: config.clone(),
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
65 }
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
66 }
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
67
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
68 fn step(&mut self) -> Readings {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
69 let mut r = Readings::new();
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
70 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
71 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
72 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
73 r
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
74 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
75
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
76 fn try_read(filename: &str) -> Result<f32, String> {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
77 File::open(filename)
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
78 .map_err(|e| e.to_string())
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
79 .and_then(|mut f| {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
80 let mut s = String::new();
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
81 f.read_to_string(&mut s)
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
82 .map_err(|e| e.to_string())
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
83 .map(|_| s)
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
84 })
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
85 .and_then(|s| {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
86 s.trim().parse::<f32>()
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
87 .map_err(|e| e.to_string())
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
88 })
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
89 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
90 }
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 impl Sensor for TestSensor {
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
93 fn stream(handle: &Handle, config: &Config)
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
94 -> Box<Stream<Item=Readings, Error=io::Error>> {
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
95 let mut s = TestSensor::new(config);
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
96
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
97 let dur = Duration::new(s.config.SENSOR_SLEEP,0);
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
98 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
99 s.step()
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
100 }).boxed()
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
101 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
102 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
103