Mercurial > templog
annotate rust/src/sensor.rs @ 635:4424a8b30f9c rust
config crate wants everything to be lower case
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 22 Sep 2019 22:06:46 +0800 |
parents | a5721c02d3ee |
children | a9f353f488d0 |
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 | 2 |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
3 use std::time::Duration; |
590 | 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 | 6 use std::io::{Read,BufReader,BufRead}; |
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 | 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 | 13 |
631 | 14 pub struct OneWireSensor { |
15 config: Config, | |
634 | 16 chan: Option<ChannelRef<Readings>>, |
633 | 17 } |
18 | |
634 | 19 // #[derive(Clone)] |
20 pub struct TestSensor { | |
21 config: Config, | |
22 chan: Option<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 | 25 #[derive(Debug,Clone)] |
26 pub struct SendReading; | |
27 | |
28 impl Actor for OneWireSensor { | |
633 | 29 type Msg = SendReading; |
30 | |
31 fn recv(&mut self, | |
634 | 32 _ctx: &Context<Self::Msg>, |
33 _msg: Self::Msg, | |
34 _sender: Sender) { | |
35 self.chan.as_ref().unwrap().tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None); | |
633 | 36 } |
37 | |
634 | 38 fn pre_start(&mut self, ctx: &Context<Self::Msg>) { |
39 self.chan = Some(channel("readings", &ctx.system).unwrap()); | |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
40 let dur = Duration::new(self.config.sensor_sleep,0); |
634 | 41 ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading); |
633 | 42 } |
587 | 43 } |
44 | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
45 impl OneWireSensor { |
634 | 46 pub fn new(config: Config) -> Self { |
595 | 47 OneWireSensor { |
605 | 48 config: config.clone(), |
634 | 49 chan: None, |
595 | 50 } |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
51 } |
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
52 |
633 | 53 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
|
54 let mut r = Readings::new(); |
601 | 55 |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
56 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
|
57 for n in &names { |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
58 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
|
59 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
|
60 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
|
61 } |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
62 } |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
63 } |
601 | 64 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
65 debug!("sensor step {:?}", r); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
66 r |
590 | 67 } |
595 | 68 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
69 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
|
70 lazy_static! { |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
71 // multiline |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
72 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
|
73 } |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
74 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
|
75 path.push(n); |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
76 path.push("w1_slave"); |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
77 let mut s = String::new(); |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
78 File::open(path)?.read_to_string(&mut s)?; |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
79 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
|
80 TemplogError::new("Bad sensor contents match") |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
81 })?; |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
82 let v = caps.get(1).ok_or_else(|| { |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
83 TemplogError::new("Bad field contents match") |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
84 })?.as_str(); |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
85 |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
86 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
|
87 } |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
88 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
89 fn sensor_names(&self) -> Result<Vec<String>, TemplogError> { |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
605
diff
changeset
|
90 // 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
|
91 let mut path = PathBuf::from(&self.config.sensor_base_dir); |
601 | 92 path.push("w1_master_slaves"); |
93 | |
94 let f = BufReader::new(File::open(path)?); | |
95 let s = f.lines().collect::<Result<Vec<String>, io::Error>>()?; | |
96 Ok(s) | |
595 | 97 } |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
98 } |
588
038734052b20
fiddling with futures-rs instead
Matt Johnston <matt@ucc.asn.au>
parents:
587
diff
changeset
|
99 |
634 | 100 impl Actor for TestSensor { |
101 type Msg = SendReading; | |
587 | 102 |
634 | 103 fn recv(&mut self, |
104 _ctx: &Context<Self::Msg>, | |
105 _msg: Self::Msg, | |
106 _sender: Sender) { | |
107 self.chan.as_ref().unwrap().tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None); | |
108 } | |
109 | |
110 fn pre_start(&mut self, ctx: &Context<Self::Msg>) { | |
111 self.chan = Some(channel("readings", &ctx.system).unwrap()); | |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
112 let dur = Duration::new(self.config.sensor_sleep,0); |
634 | 113 ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading); |
114 } | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
115 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
116 |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
117 impl TestSensor { |
634 | 118 pub fn new(config: Config) -> Self { |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
119 TestSensor { |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
120 config: config.clone(), |
634 | 121 chan: None, |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
601
diff
changeset
|
122 } |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
123 } |
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
124 |
634 | 125 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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 r |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
131 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
132 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
133 fn try_read(filename: &str) -> Result<f32, TemplogError> { |
601 | 134 let mut s = String::new(); |
135 File::open(filename)?.read_to_string(&mut s)?; | |
136 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
|
137 } |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
138 } |