annotate rust/src/types.rs @ 610:af0dac00d40b rust

untested step integrator
author Matt Johnston <matt@ucc.asn.au>
date Thu, 23 Feb 2017 23:27:09 +0800
parents 7bda01659426
children f3e39e2107fd
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;
610
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
5 use std::cmp;
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
6 use std::cell::Cell;
610
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
7 use std::iter::FromIterator;
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
8
595
e87655ed8429 add config
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
9 use serde::{Deserialize,Serialize};
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
10
596
ca8102feaca6 sensor takes config parameter
Matt Johnston <matt@ucc.asn.au>
parents: 595
diff changeset
11 #[derive(Deserialize, Serialize, Debug)]
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 pub struct Params {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 pub fridge_setpoint: f32,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 pub fridge_difference: f32,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 pub overshoot_delay: u32,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 pub overshoot_factor: f32,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 pub disabled: bool,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 pub nowort: bool,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 pub fridge_range_lower: f32,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 pub fridge_range_upper: f32,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 impl Params {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 pub fn defaults() -> Params {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 Params {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 fridge_setpoint: 16.0,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 fridge_difference: 0.2,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 overshoot_delay: 720, // 12 minutes
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 overshoot_factor: 1.0,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 disabled: false,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 nowort: false,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 fridge_range_lower: 3.0,
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 fridge_range_upper: 3.0,
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 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 #[derive(Debug)]
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 pub struct ParamHolder {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 pub p: Params,
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
41 epoch: String,
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 impl ParamHolder {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 pub fn new() -> ParamHolder {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 ParamHolder {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 p: Params::defaults(),
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 epoch: String::new(),
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 }
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
51
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
52 pub fn receive(&mut self, p: &Params, epoch: &String)
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
53 {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
54
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
55 }
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 #[derive(Debug)]
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 pub struct Readings {
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
60 pub temps: HashMap<String, f32>,
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 impl Readings {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 pub fn new() -> Readings {
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 Readings {
594
aff50ee77252 rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents: 593
diff changeset
66 temps: HashMap::new(),
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 }
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
69
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
70 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
71 self.temps.insert(name.to_string(), v);
593
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
72 }
bf138339d20a fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents: 592
diff changeset
73
603
b45b8b4cf0f5 get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents: 596
diff changeset
74 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
75 self.temps.get(name).map(|f| *f)
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
79 #[derive(Debug)]
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
80 pub struct TemplogError {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
81 desc: String,
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
82 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
83
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
84 impl Error for TemplogError {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
85 fn description(&self) -> &str { &self.desc }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
86 fn cause(&self) -> Option<&Error> { None }
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 impl fmt::Display for TemplogError {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
90 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
91 write!(f, "TemplogError: {}", self.desc);
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
92 Ok(())
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
93 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
94
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
95
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
96 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
97
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
98 impl TemplogError {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
99 pub fn new(desc: &str) -> Self {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
100 TemplogError {
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
101 desc: desc.to_string(),
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
102 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
103 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
104 }
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
105
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
106
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
107 /// Call closures with a rate limit. Useful for log message ratelimiting
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
108 pub struct NotTooOften {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
109 last: Cell<Instant>,
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
110 limit: Duration,
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
111 }
604
278f1002b5c7 sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents: 603
diff changeset
112
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
113 impl NotTooOften {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
114 pub fn new(limit_secs: u64) -> Self {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
115 NotTooOften {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
116 limit: Duration::new(limit_secs, 0),
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
117 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
118 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
119 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
120
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
121 pub fn and_then<F>(&self, op: F)
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
122 where F: Fn() {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
123 let now = Instant::now();
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
124 if now - self.last.get() > self.limit {
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
125 self.last.set(now);
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
126 op();
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
127 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
128 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
129 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
130
610
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
131 struct Period {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
132 start: Instant,
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
133 end: Option<Instant>,
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
134 }
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 604
diff changeset
135
610
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
136 pub struct StepIntegrator {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
137 on_periods: Vec<Period>,
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
138 limit: Duration,
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
139 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
140
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
141 impl StepIntegrator {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
142 pub fn new(limit: Duration) -> Self {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
143 StepIntegrator {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
144 on_periods: Vec::new(),
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
145 limit: limit,
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
146 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
147 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
148
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
149 pub fn turn(&mut self, on: bool) {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
150 self.trim();
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
151
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
152 if self.on_periods.is_empty() {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
153 self.on_periods.push( Period { start: Instant::now(), end: None });
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
154 return;
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
155 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
156
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
157 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
158 if on == current_on {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
159 // state is unchanged
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
160 return;
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
161 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
162
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
163 if on {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
164 self.on_periods.push( Period { start: Instant::now(), end: None });
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
165 } else {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
166 self.on_periods.last_mut().unwrap().end = Some(Instant::now());
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
167 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
168 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
169
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
170 pub fn set_limit(&mut self, limit: Duration) {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
171 self.limit = limit;
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
172 self.trim();
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
173 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
174
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
175 fn integrate(&self) -> Duration {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
176 let durs = self.on_periods.iter().map(|p| {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
177 let end = p.end.unwrap_or_else(|| Instant::now());
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
178 end - p.start
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
179 });
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
180 // TODO rust 1.16: impl Sum for Duration
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
181 // durs.sum()
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
182 durs.fold(Duration::new(0,0), |acc, d| acc + d)
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
183 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
184
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
185 fn trim(&mut self) {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
186 let begin = Instant::now() - self.limit;
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
187
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
188 self.on_periods.retain(|p| {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
189 // remove expired endtimes
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
190 if let Some(e) = p.end {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
191 e >= begin && e != p.start
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
192 } else {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
193 true
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
194 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
195 });
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
196
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
197 for p in &mut self.on_periods {
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
198 p.start = cmp::max(p.start, begin);
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
199 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
200 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
201 }
af0dac00d40b untested step integrator
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
202