Mercurial > templog
comparison rust/src/sensor.rs @ 634:a5721c02d3ee rust
build succeeds
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 22 Sep 2019 20:35:40 +0800 |
parents | 490e9e15b98c |
children | 4424a8b30f9c |
comparison
equal
deleted
inserted
replaced
633:490e9e15b98c | 634:a5721c02d3ee |
---|---|
11 use super::types::*; | 11 use super::types::*; |
12 use super::config::Config; | 12 use super::config::Config; |
13 | 13 |
14 pub struct OneWireSensor { | 14 pub struct OneWireSensor { |
15 config: Config, | 15 config: Config, |
16 chan: ChannelRef<Readings>, | 16 chan: Option<ChannelRef<Readings>>, |
17 } | 17 } |
18 | 18 |
19 struct SendReading; | 19 // #[derive(Clone)] |
20 | 20 pub struct TestSensor { |
21 trait Sensor { | 21 config: Config, |
22 chan: Option<ChannelRef<Readings>>, | |
22 } | 23 } |
23 | 24 |
24 impl Actor for dyn Sensor { | 25 #[derive(Debug,Clone)] |
26 pub struct SendReading; | |
27 | |
28 impl Actor for OneWireSensor { | |
25 type Msg = SendReading; | 29 type Msg = SendReading; |
26 | 30 |
27 fn recv(&mut self, | 31 fn recv(&mut self, |
28 ctx: &Context<Self::Msg>, | 32 _ctx: &Context<Self::Msg>, |
29 msg: Self::Msg, | 33 _msg: Self::Msg, |
30 sender: Sender) { | 34 _sender: Sender) { |
31 self.chan.tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None); | 35 self.chan.as_ref().unwrap().tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None); |
32 } | 36 } |
33 | 37 |
34 fn post_start(&mut self, ctx: &Context<Self::Msg>) { | 38 fn pre_start(&mut self, ctx: &Context<Self::Msg>) { |
35 self.chan = channel("readings", &ctx.system).unwrap(); | 39 self.chan = Some(channel("readings", &ctx.system).unwrap()); |
36 let dur = Duration::new(self.config.SENSOR_SLEEP,0); | 40 let dur = Duration::new(self.config.SENSOR_SLEEP,0); |
37 ctx.schedule(Duration::from_millis(0), dur, self, None, SendReading); | 41 ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading); |
38 } | 42 } |
39 } | 43 } |
40 | 44 |
41 impl OneWireSensor { | 45 impl OneWireSensor { |
42 pub fn new(config: &Config) -> Self { | 46 pub fn new(config: Config) -> Self { |
43 OneWireSensor { | 47 OneWireSensor { |
44 config: config.clone(), | 48 config: config.clone(), |
49 chan: None, | |
45 } | 50 } |
46 } | 51 } |
47 | 52 |
48 fn get_readings(&self) -> Readings { | 53 fn get_readings(&self) -> Readings { |
49 let mut r = Readings::new(); | 54 let mut r = Readings::new(); |
90 let s = f.lines().collect::<Result<Vec<String>, io::Error>>()?; | 95 let s = f.lines().collect::<Result<Vec<String>, io::Error>>()?; |
91 Ok(s) | 96 Ok(s) |
92 } | 97 } |
93 } | 98 } |
94 | 99 |
95 impl Sensor for OneWireSensor { | 100 impl Actor for TestSensor { |
96 } | 101 type Msg = SendReading; |
97 | 102 |
98 #[derive(Clone)] | 103 fn recv(&mut self, |
99 pub struct TestSensor { | 104 _ctx: &Context<Self::Msg>, |
100 config: Config, | 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()); | |
112 let dur = Duration::new(self.config.SENSOR_SLEEP,0); | |
113 ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading); | |
114 } | |
101 } | 115 } |
102 | 116 |
103 impl TestSensor { | 117 impl TestSensor { |
104 pub fn new(config: &Config) -> Self { | 118 pub fn new(config: Config) -> Self { |
105 TestSensor { | 119 TestSensor { |
106 config: config.clone(), | 120 config: config.clone(), |
121 chan: None, | |
107 } | 122 } |
108 } | 123 } |
109 | 124 |
110 fn test_step() -> Readings { | 125 fn get_readings(&self) -> Readings { |
111 let mut r = Readings::new(); | 126 let mut r = Readings::new(); |
112 r.add("ambient", 31.2); | 127 r.add("ambient", 31.2); |
113 r.add("wort", Self::try_read("test_wort.txt").unwrap_or_else(|_| 18.0)); | 128 r.add("wort", Self::try_read("test_wort.txt").unwrap_or_else(|_| 18.0)); |
114 r.add("fridge", Self::try_read("test_fridge.txt").unwrap_or_else(|_| 20.0)); | 129 r.add("fridge", Self::try_read("test_fridge.txt").unwrap_or_else(|_| 20.0)); |
115 r | 130 r |
119 let mut s = String::new(); | 134 let mut s = String::new(); |
120 File::open(filename)?.read_to_string(&mut s)?; | 135 File::open(filename)?.read_to_string(&mut s)?; |
121 Ok(s.trim().parse::<f32>()?) | 136 Ok(s.trim().parse::<f32>()?) |
122 } | 137 } |
123 } | 138 } |
124 | |
125 impl Sensor for TestSensor { | |
126 } | |
127 |