Mercurial > templog
annotate rust/src/types.rs @ 594:aff50ee77252 rust
rust working better now with streams and sinks.
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 04 Jan 2017 17:18:44 +0800 |
parents | bf138339d20a |
children | e87655ed8429 |
rev | line source |
---|---|
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
1 use std::collections::HashMap; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
2 use std::time::Duration; |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
3 |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
4 #[derive(RustcDecodable, RustcEncodable, Debug, Clone)] |
592 | 5 pub struct Params { |
6 pub fridge_setpoint: f32, | |
7 pub fridge_difference: f32, | |
8 pub overshoot_delay: u32, | |
9 pub overshoot_factor: f32, | |
10 pub disabled: bool, | |
11 pub nowort: bool, | |
12 pub fridge_range_lower: f32, | |
13 pub fridge_range_upper: f32, | |
14 } | |
15 | |
16 impl Params { | |
17 pub fn defaults() -> Params { | |
18 Params { | |
19 fridge_setpoint: 16.0, | |
20 fridge_difference: 0.2, | |
21 overshoot_delay: 720, // 12 minutes | |
22 overshoot_factor: 1.0, | |
23 disabled: false, | |
24 nowort: false, | |
25 fridge_range_lower: 3.0, | |
26 fridge_range_upper: 3.0, | |
27 } | |
28 } | |
29 } | |
30 | |
31 #[derive(Debug)] | |
32 pub struct ParamHolder { | |
33 pub p: Params, | |
34 epoch: String, // XXX or a byte array? | |
35 } | |
36 | |
37 impl ParamHolder { | |
38 pub fn new() -> ParamHolder { | |
39 ParamHolder { | |
40 p: Params::defaults(), | |
41 epoch: String::new(), | |
42 } | |
43 } | |
44 } | |
45 | |
46 #[derive(Debug)] | |
47 pub struct Readings { | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
48 temps: HashMap<String, Option<f32>>, |
592 | 49 } |
50 | |
51 impl Readings { | |
52 pub fn new() -> Readings { | |
53 Readings { | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
54 temps: HashMap::new(), |
592 | 55 } |
56 } | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
57 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
58 pub fn add(&mut self, name: &str, v: Option<f32>) { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
59 if let Some(prev) = self.temps.insert(name.to_string(), v) { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
60 warn!("Replaced existing reading '{}' {:?} -> {:?}", |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
61 name, prev, v); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
62 } |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
63 } |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
64 |
592 | 65 pub fn fridge(&self) -> Option<f32> { |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
66 if let Some(t) = self.temps.get("fridge") { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
67 t.clone() |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
68 } else { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
69 warn!("No fridge reading was added"); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
70 None |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
71 } |
592 | 72 } |
73 | |
74 pub fn wort(&self) -> Option<f32> { | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
75 if let Some(t) = self.temps.get("wort") { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
76 t.clone() |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
77 } else { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
78 warn!("No wort reading was added"); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
79 None |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
80 } |
592 | 81 } |
82 } | |
83 |