comparison rust/src/fridge.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
1 // TODO:
2 // - riker
3 // - use monotonic clock
4 // - timer.rs should use rx.recv_timeout(next_time) instead of rx.try_recv()
5 // and then could remove cfg.frequency_millis
1 use std; 6 use std;
2 7
3 use std::time::{Duration,Instant}; 8 use std::time::{Duration,Instant};
4 use riker::actors::*; 9 use riker::actors::*;
5 10
8 13
9 use super::config::Config; 14 use super::config::Config;
10 use super::params::Params; 15 use super::params::Params;
11 use super::types::*; 16 use super::types::*;
12 17
13 #[derive(Debug)] 18 #[derive(Debug,Clone)]
14 pub struct Tick; 19 pub struct Tick;
15
16 20
17 #[actor(Params, Tick, Readings)] 21 #[actor(Params, Tick, Readings)]
18 pub struct Fridge { 22 pub struct Fridge {
19 params: Params, 23 params: Params,
20 config: Config, 24 config: Config,
36 sender: Sender) { 40 sender: Sender) {
37 self.receive(ctx, msg, sender); 41 self.receive(ctx, msg, sender);
38 // TODO: should we do self.tick(ctx) here instead? 42 // TODO: should we do self.tick(ctx) here instead?
39 } 43 }
40 44
41 fn post_start(&mut self, ctx: &Context<Self::Msg>) { 45 fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
42 self.tick(ctx); 46 let chan: ChannelRef<Readings> = channel("readings", &ctx.system).unwrap();
43
44 let chan = channel("readings", &ctx.system).unwrap();
45 let sub = Box::new(ctx.myself()); 47 let sub = Box::new(ctx.myself());
46 chan.tell(Subscribe {actor: sub, topic: "readings".into()}, None); 48 chan.tell(Subscribe {actor: sub, topic: "readings".into()}, None);
47 49
48 let chan = channel("params", &ctx.system).unwrap(); 50 let chan: ChannelRef<Params> = channel("params", &ctx.system).unwrap();
49 let sub = Box::new(ctx.myself()); 51 let sub = Box::new(ctx.myself());
50 chan.tell(Subscribe {actor: sub, topic: "params".into()}, None); 52 chan.tell(Subscribe {actor: sub, topic: "params".into()}, None);
53
54 self.tick(ctx);
51 } 55 }
52 } 56 }
53 57
54 impl Receive<Readings> for Fridge { 58 impl Receive<Readings> for Fridge {
55 type Msg = FridgeMsg; 59 type Msg = FridgeMsg;
56 fn receive(&mut self, 60 fn receive(&mut self,
57 ctx: &Context<Self::Msg>, 61 ctx: &Context<Self::Msg>,
58 r: Readings, 62 r: Readings,
59 _sender: Sender) { 63 _sender: Sender) {
60 self.temp_wort = r.get_temp(self.config.WORT_NAME); 64 self.temp_wort = r.get_temp(&self.config.WORT_NAME);
61 self.temp_fridge = r.get_temp(self.config.FRIDGE_NAME); 65 self.temp_fridge = r.get_temp(&self.config.FRIDGE_NAME);
62 66
63 if self.temp_wort.is_some() { 67 if self.temp_wort.is_some() {
64 self.wort_valid_time = Instant::now(); 68 self.wort_valid_time = Instant::now();
65 } 69 }
66 70
103 self.turn(false); 107 self.turn(false);
104 } 108 }
105 } 109 }
106 110
107 impl Fridge { 111 impl Fridge {
108 pub fn new(config: &Config, nowait: bool, p: Params) -> Fridge { 112 pub fn new_actor((config, nowait) : (Config, bool)) -> Fridge {
113 Self::new(config, nowait)
114
115 }
116 pub fn new(config: Config, nowait: bool) -> Fridge {
109 let mut f = Fridge { 117 let mut f = Fridge {
110 config: config.clone(), 118 config: config.clone(),
111 params: p.clone(), 119 params: Params::defaults(),
112 on: false, 120 on: false,
113 temp_wort: None, 121 temp_wort: None,
114 temp_fridge: None, 122 temp_fridge: None,
115 last_off_time: Instant::now(), 123 last_off_time: Instant::now(),
116 wort_valid_time: Instant::now() - Duration::new(config.FRIDGE_WORT_INVALID_TIME, 100), 124 wort_valid_time: Instant::now() - Duration::new(config.FRIDGE_WORT_INVALID_TIME, 100),
117 integrator: StepIntegrator::new(Duration::new(p.overshoot_delay, 0)), 125 integrator: StepIntegrator::new(Duration::new(1, 0)),
118 control: Self::make_control(config), 126 control: Self::make_control(&config),
119 }; 127 };
120 128
121 if nowait { 129 if nowait {
122 f.last_off_time -= Duration::new(config.FRIDGE_DELAY, 1); 130 f.last_off_time -= Duration::new(config.FRIDGE_DELAY, 1);
123 } 131 }
124
125 f.tick();
126 132
127 f 133 f
128 } 134 }
129 135
130 #[cfg(target_os = "linux")] 136 #[cfg(target_os = "linux")]
135 pin.set_direction(Direction::Low).expect("Fridge gpio direction failed"); 141 pin.set_direction(Direction::Low).expect("Fridge gpio direction failed");
136 FridgeControl::Gpio(pin) 142 FridgeControl::Gpio(pin)
137 } 143 }
138 144
139 #[cfg(not(target_os = "linux"))] 145 #[cfg(not(target_os = "linux"))]
140 fn make_control(config: &Config) -> FridgeControl { 146 fn make_control(_config: &Config) -> FridgeControl {
141 FridgeControl::Fake 147 FridgeControl::Fake
142 } 148 }
143 149
144 fn next_wakeup(&self) -> Duration { 150 fn next_wakeup(&self) -> Duration {
145 let millis = 400; 151 let millis = 400;
278 284
279 self.compare_temperatures(); 285 self.compare_temperatures();
280 286
281 // Sets the next self-wakeup timeout 287 // Sets the next self-wakeup timeout
282 let dur = self.next_wakeup(); 288 let dur = self.next_wakeup();
283 ctx.schedule_once(dur, self, None, Tick); 289 ctx.schedule_once(dur, ctx.myself(), None, Tick);
284 } 290 }
285 } 291 }