annotate rust/src/sensor.rs @ 595:e87655ed8429 rust

add config
author Matt Johnston <matt@ucc.asn.au>
date Thu, 05 Jan 2017 23:26:00 +0800
parents aff50ee77252
children ca8102feaca6
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::*;
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
14 pub trait Sensor {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
15 fn stream(handle: &Handle)
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
16 -> 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
17 }
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 pub struct OneWireSensor {
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
20 master_dir: String,
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
23 impl OneWireSensor {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
24 fn new() -> OneWireSensor {
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
25 OneWireSensor {
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
26 master_dir: String::new(), // XXX
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
27
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
28 }
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
29 // todo
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
30 }
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
31
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
32 fn step(&mut self) -> Readings {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
33 let mut r = Readings::new();
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
34 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
35 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
36 debug!("sensor step {:?}", r);
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
37 r
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
38 }
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
39
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
40 fn sensor_names(self) -> Vec<String> {
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
41 let mut names = vec![];
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
42 names
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
43 }
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
44 }
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
45
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
46 impl Sensor for OneWireSensor {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
47 fn stream(handle: &Handle)
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
48 -> 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
49 let mut s = OneWireSensor::new();
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
50
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
51 let dur = Duration::from_millis(600);
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
52 Interval::new(dur, handle).unwrap().map(move |()| {
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
53 s.step()
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
54 }).boxed()
587
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 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
58 pub struct TestSensor {
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 {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
62 fn step(&mut self) -> Readings {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
63 let mut r = Readings::new();
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
64 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
65 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
66 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
67 r
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
68 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
69
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
70 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
71 File::open(filename)
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
72 .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
73 .and_then(|mut f| {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
74 let mut s = String::new();
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
75 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
76 .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
77 .map(|_| s)
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
78 })
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
79 .and_then(|s| {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
80 s.trim().parse::<f32>()
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
81 .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
82 })
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
83 }
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
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
86 impl Sensor for TestSensor {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
87 fn stream(handle: &Handle)
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
88 -> 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
89 let mut s = TestSensor {};
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 let dur = Duration::new(1,0);
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
92 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
93 s.step()
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
94 }).boxed()
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
95 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
96 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
97