diff 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
line wrap: on
line diff
--- a/rust/src/sensor.rs	Sat Nov 09 11:35:43 2019 +0800
+++ b/rust/src/sensor.rs	Sat Nov 09 11:35:59 2019 +0800
@@ -13,13 +13,13 @@
 
 pub struct OneWireSensor {
     config: Config,
-    chan: Option<ChannelRef<Readings>>,
+    notify: ChannelRef<Readings>,
 }
 
 // #[derive(Clone)]
 pub struct TestSensor {
     config: Config,
-    chan: Option<ChannelRef<Readings>>,
+    notify: ChannelRef<Readings>,
 }
 
 #[derive(Debug,Clone)]
@@ -32,21 +32,24 @@
             _ctx: &Context<Self::Msg>,
             _msg: Self::Msg,
             _sender: Sender) {
-        self.chan.as_ref().unwrap().tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None);
+        self.notify.tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None);
     }
 
     fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
-        self.chan = Some(channel("readings", &ctx.system).unwrap());
         let dur = Duration::new(self.config.sensor_sleep,0);
         ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading);
     }
 }
 
 impl OneWireSensor {
-    pub fn new(config: Config) -> Self {
+    pub fn new_actor((config, notify): (Config, ChannelRef<Readings>)) -> Self {
+        Self::new(config, notify)
+    }
+
+    pub fn new(config: Config, notify: ChannelRef<Readings>) -> Self {
         OneWireSensor {
             config: config.clone(),
-            chan: None,
+            notify: notify,
         }
     }
 
@@ -104,21 +107,25 @@
             _ctx: &Context<Self::Msg>,
             _msg: Self::Msg,
             _sender: Sender) {
-        self.chan.as_ref().unwrap().tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None);
+        self.notify.tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None);
     }
 
     fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
-        self.chan = Some(channel("readings", &ctx.system).unwrap());
+        info!("pre_start testsensor readings");
         let dur = Duration::new(self.config.sensor_sleep,0);
         ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading);
     }
 }
 
 impl TestSensor {
-    pub fn new(config: Config) -> Self {
+    pub fn new_actor((config, notify): (Config, ChannelRef<Readings>)) -> Self {
+        Self::new(config, notify)
+    }
+
+    pub fn new(config: Config, notify: ChannelRef<Readings>) -> Self {
         TestSensor {
             config: config.clone(),
-            chan: None,
+            notify: notify,
         }
     }