annotate rust/src/fridge.rs @ 639:89818a14648b rust tip

- switch to using anyhow for errors, surf for http runs but surf has problems
author Matt Johnston <matt@ucc.asn.au>
date Thu, 28 Nov 2019 23:57:00 +0800
parents a9f353f488d0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
1 // TODO:
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
2 // - riker
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
3 // - use monotonic clock
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
4 // - timer.rs should use rx.recv_timeout(next_time) instead of rx.try_recv()
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
5 // and then could remove cfg.frequency_millis
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
6 use std;
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
7
597
a440eafa84a9 progress for debug
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
8 use std::time::{Duration,Instant};
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
9 use riker::actors::*;
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
10
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
11 #[cfg(target_os = "linux")]
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
12 use self::sysfs_gpio::{Direction, Pin};
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
13
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
14 use crate::params::Params;
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
15 use super::config::Config;
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
16 use super::params;
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
17 use super::sensor;
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
18 use super::types::*;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
20 #[derive(Debug,Clone)]
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
21 pub struct Tick;
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
22
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
23 #[actor(Params, Tick, Readings)]
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
24 pub struct Fridge {
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
25 params: Params,
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
26 config: Config,
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
27 testmode: bool,
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
28
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
29 on: bool,
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
30 temp_wort: Option<f32>,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
31 temp_fridge: Option<f32>,
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
32 last_off_time: Instant,
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
33 wort_valid_time: Instant,
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
34 integrator: StepIntegrator,
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
35 control: FridgeControl,
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
36 }
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
37
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
38 impl Actor for Fridge {
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
39 type Msg = FridgeMsg;
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
40 fn recv(&mut self,
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
41 ctx: &Context<Self::Msg>,
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
42 msg: Self::Msg,
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
43 sender: Sender) {
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
44 self.receive(ctx, msg, sender);
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
45 // TODO: should we do self.tick(ctx) here instead?
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
46 }
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
47
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
48 fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
49
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
50 let params_chan : ChannelRef<Params> = channel("params", ctx).unwrap();
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
51 let sensor_chan : ChannelRef<Readings> = channel("readings", ctx).unwrap();
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
52 let sub = Box::new(ctx.myself());
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
53 params_chan.tell(Subscribe {actor: sub.clone(), topic: "params".into()}, None);
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
54 sensor_chan.tell(Subscribe {actor: sub.clone(), topic: "readings".into()}, None);
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
55
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
56
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
57 // XXX a better way to get own reference?
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
58 let props = Props::new_args(params::ParamWaiter::new_actor, (self.config.clone(), params_chan));
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
59 ctx.actor_of(props, "paramwaiter").unwrap();
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
60
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
61 if self.testmode {
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
62 let props = Props::new_args(sensor::TestSensor::new_actor, (self.config.clone(), sensor_chan));
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
63 ctx.actor_of(props, "sensor").unwrap()
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
64 } else {
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
65 let props = Props::new_args(sensor::OneWireSensor::new_actor, (self.config.clone(), sensor_chan));
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
66 ctx.actor_of(props, "sensor").unwrap()
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
67 };
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
68
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
69 self.tick(ctx);
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
70 }
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
71 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
72
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
73 impl Receive<Readings> for Fridge {
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
74 type Msg = FridgeMsg;
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
75 fn receive(&mut self,
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
76 ctx: &Context<Self::Msg>,
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
77 r: Readings,
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
78 _sender: Sender) {
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
79 self.temp_wort = r.get_temp(&self.config.wort_name);
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
80 self.temp_fridge = r.get_temp(&self.config.fridge_name);
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
81
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
82 if self.temp_wort.is_some() {
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
83 self.wort_valid_time = Instant::now();
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
84 }
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
85
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
86 self.tick(ctx);
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
87 }
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
88 }
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
89
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
90 impl Receive<Params> for Fridge {
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
91 type Msg = FridgeMsg;
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
92 fn receive(&mut self,
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
93 ctx: &Context<Self::Msg>,
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
94 p: Params,
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
95 _sender: Sender) {
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
96 self.params = p;
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
97 println!("fridge set_params {:?}", self.params);
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
98
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
99 self.tick(ctx);
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
100 }
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
101 }
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
102
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
103 impl Receive<Tick> for Fridge {
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
104 type Msg = FridgeMsg;
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
105 fn receive(&mut self,
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
106 ctx: &Context<Self::Msg>,
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
107 _tick: Tick,
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
108 _sender: Sender) {
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
109 self.tick(ctx);
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
110 }
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
113 enum FridgeControl {
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
114 #[cfg(target_os = "linux")]
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
115 Gpio(Pin),
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
116 Fake,
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
117 }
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
118
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
119 impl Drop for Fridge {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
120 fn drop(&mut self) {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
121 // safety fridge off
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
122 self.turn(false);
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
123 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
124 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
125
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
126 impl Fridge {
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
127 pub fn new_actor((config, testmode, nowait)
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
128 : (Config, bool, bool)) -> Fridge {
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
129 Self::new(config, testmode, nowait)
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
130
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
131 }
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
132 pub fn new(config: Config,
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
133 testmode: bool,
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
134 nowait: bool) -> Fridge {
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
135 let mut f = Fridge {
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 601
diff changeset
136 config: config.clone(),
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
137 params: Params::defaults(),
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
138 on: false,
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
139 temp_wort: None,
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
140 temp_fridge: None,
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
141 last_off_time: Instant::now(),
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
142 wort_valid_time: Instant::now() - Duration::new(config.fridge_wort_invalid_time, 100),
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
143 integrator: StepIntegrator::new(Duration::new(1, 0)),
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
144 control: Self::make_control(&config),
638
a9f353f488d0 fix channels
Matt Johnston <matt@ucc.asn.au>
parents: 636
diff changeset
145 testmode: testmode,
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
146 };
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
147
597
a440eafa84a9 progress for debug
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
148 if nowait {
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
149 f.last_off_time -= Duration::new(config.fridge_delay, 1);
597
a440eafa84a9 progress for debug
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
150 }
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
151
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
152 f
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
153 }
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
154
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
155 #[cfg(target_os = "linux")]
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
156 fn make_control(config: &Config) -> FridgeControl {
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
157 let mut pin = Pin(config.fridge_gpio_pin);
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
158 // XXX better error handling?
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
159 pin.export().expect("Exporting fridge gpio failed");
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
160 pin.set_direction(Direction::Low).expect("Fridge gpio direction failed");
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
161 FridgeControl::Gpio(pin)
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
162 }
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
163
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
164 #[cfg(not(target_os = "linux"))]
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
165 fn make_control(_config: &Config) -> FridgeControl {
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
166 FridgeControl::Fake
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
167 }
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
168
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
169 fn next_wakeup(&self) -> Duration {
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
170 let millis = 400;
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
171 let dur = Duration::from_millis(millis);
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
172 dur
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
173 }
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
174
606
3c1d37d78415 untested wort_valid_time
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
175
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
176 fn turn_off(&mut self) {
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
177 info!("Turning fridge off");
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
178 self.turn(false);
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
179 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
180
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
181 fn turn_on(&mut self) {
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
182 info!("Turning fridge on");
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
183 self.turn(true);
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
184 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
185
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
186 fn turn(&mut self, on: bool) {
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
187 match self.control {
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
188 #[cfg(target_os = "linux")]
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
189 Gpio(pin) => pin.set_value(on as u8),
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
190 FridgeControl::Fake => debug!("fridge turns {}", if on {"on"} else {"off"}),
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
191 }
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
192 self.on = on;
636
43eb3cfdf769 some progress, better error handling
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
193 self.integrator.turn(on)
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
194 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
195
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
196 // Turns the fridge off and on
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
197 fn compare_temperatures(&mut self) {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
198 let fridge_min = self.params.fridge_setpoint - self.params.fridge_range_lower;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
199 let fridge_max = self.params.fridge_setpoint - self.params.fridge_range_upper;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
200 let wort_max = self.params.fridge_setpoint + self.params.fridge_difference;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
201 let off_time = Instant::now() - self.last_off_time;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
202
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
203 // Or elsewhere?
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
204 self.integrator.set_limit(Duration::new(self.params.overshoot_delay, 0));
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
205
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
206 // Safety to avoid bad things happening to the fridge motor (?)
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
207 // When it turns off don't start up again for at least FRIDGE_DELAY
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
208 if !self.on && off_time < Duration::new(self.config.fridge_delay, 0) {
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
209 info!("fridge skipping, too early");
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
210 return;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
211 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
212
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
213 if self.params.disabled {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
214 if self.on {
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
215 info!("Disabled, turning fridge off");
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
216 self.turn_off();
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
217 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
218 return;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
219 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
220
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
221 // handle broken wort sensor
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
222 if self.temp_wort.is_none() {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
223 let invalid_time = Instant::now() - self.wort_valid_time;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
224 warn!("Invalid wort sensor for {:?} secs", invalid_time);
635
4424a8b30f9c config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents: 634
diff changeset
225 if invalid_time < Duration::new(self.config.fridge_wort_invalid_time, 0) {
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
226 warn!("Has only been invalid for {:?}, waiting", invalid_time);
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
227 return;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
228 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
229 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
230
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
231 if self.temp_fridge.is_none() {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
232 warn!("Invalid fridge sensor");
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
233 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
234
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
235 if self.on {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
236 debug!("fridge is on");
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
237 let on_time = self.integrator.integrate().as_secs() as f32;
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
238 let on_ratio = on_time / self.params.overshoot_delay as f32;
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
239
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
240 let overshoot = self.params.overshoot_factor as f32 * on_ratio;
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
241 debug!("on_percent {}, overshoot {}", on_ratio * 100.0, overshoot);
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
242
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
243 let mut turn_off = false;
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
244 if self.temp_wort.is_some() && !self.params.nowort {
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
245 let t = self.temp_wort.unwrap();
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
246 // use the wort temperature
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
247 if t - overshoot < self.params.fridge_setpoint {
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
248 info!("wort has cooled enough, {temp}º (overshoot {overshoot}º = {factor} × {percent}%)",
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
249 temp = t, overshoot = overshoot,
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
250 factor = self.params.overshoot_factor,
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
251 percent = on_ratio*100.0);
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
252 turn_off = true;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
253 }
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
254 } else if let Some(t) = self.temp_fridge {
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
255 // use the fridge temperature
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
256 if t < fridge_min {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
257 warn!("fridge off fallback, fridge {}, min {}", t, fridge_min);
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
258 if self.temp_wort.is_none() {
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
259 warn!("wort has been invalid for {:?}", Instant::now() - self.wort_valid_time);
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
260 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
261 turn_off = true;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
262 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
263 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
264 if turn_off {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
265 self.turn_off();
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
266 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
267 } else {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
268 debug!("fridge is off. fridge {:?} max {:?}. wort {:?} max {:?}",
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
269 self.temp_fridge, fridge_max, self.temp_wort, wort_max);
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
270 let mut turn_on = false;
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
271 if self.temp_wort.is_some() && !self.params.nowort {
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
272 // use the wort temperature
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
273 let t = self.temp_wort.unwrap();
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
274 if t >= wort_max {
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
275 info!("Wort is too hot {}°, max {}°", t, wort_max);
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
276 turn_on = true;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
277 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
278 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
279
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
280 if let Some(t) = self.temp_fridge {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
281 if t >= fridge_max {
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
282 warn!("fridge too hot fallback, fridge {}°, max {}°", t, fridge_max);
620
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
283 turn_on = true;
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
284 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
285 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
286
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
287 if turn_on {
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
288 self.turn_on()
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
289 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
290 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
291 }
8fda564cc46f fridge work
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
292
606
3c1d37d78415 untested wort_valid_time
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
293 /// Must be called after every state change. Turns the fridge on/off as required and
3c1d37d78415 untested wort_valid_time
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
294 /// schedules any future wakeups based on the present (new) state
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
295 /// Examples of wakeups events are
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
296 ///
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
297 /// * overshoot calculation
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
298 /// * minimum fridge-off time
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
299 /// * invalid wort timeout
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
300 /// All specified in next_wakeup()
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
301 fn tick(&mut self,
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
302 ctx: &Context<<Self as Actor>::Msg>) {
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
303 debug!("tick");
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
304
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 620
diff changeset
305 self.compare_temperatures();
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306
632
bde302def78e moving to riker, nowhere near yet
Matt Johnston <matt@ucc.asn.au>
parents: 631
diff changeset
307 // Sets the next self-wakeup timeout
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
308 let dur = self.next_wakeup();
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
309 ctx.schedule_once(dur, ctx.myself(), None, Tick);
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
310 }
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311 }