annotate rust/src/sensor.rs @ 639:89818a14648b rust tip

- switch to using anyhow for errors, surf for http runs but surf has problems
author Matt Johnston <matt@ucc.asn.au>
date Thu, 28 Nov 2019 23:57:00 +0800
parents a9f353f488d0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
1 use riker::actors::*;
631
c57821a60e51 rust work in progress ?
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
2
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
3 use std::time::Duration;
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
4 use std::io;
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
5 use std::fs::File;
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
6 use std::io::{Read,BufReader,BufRead};
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
7 use std::path::PathBuf;
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
8
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
9 use std::str::FromStr;
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
11 use super::types::*;
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
12 use super::config::Config;
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
13
631
c57821a60e51 rust work in progress ?
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
14 pub struct OneWireSensor {
c57821a60e51 rust work in progress ?
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
15 config: Config,
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
16 notify: ChannelRef<Readings>,
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
17 }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
18
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
19 // #[derive(Clone)]
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
20 pub struct TestSensor {
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
21 config: Config,
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
22 notify: ChannelRef<Readings>,
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
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
25 #[derive(Debug,Clone)]
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
26 pub struct SendReading;
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
27
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
28 impl Actor for OneWireSensor {
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
29 type Msg = SendReading;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
30
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
31 fn recv(&mut self,
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
32 _ctx: &Context<Self::Msg>,
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
33 _msg: Self::Msg,
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
34 _sender: Sender) {
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
35 self.notify.tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None);
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
36 }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
37
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
38 fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
39 let dur = Duration::new(self.config.sensor_sleep,0);
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
40 ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading);
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
41 }
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 }
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
44 impl OneWireSensor {
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
45 pub fn new_actor((config, notify): (Config, ChannelRef<Readings>)) -> Self {
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
46 Self::new(config, notify)
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
47 }
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
48
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
49 pub fn new(config: Config, notify: ChannelRef<Readings>) -> Self {
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
50 OneWireSensor {
605
8dd63473b6d8 config doesn't need Arc
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
51 config: config.clone(),
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
52 notify: notify,
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
53 }
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
54 }
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
55
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
56 fn get_readings(&self) -> Readings {
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
57 let mut r = Readings::new();
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
58
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
59 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
60 for n in &names {
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
61 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
62 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
63 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
64 }
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
65 }
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
66 }
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
67
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
68 debug!("sensor step {:?}", r);
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
69 r
590
dccd8504aa38 it runs
Matt Johnston <matt@ucc.asn.au>
parents: 588
diff changeset
70 }
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
71
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
72 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
73 lazy_static! {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
74 // multiline
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
75 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
76 }
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
77 let mut path = PathBuf::from(&self.config.sensor_base_dir);
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
78 path.push(n);
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
79 path.push("w1_slave");
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
80 let mut s = String::new();
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
81 File::open(path)?.read_to_string(&mut s)?;
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
82 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
83 TemplogError::new("Bad sensor contents match")
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
84 })?;
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
85 let v = caps.get(1).ok_or_else(|| {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
86 TemplogError::new("Bad field contents match")
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
87 })?.as_str();
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
88
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
89 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
90 }
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
91
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
92 fn sensor_names(&self) -> Result<Vec<String>, TemplogError> {
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 605
diff changeset
93 // TODO: needs to handle multiple busses.
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
94 let mut path = PathBuf::from(&self.config.sensor_base_dir);
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
95 path.push("w1_master_slaves");
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
96
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
97 let f = BufReader::new(File::open(path)?);
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
98 let s = f.lines().collect::<Result<Vec<String>, io::Error>>()?;
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
99 Ok(s)
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
100 }
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
101 }
588
038734052b20 fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents: 587
diff changeset
102
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
103 impl Actor for TestSensor {
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
104 type Msg = SendReading;
587
646f03870762 trying rust
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
106 fn recv(&mut self,
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
107 _ctx: &Context<Self::Msg>,
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
108 _msg: Self::Msg,
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
109 _sender: Sender) {
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
110 self.notify.tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None);
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
111 }
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
112
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
113 fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
114 info!("pre_start testsensor readings");
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
115 let dur = Duration::new(self.config.sensor_sleep,0);
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
116 ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading);
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
117 }
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
118 }
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 impl TestSensor {
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
121 pub fn new_actor((config, notify): (Config, ChannelRef<Readings>)) -> Self {
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
122 Self::new(config, notify)
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
123 }
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
124
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
125 pub fn new(config: Config, notify: ChannelRef<Readings>) -> Self {
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
126 TestSensor {
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
127 config: config.clone(),
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
128 notify: notify,
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
129 }
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
130 }
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
131
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
132 fn get_readings(&self) -> Readings {
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
133 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
134 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
135 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
136 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
137 r
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
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
140 fn try_read(filename: &str) -> Result<f32, TemplogError> {
601
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
141 let mut s = String::new();
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
142 File::open(filename)?.read_to_string(&mut s)?;
8c21df3711e2 rigid_config
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
143 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
144 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
145 }