comparison rust/src/sensor.rs @ 638:a9f353f488d0 rust

fix channels
author Matt Johnston <matt@ucc.asn.au>
date Sat, 09 Nov 2019 11:35:59 +0800
parents 4424a8b30f9c
children
comparison
equal deleted inserted replaced
637:1e147b3c2c55 638:a9f353f488d0
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: Option<ChannelRef<Readings>>, 16 notify: ChannelRef<Readings>,
17 } 17 }
18 18
19 // #[derive(Clone)] 19 // #[derive(Clone)]
20 pub struct TestSensor { 20 pub struct TestSensor {
21 config: Config, 21 config: Config,
22 chan: Option<ChannelRef<Readings>>, 22 notify: ChannelRef<Readings>,
23 } 23 }
24 24
25 #[derive(Debug,Clone)] 25 #[derive(Debug,Clone)]
26 pub struct SendReading; 26 pub struct SendReading;
27 27
30 30
31 fn recv(&mut self, 31 fn recv(&mut self,
32 _ctx: &Context<Self::Msg>, 32 _ctx: &Context<Self::Msg>,
33 _msg: Self::Msg, 33 _msg: Self::Msg,
34 _sender: Sender) { 34 _sender: Sender) {
35 self.chan.as_ref().unwrap().tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None); 35 self.notify.tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None);
36 } 36 }
37 37
38 fn pre_start(&mut self, ctx: &Context<Self::Msg>) { 38 fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
39 self.chan = Some(channel("readings", &ctx.system).unwrap());
40 let dur = Duration::new(self.config.sensor_sleep,0); 39 let dur = Duration::new(self.config.sensor_sleep,0);
41 ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading); 40 ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading);
42 } 41 }
43 } 42 }
44 43
45 impl OneWireSensor { 44 impl OneWireSensor {
46 pub fn new(config: Config) -> Self { 45 pub fn new_actor((config, notify): (Config, ChannelRef<Readings>)) -> Self {
46 Self::new(config, notify)
47 }
48
49 pub fn new(config: Config, notify: ChannelRef<Readings>) -> Self {
47 OneWireSensor { 50 OneWireSensor {
48 config: config.clone(), 51 config: config.clone(),
49 chan: None, 52 notify: notify,
50 } 53 }
51 } 54 }
52 55
53 fn get_readings(&self) -> Readings { 56 fn get_readings(&self) -> Readings {
54 let mut r = Readings::new(); 57 let mut r = Readings::new();
102 105
103 fn recv(&mut self, 106 fn recv(&mut self,
104 _ctx: &Context<Self::Msg>, 107 _ctx: &Context<Self::Msg>,
105 _msg: Self::Msg, 108 _msg: Self::Msg,
106 _sender: Sender) { 109 _sender: Sender) {
107 self.chan.as_ref().unwrap().tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None); 110 self.notify.tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None);
108 } 111 }
109 112
110 fn pre_start(&mut self, ctx: &Context<Self::Msg>) { 113 fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
111 self.chan = Some(channel("readings", &ctx.system).unwrap()); 114 info!("pre_start testsensor readings");
112 let dur = Duration::new(self.config.sensor_sleep,0); 115 let dur = Duration::new(self.config.sensor_sleep,0);
113 ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading); 116 ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading);
114 } 117 }
115 } 118 }
116 119
117 impl TestSensor { 120 impl TestSensor {
118 pub fn new(config: Config) -> Self { 121 pub fn new_actor((config, notify): (Config, ChannelRef<Readings>)) -> Self {
122 Self::new(config, notify)
123 }
124
125 pub fn new(config: Config, notify: ChannelRef<Readings>) -> Self {
119 TestSensor { 126 TestSensor {
120 config: config.clone(), 127 config: config.clone(),
121 chan: None, 128 notify: notify,
122 } 129 }
123 } 130 }
124 131
125 fn get_readings(&self) -> Readings { 132 fn get_readings(&self) -> Readings {
126 let mut r = Readings::new(); 133 let mut r = Readings::new();