annotate rust/src/sensor.rs @ 620:8fda564cc46f rust

fridge work
author Matt Johnston <matt@ucc.asn.au>
date Thu, 23 Mar 2017 00:20:22 +0800
parents f3e39e2107fd
children c57821a60e51
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;
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
4 extern crate regex;
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
5
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
6 use std::time::Duration;
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
7 use std::io;
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
8 use std::fs::File;
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
9 use std::io::{Read,BufReader,BufRead};
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
10 use std::path::PathBuf;
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
11 use std::error::Error;
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
12 use std::sync::Arc;
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
13 use std::str::FromStr;
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
15 use tokio_core::reactor::Interval;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
16 use tokio_core::reactor::Handle;
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
17 use futures::Stream;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 591
diff changeset
18 use types::*;
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
19 use ::Config;
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
20
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
21 pub trait Sensor {
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
22 fn stream(&self, handle: &Handle) -> Box<Stream<Item=Readings, Error=TemplogError>>;
594
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
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
25 #[derive(Clone)]
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
26 pub struct OneWireSensor {
605
8dd63473b6d8 config doesn't need Arc
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
27 config: Config,
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
30 impl OneWireSensor {
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
31 pub fn new(config: &Config) -> Self {
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
32 OneWireSensor {
605
8dd63473b6d8 config doesn't need Arc
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
33 config: config.clone(),
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
34 }
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
35 }
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
36
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
37 fn step(&self) -> Readings {
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
38 let mut r = Readings::new();
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
39
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
40 if let Ok(names) = self.sensor_names() {
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
41 for n in &names {
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
42 match self.read_sensor(n) {
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
43 Ok(s) => r.add(n, s),
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
44 Err(e) => debug!("Error reading sensors {}: {}", n, e)
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
45 }
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
46 }
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
47 }
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
48
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
49 debug!("sensor step {:?}", r);
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
50 r
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
51 }
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
52
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
53 fn read_sensor(&self, n: &str) -> Result<f32, TemplogError> {
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
54 lazy_static! {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
55 // multiline
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
56 static ref THERM_RE: regex::Regex = regex::Regex::new("(?m).* YES\n.*t=(.*)\n").unwrap();
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
57 }
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
58 let mut path = PathBuf::from(&self.config.SENSOR_BASE_DIR);
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
59 path.push(n);
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
60 path.push("w1_slave");
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
61 let mut s = String::new();
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
62 File::open(path)?.read_to_string(&mut s)?;
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
63 let caps = THERM_RE.captures(&s).ok_or_else(|| {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
64 TemplogError::new("Bad sensor contents match")
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
65 })?;
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
66 let v = caps.get(1).ok_or_else(|| {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
67 TemplogError::new("Bad field contents match")
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
68 })?.as_str();
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
69
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
70 Ok(f32::from_str(v)?)
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
71 }
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
72
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
73 fn sensor_names(&self) -> Result<Vec<String>, TemplogError> {
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 605
diff changeset
74 // TODO: needs to handle multiple busses.
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
75 let mut path = PathBuf::from(&self.config.SENSOR_BASE_DIR);
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
76 path.push("w1_master_slaves");
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
77
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
78 let f = BufReader::new(File::open(path)?);
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
79 let s = f.lines().collect::<Result<Vec<String>, io::Error>>()?;
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
80 Ok(s)
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
81 }
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
82 }
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
83
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
84 impl Sensor for OneWireSensor {
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
85
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
86 fn stream(&self, handle: &Handle) -> Box<Stream<Item=Readings, Error=TemplogError>> {
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
87 let pool = futures_cpupool::CpuPool::new(4); // TODO: how many?
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
88
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
89 let dur = Duration::new(self.config.SENSOR_SLEEP,0);
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
90 let s = Arc::new(self.clone());
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
91 let i = Interval::new(dur, handle).unwrap()
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
92 .map_err(|e| TemplogError::new_io("Interval failed. Should not happen", e))
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
93 .and_then(move |()| {
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
94 let a = s.clone();
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
95 pool.spawn_fn(move || Ok(a.step()))
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
96 });
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
97 consume_errors(i)
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
101 #[derive(Clone)]
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
102 pub struct TestSensor {
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
103 config: Config,
594
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
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
106 impl TestSensor {
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
107 pub fn new(config: &Config) -> Self {
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
108 TestSensor {
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
109 config: config.clone(),
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
110 }
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
111 }
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
112
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
113 fn test_step() -> Readings {
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
114 let mut r = Readings::new();
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
115 r.add("ambient", 31.2);
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
116 r.add("wort", Self::try_read("test_wort.txt").unwrap_or_else(|_| 18.0));
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
117 r.add("fridge", Self::try_read("test_fridge.txt").unwrap_or_else(|_| 20.0));
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
118 r
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
119 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
120
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
121 fn try_read(filename: &str) -> Result<f32, TemplogError> {
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
122 let mut s = String::new();
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
123 File::open(filename)?.read_to_string(&mut s)?;
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
124 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
125 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
126 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
127
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
128 impl Sensor for TestSensor {
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
129 fn stream(&self, handle: &Handle) -> Box<Stream<Item=Readings, Error=TemplogError>> {
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
130
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
131 let dur = Duration::new(self.config.SENSOR_SLEEP,0);
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
132 let i = Interval::new(dur, handle).unwrap()
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
133 .map_err(|e| TemplogError::new_io("Interval failed. Should not happen", e))
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
134 .and_then(move |()| {
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
135 Ok(Self::test_step())
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
136 });
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
137 consume_errors(i)
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
138 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
139 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
140