diff rust/src/fridge.rs @ 638:a9f353f488d0 rust

fix channels
author Matt Johnston <matt@ucc.asn.au>
date Sat, 09 Nov 2019 11:35:59 +0800
parents 43eb3cfdf769
children
line wrap: on
line diff
--- a/rust/src/fridge.rs	Sat Nov 09 11:35:43 2019 +0800
+++ b/rust/src/fridge.rs	Sat Nov 09 11:35:59 2019 +0800
@@ -11,8 +11,10 @@
 #[cfg(target_os = "linux")]
 use self::sysfs_gpio::{Direction, Pin};
 
+use crate::params::Params;
 use super::config::Config;
-use super::params::Params;
+use super::params;
+use super::sensor;
 use super::types::*;
 
 #[derive(Debug,Clone)]
@@ -22,6 +24,7 @@
 pub struct Fridge {
     params: Params,
     config: Config,
+    testmode: bool,
 
     on: bool,
     temp_wort: Option<f32>,
@@ -43,13 +46,25 @@
     }
 
     fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
-        let chan: ChannelRef<Readings> = channel("readings", &ctx.system).unwrap();
+
+        let params_chan : ChannelRef<Params> = channel("params", ctx).unwrap();
+        let sensor_chan : ChannelRef<Readings> = channel("readings", ctx).unwrap();
         let sub = Box::new(ctx.myself());
-        chan.tell(Subscribe {actor: sub, topic: "readings".into()}, None);
+        params_chan.tell(Subscribe {actor: sub.clone(), topic: "params".into()}, None);
+        sensor_chan.tell(Subscribe {actor: sub.clone(), topic: "readings".into()}, None);
+
 
-        let chan: ChannelRef<Params> = channel("params", &ctx.system).unwrap();
-        let sub = Box::new(ctx.myself());
-        chan.tell(Subscribe {actor: sub, topic: "params".into()}, None);
+        // XXX a better way to get own reference?
+        let props = Props::new_args(params::ParamWaiter::new_actor, (self.config.clone(), params_chan));
+        ctx.actor_of(props, "paramwaiter").unwrap();
+
+        if self.testmode {
+            let props = Props::new_args(sensor::TestSensor::new_actor, (self.config.clone(), sensor_chan));
+            ctx.actor_of(props, "sensor").unwrap()
+        } else {
+            let props = Props::new_args(sensor::OneWireSensor::new_actor, (self.config.clone(), sensor_chan));
+            ctx.actor_of(props, "sensor").unwrap()
+        };
 
         self.tick(ctx);
     }
@@ -109,11 +124,14 @@
 }
 
 impl Fridge {
-    pub fn new_actor((config, nowait) : (Config, bool)) -> Fridge {
-        Self::new(config, nowait)
+    pub fn new_actor((config, testmode, nowait) 
+        : (Config, bool, bool)) -> Fridge {
+        Self::new(config, testmode, nowait)
 
     }
-    pub fn new(config: Config, nowait: bool) -> Fridge {
+    pub fn new(config: Config, 
+            testmode: bool,
+            nowait: bool) -> Fridge {
         let mut f = Fridge { 
             config: config.clone(),
             params: Params::defaults(),
@@ -124,6 +142,7 @@
             wort_valid_time: Instant::now() - Duration::new(config.fridge_wort_invalid_time, 100),
             integrator: StepIntegrator::new(Duration::new(1, 0)),
             control: Self::make_control(&config),
+            testmode: testmode,
         };
 
         if nowait {