annotate rust/src/types.rs @ 633:490e9e15b98c rust

move some bits to riker
author Matt Johnston <matt@ucc.asn.au>
date Wed, 04 Sep 2019 23:24:13 +0800
parents efcbe0d3afd6
children a5721c02d3ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
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;
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
8
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
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
11 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
12 use toml;
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
13 use hyper;
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
14 use serde_json;
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
15
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 #[derive(Debug)]
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 pub struct Readings {
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
18 pub temps: HashMap<String, f32>,
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 impl Readings {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 pub fn new() -> Readings {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 Readings {
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
24 temps: HashMap::new(),
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 }
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
27
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
28 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
29 self.temps.insert(name.to_string(), v);
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
30 }
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
31
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
32 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
33 self.temps.get(name).map(|f| *f)
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
37 #[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
38 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
39 None,
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
40 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
41 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
42 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
43 SerdeJson(serde_json::Error),
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
44 Hyper(hyper::Error),
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
45 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
46
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
47 #[derive(Debug)]
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
48 pub struct TemplogError {
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
49 msg: String,
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
50 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
51 kind: TemplogErrorKind,
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
52 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
53
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
54 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
55 fn description(&self) -> &str {
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
56 &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
57 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
58
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
59 fn cause(&self) -> Option<&dyn Error> {
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
60 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
61 TemplogErrorKind::None => None,
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
62 TemplogErrorKind::Io(ref e) => Some(e),
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
63 TemplogErrorKind::ParseFloat(ref e) => Some(e),
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
64 TemplogErrorKind::TomlDe(ref e) => Some(e),
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
65 TemplogErrorKind::SerdeJson(ref e) => Some(e),
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
66 TemplogErrorKind::Hyper(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
67 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
68 }
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
69
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
70 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
71
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
72 impl fmt::Display for TemplogError {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
74 write!(f, "{}", self.kind_str());
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
75 if !self.msg.is_empty() {
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
76 write!(f, ": {}", self.msg);
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
77 }
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
78 match self.kind {
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
79 TemplogErrorKind::None => Ok(()),
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
80 TemplogErrorKind::Io(ref e) => write!(f, ": {}", e),
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
81 TemplogErrorKind::TomlDe(ref e) => write!(f, ": {}", e),
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
82 TemplogErrorKind::SerdeJson(ref e) => write!(f, ": {}", e),
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
83 TemplogErrorKind::ParseFloat(ref e) => write!(f, ": {}", e),
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
84 TemplogErrorKind::Hyper(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
85 };
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
86 Ok(())
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
87 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
88 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
89
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
90 impl TemplogError {
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
91 pub fn new(msg: &str) -> Self {
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
92 TemplogError::new_kind(msg, TemplogErrorKind::None)
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
93 }
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
94
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
95 pub fn new_io(msg: &str, e: io::Error) -> Self {
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
96 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
97 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
98
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
99 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
100 TemplogError::new_kind(msg, TemplogErrorKind::TomlDe(e))
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
101 }
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
102
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
103 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
104 TemplogError::new_kind(msg, TemplogErrorKind::ParseFloat(e))
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
105 }
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
106
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
107 pub fn new_hyper(msg: &str, e: hyper::Error) -> Self {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
108 TemplogError::new_kind(msg, TemplogErrorKind::Hyper(e))
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
109 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
110
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
111 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
112 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
113 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
114
623
03167105f6de ignore missing local.conf
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
115 pub fn kind(&self) -> &TemplogErrorKind {
03167105f6de ignore missing local.conf
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
116 return &self.kind;
03167105f6de ignore missing local.conf
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
117 }
03167105f6de ignore missing local.conf
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
118
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
119 fn new_kind(msg: &str, k: TemplogErrorKind) -> Self {
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
120 let mut s = TemplogError {
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
121 msg: msg.to_string(),
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
122 desc: String::new(),
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
123 kind: k,
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
124 };
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
125 s.desc = if s.msg.is_empty() {
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
126 s.kind_str().to_string()
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
127 } else {
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
128 format!("{}: {}", s.kind_str(), s.msg)
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
129 };
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
130 s
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
131 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
132
612
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
133 fn kind_str(&self) -> &str {
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
134 match self.kind {
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
135 TemplogErrorKind::None => "Templog Error",
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
136 TemplogErrorKind::Io(_) => "Templog IO error",
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
137 TemplogErrorKind::TomlDe(_) => "Templog toml error",
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
138 TemplogErrorKind::SerdeJson(_) => "Templog Json decode error",
5fc41e0833b4 fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
139 TemplogErrorKind::ParseFloat(_) => "Templog parse error",
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
140 TemplogErrorKind::Hyper(_) => "Templog http error",
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
141 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
142 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
143 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
144
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
145 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
146 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
147 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
148 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
149 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
150
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
151 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
152 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
153 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
154 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
155 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
156
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
157 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
158 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
159 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
160 }
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
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
163 impl From<hyper::Error> for TemplogError {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
164 fn from(e: hyper::Error) -> Self {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 623
diff changeset
165 TemplogError::new_hyper("", e)
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
166 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
167 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
168
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
169 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
170 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
171 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
172 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 610
diff changeset
173 }
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
174
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
175 /// 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
176 #[derive(Clone)]
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
177 pub struct NotTooOften {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
178 last: Cell<Instant>,
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
179 limit: Duration,
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
180 }
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
181
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
182 impl NotTooOften {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
183 pub fn new(limit_secs: u64) -> Self {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
184 NotTooOften {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
185 limit: Duration::new(limit_secs, 0),
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
186 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
187 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
188 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
189
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
190 pub fn and_then<F>(&self, op: F)
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
191 where F: Fn() {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
192 let now = Instant::now();
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
193 if now - self.last.get() > self.limit {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
194 self.last.set(now);
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
195 op();
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
196 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
197 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
198 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
199
610
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
200 struct Period {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
201 start: Instant,
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
202 end: Option<Instant>,
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
203 }
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
204
610
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
205 pub struct StepIntegrator {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
206 on_periods: Vec<Period>,
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
207 limit: Duration,
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
208 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
209
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
210 impl StepIntegrator {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
211 pub fn new(limit: Duration) -> Self {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
212 StepIntegrator {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
213 on_periods: Vec::new(),
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
214 limit: limit,
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
215 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
216 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
217
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
218 pub fn turn(&mut self, on: bool) {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
219 self.trim();
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
220
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
221 if self.on_periods.is_empty() {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
222 self.on_periods.push( Period { start: Instant::now(), end: None });
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
223 return;
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
224 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
225
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
226 let current_on = self.on_periods.last().unwrap().end.is_none();
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
227 if on == current_on {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
228 // state is unchanged
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
229 return;
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
230 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
231
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
232 if on {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
233 self.on_periods.push( Period { start: Instant::now(), end: None });
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
234 } else {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
235 self.on_periods.last_mut().unwrap().end = Some(Instant::now());
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
236 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
237 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
238
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
239 pub fn set_limit(&mut self, limit: Duration) {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
240 self.limit = limit;
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
241 self.trim();
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
242 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
243
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 617
diff changeset
244 pub fn integrate(&self) -> Duration {
610
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
245 let durs = self.on_periods.iter().map(|p| {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
246 let end = p.end.unwrap_or_else(|| Instant::now());
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
247 end - p.start
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
248 });
617
87a78343140e Duration::sum works now
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
249 durs.sum()
610
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
250 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
251
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
252 fn trim(&mut self) {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
253 let begin = Instant::now() - self.limit;
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
254
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
255 self.on_periods.retain(|p| {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
256 // remove expired endtimes
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
257 if let Some(e) = p.end {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
258 e >= begin && e != p.start
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
259 } else {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
260 true
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
261 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
262 });
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
263
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
264 for p in &mut self.on_periods {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
265 p.start = cmp::max(p.start, begin);
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
266 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
267 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
268 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
269