Mercurial > templog
annotate rust/src/types.rs @ 601:8c21df3711e2 rust
rigid_config
more on sensors
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 15 Feb 2017 23:58:02 +0800 |
parents | ca8102feaca6 |
children | b45b8b4cf0f5 |
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; |
595 | 3 use serde::{Deserialize,Serialize}; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
4 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
5 #[derive(Deserialize, Serialize, Debug)] |
592 | 6 pub struct Params { |
7 pub fridge_setpoint: f32, | |
8 pub fridge_difference: f32, | |
9 pub overshoot_delay: u32, | |
10 pub overshoot_factor: f32, | |
11 pub disabled: bool, | |
12 pub nowort: bool, | |
13 pub fridge_range_lower: f32, | |
14 pub fridge_range_upper: f32, | |
15 } | |
16 | |
17 impl Params { | |
18 pub fn defaults() -> Params { | |
19 Params { | |
20 fridge_setpoint: 16.0, | |
21 fridge_difference: 0.2, | |
22 overshoot_delay: 720, // 12 minutes | |
23 overshoot_factor: 1.0, | |
24 disabled: false, | |
25 nowort: false, | |
26 fridge_range_lower: 3.0, | |
27 fridge_range_upper: 3.0, | |
28 } | |
29 } | |
30 } | |
31 | |
32 #[derive(Debug)] | |
33 pub struct ParamHolder { | |
34 pub p: Params, | |
35 epoch: String, // XXX or a byte array? | |
36 } | |
37 | |
38 impl ParamHolder { | |
39 pub fn new() -> ParamHolder { | |
40 ParamHolder { | |
41 p: Params::defaults(), | |
42 epoch: String::new(), | |
43 } | |
44 } | |
45 } | |
46 | |
47 #[derive(Debug)] | |
48 pub struct Readings { | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
49 temps: HashMap<String, Option<f32>>, |
592 | 50 } |
51 | |
52 impl Readings { | |
53 pub fn new() -> Readings { | |
54 Readings { | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
55 temps: HashMap::new(), |
592 | 56 } |
57 } | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
58 |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
59 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
|
60 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
|
61 warn!("Replaced existing reading '{}' {:?} -> {:?}", |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
62 name, prev, v); |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
63 } |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
64 } |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
65 |
592 | 66 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
|
67 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
|
68 t.clone() |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
69 } else { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
70 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
|
71 None |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
72 } |
592 | 73 } |
74 | |
75 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
|
76 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
|
77 t.clone() |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
78 } else { |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
79 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
|
80 None |
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
81 } |
592 | 82 } |
83 } | |
84 |