1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::{
error::DataError,
util::{check_deserialization, check_range},
};
/// Configuration for the VOC Index algorithm.
#[derive(Debug, PartialEq)]
pub struct VocTuning(Tuning);
impl VocTuning {
/// Creates a new [`VocTuning`](VocTuning) Index configuration:
/// - `index_offset`: VOC Index representing typical conditions. Range: 1 - 250, Default: 100.
/// - `learning_time_offset`: Time constant to estimate the offset from the history in hours.
/// After twice the learning time events are forgotten. Range: 1 - 1,000h, Default: 12h
/// - `learning_time_gain`: Time constant to estimate the gain from the history in hours.
/// After twice the learning time events are forgotten. Range 1 - 1,000h, Default: 12h
/// - `gating_max_durations`: Maximum duration the estimator freezes on a high VOC index
/// signal. Zero disables the gating. Range 0 - 3,000min, Default 180min.
/// - `initial_standard_deviation`: Initial estimate for the standard deviation. Range 10 -
/// 5,000, Default: 50.
/// - `gain_factor`: Factor to amplify/attunate the VOC index output. Range 1 - 1,000, Default:
/// 230.
///
/// # Errors
///
/// - [`ValueOutOfRange`](crate::error::DataError::ValueOutOfRange)`: If the values with scaling
/// are not in range.
pub fn new(
index_offset: i16,
learning_time_offset: i16,
learning_time_gain: i16,
gating_max_durations: i16,
initial_standard_deviation: i16,
gain_factor: i16,
) -> Result<Self, DataError> {
Ok(Self(Tuning::new(
index_offset,
learning_time_offset,
learning_time_gain,
gating_max_durations,
initial_standard_deviation,
gain_factor,
)?))
}
}
impl From<VocTuning> for [u16; 6] {
fn from(value: VocTuning) -> Self {
<[u16; 6]>::from(value.0)
}
}
impl TryFrom<&[u8]> for VocTuning {
type Error = DataError;
/// Parse the VOC tuning parameters from the received data.
///
/// # Errors
///
/// - [`CrcFailed`](crate::error::DataError::CrcFailed): If the received data CRC indicates
/// corruption.
/// - [`ReceivedBufferWrongSize`](crate::error::DataError::ReceivedBufferWrongSize): If the
/// received data buffer is not the expected size.
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
Ok(VocTuning(Tuning::try_from(data)?))
}
}
impl Default for VocTuning {
/// Creates a default [`VocTuning`](VocTuning) with:
/// - `index_offset`: 100
/// - `learning_time_offset`: 12h
/// - `learning_time_gain`: 12h
/// - `gating_max_durations`: 180min
/// - `initial_standard_deviation`: 50
/// - `gain_factor`: 230
fn default() -> Self {
Self(Tuning {
index_offset: 100,
learning_time_offset: 12,
learning_time_gain: 12,
gating_max_durations: 180,
initial_standard_deviation: 50,
gain_factor: 230,
})
}
}
/// Configuration for the NOx Index algorithm.
#[derive(Debug, PartialEq)]
pub struct NoxTuning(Tuning);
impl NoxTuning {
/// Creates a new [`NoxTuning`](NoxTuning) Index configuration:
/// - `index_offset`: NOx Index representing typical conditions. Range 1 - 250, Default: 1.
/// - `learning_time_offset`: Time constant to estimate the offset from the history in hours.
/// After twice the learning time events are forgotten. Range 1 - 1,000h, Default 12h.
/// - `learning_time_gain`: Time constant to estimate the gain from the history in hours.
/// After twice the learning time events are forgotten. Range 1 - 1,000h, Default 12h.
/// - `gating_max_durations`: Maximum duration the estimator freezes on a high NOx index
/// signal. Zero disables the gating. Range 0 - 3,000min, Default: 720min.
/// - `gain_factor`: Factor to amplify/attunate the NOx index output. Range 1 - 1,000, Default:
/// 230.
///
/// # Errors
///
/// - [`ValueOutOfRange`](crate::error::DataError::ValueOutOfRange)`: If the values with scaling
/// are not in range.
pub fn new(
index_offset: i16,
learning_time_offset: i16,
learning_time_gain: i16,
gating_max_durations: i16,
gain_factor: i16,
) -> Result<Self, DataError> {
Ok(Self(Tuning::new(
index_offset,
learning_time_offset,
learning_time_gain,
gating_max_durations,
50,
gain_factor,
)?))
}
}
impl From<NoxTuning> for [u16; 6] {
fn from(value: NoxTuning) -> Self {
<[u16; 6]>::from(value.0)
}
}
impl TryFrom<&[u8]> for NoxTuning {
type Error = DataError;
/// Parse the NOx tuning parameters from the received data.
///
/// # Errors
///
/// - [`CrcFailed`](crate::error::DataError::CrcFailed): If the received data CRC indicates
/// corruption.
/// - [`ReceivedBufferWrongSize`](crate::error::DataError::ReceivedBufferWrongSize): If the
/// received data buffer is not the expected size.
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
Ok(NoxTuning(Tuning::try_from(data)?))
}
}
impl Default for NoxTuning {
/// Creates a default [`NoxTuning`](NoxTuning) with:
/// - `index_offset`: 1
/// - `learning_time_offset`: 12h
/// - `learning_time_gain`: 12h
/// - `gating_max_durations`: 720min
/// - `initial_standard_deviation`: 50
/// - `gain_factor`: 230
fn default() -> Self {
Self(Tuning {
index_offset: 100,
learning_time_offset: 12,
learning_time_gain: 12,
gating_max_durations: 180,
initial_standard_deviation: 50,
gain_factor: 230,
})
}
}
#[derive(Debug, PartialEq)]
struct Tuning {
index_offset: i16,
learning_time_offset: i16,
learning_time_gain: i16,
gating_max_durations: i16,
initial_standard_deviation: i16,
gain_factor: i16,
}
impl Tuning {
fn new(
index_offset: i16,
learning_time_offset: i16,
learning_time_gain: i16,
gating_max_durations: i16,
initial_standard_deviation: i16,
gain_factor: i16,
) -> Result<Self, DataError> {
Ok(Self {
index_offset: check_range(index_offset, 1, 250, "VOC Index Offset", "")?,
learning_time_offset: check_range(
learning_time_offset,
1,
1_000,
"VOC Learning Time Offset",
"h",
)?,
learning_time_gain: check_range(
learning_time_gain,
1,
1_000,
"VOC Learning Time Gain",
"h",
)?,
gating_max_durations: check_range(
gating_max_durations,
0,
3_000,
"VOC Gating Max Duration",
"min",
)?,
initial_standard_deviation: check_range(
initial_standard_deviation,
10,
5_000,
"VOC Initial Standard Deviation",
"",
)?,
gain_factor: check_range(gain_factor, 1, 1_000, "VOC Gain Factor", "")?,
})
}
}
impl From<Tuning> for [u16; 6] {
fn from(value: Tuning) -> Self {
[
value.index_offset as u16,
value.learning_time_offset as u16,
value.learning_time_gain as u16,
value.gating_max_durations as u16,
value.initial_standard_deviation as u16,
value.gain_factor as u16,
]
}
}
impl TryFrom<&[u8]> for Tuning {
type Error = DataError;
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
check_deserialization(data, 18)?;
Tuning::new(
i16::from_be_bytes([data[0], data[1]]),
i16::from_be_bytes([data[3], data[4]]),
i16::from_be_bytes([data[6], data[7]]),
i16::from_be_bytes([data[9], data[10]]),
i16::from_be_bytes([data[12], data[13]]),
i16::from_be_bytes([data[15], data[16]]),
)
}
}