Mercurial > templog
annotate rust/src/types.rs @ 612:5fc41e0833b4 rust
fix TemplogError references
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 02 Mar 2017 00:06:03 +0800 |
parents | f3e39e2107fd |
children | f153aec221be |
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; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
2 use std::time::{Duration,Instant}; |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
3 use std::error::Error; |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
4 use std::fmt; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
5 use std::io; |
610 | 6 use std::cmp; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
7 use std::cell::Cell; |
610 | 8 use std::iter::FromIterator; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
9 use std; |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
10 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
11 use futures::{Stream,IntoFuture}; |
595 | 12 use serde::{Deserialize,Serialize}; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
13 use toml; |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
14 use curl; |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
15 use serde_json; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
16 |
596
ca8102feaca6
sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents:
595
diff
changeset
|
17 #[derive(Deserialize, Serialize, Debug)] |
592 | 18 pub struct Params { |
19 pub fridge_setpoint: f32, | |
20 pub fridge_difference: f32, | |
21 pub overshoot_delay: u32, | |
22 pub overshoot_factor: f32, | |
23 pub disabled: bool, | |
24 pub nowort: bool, | |
25 pub fridge_range_lower: f32, | |
26 pub fridge_range_upper: f32, | |
27 } | |
28 | |
29 impl Params { | |
30 pub fn defaults() -> Params { | |
31 Params { | |
32 fridge_setpoint: 16.0, | |
33 fridge_difference: 0.2, | |
34 overshoot_delay: 720, // 12 minutes | |
35 overshoot_factor: 1.0, | |
36 disabled: false, | |
37 nowort: false, | |
38 fridge_range_lower: 3.0, | |
39 fridge_range_upper: 3.0, | |
40 } | |
41 } | |
42 } | |
43 | |
44 #[derive(Debug)] | |
45 pub struct ParamHolder { | |
46 pub p: Params, | |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
47 epoch: String, |
592 | 48 } |
49 | |
50 impl ParamHolder { | |
51 pub fn new() -> ParamHolder { | |
52 ParamHolder { | |
53 p: Params::defaults(), | |
54 epoch: String::new(), | |
55 } | |
56 } | |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
57 |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
58 pub fn receive(&mut self, p: &Params, epoch: &String) |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
59 { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
60 |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
61 } |
592 | 62 } |
63 | |
64 #[derive(Debug)] | |
65 pub struct Readings { | |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
66 pub temps: HashMap<String, f32>, |
592 | 67 } |
68 | |
69 impl Readings { | |
70 pub fn new() -> Readings { | |
71 Readings { | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
72 temps: HashMap::new(), |
592 | 73 } |
74 } | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
75 |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
76 pub fn add(&mut self, name: &str, v: f32) { |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
77 self.temps.insert(name.to_string(), v); |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
78 } |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
79 |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
80 pub fn get_temp(&self, name: &str) -> Option<f32> { |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
81 self.temps.get(name).map(|f| *f) |
592 | 82 } |
83 } | |
84 | |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
85 #[derive(Debug)] |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
86 pub enum TemplogErrorKind { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
87 None, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
88 Io(io::Error), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
89 ParseFloat(std::num::ParseFloatError), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
90 TomlDe(toml::de::Error), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
91 SerdeJson(serde_json::Error), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
92 Curl(curl::Error), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
93 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
94 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
95 #[derive(Debug)] |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
96 pub struct TemplogError { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
97 msg: String, |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
98 desc: String, |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
99 kind: TemplogErrorKind, |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
100 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
101 |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
102 impl Error for TemplogError { |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
103 fn description(&self) -> &str { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
104 &self.desc |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
105 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
106 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
107 fn cause(&self) -> Option<&Error> { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
108 match self.kind { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
109 TemplogErrorKind::None => None, |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
110 TemplogErrorKind::Io(ref e) => Some(e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
111 TemplogErrorKind::ParseFloat(ref e) => Some(e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
112 TemplogErrorKind::TomlDe(ref e) => Some(e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
113 TemplogErrorKind::SerdeJson(ref e) => Some(e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
114 TemplogErrorKind::Curl(ref e) => Some(e), |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
115 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
116 } |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
117 |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
118 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
119 |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
120 impl fmt::Display for TemplogError { |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
121 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
122 write!(f, "{}", self.kind_str()); |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
123 if !self.msg.is_empty() { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
124 write!(f, ": {}", self.msg); |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
125 } |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
126 match self.kind { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
127 TemplogErrorKind::None => Ok(()), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
128 TemplogErrorKind::Io(ref e) => write!(f, ": {}", e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
129 TemplogErrorKind::TomlDe(ref e) => write!(f, ": {}", e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
130 TemplogErrorKind::SerdeJson(ref e) => write!(f, ": {}", e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
131 TemplogErrorKind::ParseFloat(ref e) => write!(f, ": {}", e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
132 TemplogErrorKind::Curl(ref e) => write!(f, ": {}", e), |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
133 }; |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
134 Ok(()) |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
135 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
136 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
137 |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
138 impl TemplogError { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
139 pub fn new(msg: &str) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
140 TemplogError::new_kind(msg, TemplogErrorKind::None) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
141 } |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
142 |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
143 pub fn new_io(msg: &str, e: io::Error) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
144 TemplogError::new_kind(msg, TemplogErrorKind::Io(e)) |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
145 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
146 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
147 pub fn new_toml_de(msg: &str, e: toml::de::Error) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
148 TemplogError::new_kind(msg, TemplogErrorKind::TomlDe(e)) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
149 } |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
150 |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
151 pub fn new_parse_float(msg: &str, e: std::num::ParseFloatError) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
152 TemplogError::new_kind(msg, TemplogErrorKind::ParseFloat(e)) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
153 } |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
154 |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
155 pub fn new_curl(msg: &str, e: curl::Error) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
156 TemplogError::new_kind(msg, TemplogErrorKind::Curl(e)) |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
157 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
158 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
159 pub fn new_serde_json(msg: &str, e: serde_json::Error) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
160 TemplogError::new_kind(msg, TemplogErrorKind::SerdeJson(e)) |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
161 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
162 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
163 fn new_kind(msg: &str, k: TemplogErrorKind) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
164 let mut s = TemplogError { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
165 msg: msg.to_string(), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
166 desc: String::new(), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
167 kind: k, |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
168 }; |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
169 s.desc = if s.msg.is_empty() { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
170 s.kind_str().to_string() |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
171 } else { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
172 format!("{}: {}", s.kind_str(), s.msg) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
173 }; |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
174 s |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
175 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
176 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
177 fn kind_str(&self) -> &str { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
178 match self.kind { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
179 TemplogErrorKind::None => "Templog Error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
180 TemplogErrorKind::Io(_) => "Templog IO error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
181 TemplogErrorKind::TomlDe(_) => "Templog toml error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
182 TemplogErrorKind::SerdeJson(_) => "Templog Json decode error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
183 TemplogErrorKind::ParseFloat(_) => "Templog parse error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
184 TemplogErrorKind::Curl(_) => "Templog curl http error", |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
185 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
186 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
187 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
188 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
189 impl From<io::Error> for TemplogError { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
190 fn from(e: io::Error) -> Self { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
191 TemplogError::new_io("", e) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
192 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
193 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
194 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
195 impl From<toml::de::Error> for TemplogError { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
196 fn from(e: toml::de::Error) -> Self { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
197 TemplogError::new_toml_de("", e) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
198 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
199 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
200 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
201 impl From<std::num::ParseFloatError> for TemplogError { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
202 fn from(e: std::num::ParseFloatError) -> Self { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
203 TemplogError::new_parse_float("", e) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
204 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
205 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
206 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
207 impl From<curl::Error> for TemplogError { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
208 fn from(e: curl::Error) -> Self { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
209 TemplogError::new_curl("", e) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
210 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
211 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
212 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
213 impl From<serde_json::Error> for TemplogError { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
214 fn from(e: serde_json::Error) -> Self { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
215 TemplogError::new_serde_json("", e) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
216 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
217 } |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
218 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
219 /// Call closures with a rate limit. Useful for log message ratelimiting |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
220 #[derive(Clone)] |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
221 pub struct NotTooOften { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
222 last: Cell<Instant>, |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
223 limit: Duration, |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
224 } |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
225 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
226 impl NotTooOften { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
227 pub fn new(limit_secs: u64) -> Self { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
228 NotTooOften { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
229 limit: Duration::new(limit_secs, 0), |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
230 last: Cell::new(Instant::now() - Duration::new(limit_secs+1, 0)), |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
231 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
232 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
233 |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
234 pub fn and_then<F>(&self, op: F) |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
235 where F: Fn() { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
236 let now = Instant::now(); |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
237 if now - self.last.get() > self.limit { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
238 self.last.set(now); |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
239 op(); |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
240 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
241 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
242 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
243 |
610 | 244 struct Period { |
245 start: Instant, | |
246 end: Option<Instant>, | |
247 } | |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
248 |
610 | 249 pub struct StepIntegrator { |
250 on_periods: Vec<Period>, | |
251 limit: Duration, | |
252 } | |
253 | |
254 impl StepIntegrator { | |
255 pub fn new(limit: Duration) -> Self { | |
256 StepIntegrator { | |
257 on_periods: Vec::new(), | |
258 limit: limit, | |
259 } | |
260 } | |
261 | |
262 pub fn turn(&mut self, on: bool) { | |
263 self.trim(); | |
264 | |
265 if self.on_periods.is_empty() { | |
266 self.on_periods.push( Period { start: Instant::now(), end: None }); | |
267 return; | |
268 } | |
269 | |
270 let current_on = self.on_periods.last().unwrap().end.is_none(); | |
271 if on == current_on { | |
272 // state is unchanged | |
273 return; | |
274 } | |
275 | |
276 if on { | |
277 self.on_periods.push( Period { start: Instant::now(), end: None }); | |
278 } else { | |
279 self.on_periods.last_mut().unwrap().end = Some(Instant::now()); | |
280 } | |
281 } | |
282 | |
283 pub fn set_limit(&mut self, limit: Duration) { | |
284 self.limit = limit; | |
285 self.trim(); | |
286 } | |
287 | |
288 fn integrate(&self) -> Duration { | |
289 let durs = self.on_periods.iter().map(|p| { | |
290 let end = p.end.unwrap_or_else(|| Instant::now()); | |
291 end - p.start | |
292 }); | |
293 // TODO rust 1.16: impl Sum for Duration | |
294 // durs.sum() | |
295 durs.fold(Duration::new(0,0), |acc, d| acc + d) | |
296 } | |
297 | |
298 fn trim(&mut self) { | |
299 let begin = Instant::now() - self.limit; | |
300 | |
301 self.on_periods.retain(|p| { | |
302 // remove expired endtimes | |
303 if let Some(e) = p.end { | |
304 e >= begin && e != p.start | |
305 } else { | |
306 true | |
307 } | |
308 }); | |
309 | |
310 for p in &mut self.on_periods { | |
311 p.start = cmp::max(p.start, begin); | |
312 } | |
313 } | |
314 } | |
315 | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
316 /// Takes a stream and returns a stream without errors. |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
317 pub fn consume_errors<S>(s: S) -> Box<Stream<Item=S::Item, Error=S::Error>> |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
318 // XXX not sure why 'static is really necessary here? |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
319 where |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
320 S: Stream+Send+'static, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
321 <S as Stream>::Error: std::fmt::Display+Send+'static, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
322 <S as Stream>::Item: Send+'static, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
323 { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
324 s.then(|r| { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
325 match r { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
326 Ok(v) => Ok(Some(v)), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
327 Err(e) => { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
328 debug!("Stream error: {}", e); |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
329 Ok(None) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
330 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
331 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
332 }) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
333 .filter_map(|p| p).boxed() |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
334 } |