diff 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
line wrap: on
line diff
--- a/rust/src/sensor.rs	Wed Sep 04 23:24:13 2019 +0800
+++ b/rust/src/sensor.rs	Sun Sep 22 20:35:40 2019 +0800
@@ -13,35 +13,40 @@
 
 pub struct OneWireSensor {
     config: Config,
-    chan: ChannelRef<Readings>,
+    chan: Option<ChannelRef<Readings>>,
 }
 
-struct SendReading;
-
-trait Sensor {
+// #[derive(Clone)]
+pub struct TestSensor {
+    config: Config,
+    chan: Option<ChannelRef<Readings>>,
 }
 
-impl Actor for dyn Sensor {
+#[derive(Debug,Clone)]
+pub struct SendReading;
+
+impl Actor for OneWireSensor {
     type Msg = SendReading;
 
     fn recv(&mut self,
-            ctx: &Context<Self::Msg>,
-            msg: Self::Msg,
-            sender: Sender) {
-        self.chan.tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None);
+            _ctx: &Context<Self::Msg>,
+            _msg: Self::Msg,
+            _sender: Sender) {
+        self.chan.as_ref().unwrap().tell(Publish{msg: self.get_readings(), topic: "readings".into()}, None);
     }
 
-    fn post_start(&mut self, ctx: &Context<Self::Msg>) {
-        self.chan = channel("readings", &ctx.system).unwrap();
+    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, self, None, SendReading);
+        ctx.schedule(Duration::from_millis(0), dur, ctx.myself(), None, SendReading);
     }
 }
 
 impl OneWireSensor {
-    pub fn new(config: &Config) -> Self {
+    pub fn new(config: Config) -> Self {
         OneWireSensor {
             config: config.clone(),
+            chan: None,
         }
     }
 
@@ -92,22 +97,32 @@
     }
 }
 
-impl Sensor for OneWireSensor {
-}
+impl Actor for TestSensor {
+    type Msg = SendReading;
 
-#[derive(Clone)]
-pub struct TestSensor {
-    config: Config,
+    fn recv(&mut self,
+            _ctx: &Context<Self::Msg>,
+            _msg: Self::Msg,
+            _sender: Sender) {
+        self.chan.as_ref().unwrap().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 TestSensor {
-    pub fn new(config: &Config) -> Self {
+    pub fn new(config: Config) -> Self {
         TestSensor {
             config: config.clone(),
+            chan: None,
         }
     }
 
-    fn test_step() -> Readings {
+    fn get_readings(&self) -> Readings {
         let mut r = Readings::new();
         r.add("ambient", 31.2);
         r.add("wort", Self::try_read("test_wort.txt").unwrap_or_else(|_| 18.0));
@@ -121,7 +136,3 @@
         Ok(s.trim().parse::<f32>()?)
     }
 }
-
-impl Sensor for TestSensor {
-}
-