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
251
252
253
254
use std::collections::{HashMap, HashSet};
use std::io::{Error, ErrorKind};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use super::Result;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DhtDataRaw {
pub t: f32,
pub h: f32,
pub hi: f32,
}
#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct SensorData {
pub temperature: f32,
pub humidity: f32,
pub heat_index: f32,
}
impl From<DhtDataRaw> for SensorData {
fn from(data: DhtDataRaw) -> Self {
SensorData {
temperature: data.t,
humidity: data.h,
heat_index: data.hi,
}
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct DhtSensors {
pub timestamp: DateTime<Utc>,
pub data: HashMap<String, SensorData>,
}
impl DhtSensors {
pub fn from_serde(data: DhtSensorsSerde) -> Result<DhtSensors> {
let mut lengths = HashSet::new();
lengths.insert(data.o.len());
lengths.insert(data.t.len());
lengths.insert(data.h.len());
lengths.insert(data.hi.len());
if lengths.len() != 1 {
return Err(Error::new(
ErrorKind::InvalidData,
"length mismatch in serde data",
));
}
let mut sensor_data = HashMap::new();
for (i, key) in data.o.iter().enumerate() {
sensor_data.insert(
key.clone(),
SensorData {
temperature: data.t[i],
humidity: data.h[i],
heat_index: data.hi[i],
},
);
}
Ok(DhtSensors {
timestamp: data.ts,
data: sensor_data,
})
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct DhtSensorsSerde {
pub ts: DateTime<Utc>,
pub o: Vec<String>,
pub t: Vec<f32>,
pub h: Vec<f32>,
pub hi: Vec<f32>,
}
impl From<DhtSensors> for DhtSensorsSerde {
fn from(data: DhtSensors) -> DhtSensorsSerde {
let timestamp = data.timestamp;
let mut order = Vec::new();
let mut temperature = Vec::new();
let mut humidity = Vec::new();
let mut heat_index = Vec::new();
for (key, value) in data.data.iter() {
order.push(key.clone());
temperature.push(value.temperature);
humidity.push(value.humidity);
heat_index.push(value.heat_index);
}
DhtSensorsSerde {
ts: timestamp,
o: order,
t: temperature,
h: humidity,
hi: heat_index,
}
}
}
union DhtDataUnion<'a> {
error: &'a str,
data: SensorData,
}
pub struct Measurement<'a> {
error: bool,
data: DhtDataUnion<'a>,
}
impl<'a> Measurement<'a> {
pub fn new(data: Option<SensorData>, error: Option<&'a str>) -> Measurement {
if (data.is_some() && error.is_some()) || (data.is_none() && error.is_none()) {
panic!("Exactly one of data or error must be a Some type.");
}
if let Some(data) = data {
return Measurement {
error: false,
data: DhtDataUnion { data },
};
}
if let Some(error) = error {
return Measurement {
error: true,
data: DhtDataUnion { error },
};
}
Measurement {
error: true,
data: DhtDataUnion {
error: "initialization error",
},
}
}
pub fn get_data(&self) -> Option<SensorData> {
if self.has_data() {
unsafe { Some(self.data.data) }
} else {
None
}
}
pub fn get_error(&self) -> Option<&'a str> {
if self.has_error() {
unsafe { Some(self.data.error) }
} else {
None
}
}
pub fn has_data(&self) -> bool {
!self.error
}
pub fn has_error(&self) -> bool {
self.error
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn test_measurement_new_both_none() {
Measurement::new(None, None);
}
#[test]
#[should_panic]
fn test_measurement_new_both_some() {
let error = "test";
let data = SensorData {
temperature: 0.0,
humidity: 0.0,
heat_index: 0.0,
};
Measurement::new(Some(data), Some(error));
}
#[test]
fn test_convert_from_raw() {
let raw = DhtDataRaw {
t: 21.3,
h: 52.7,
hi: 22.8,
};
let data = SensorData::from(raw.clone());
assert_eq!(raw.t, data.temperature);
assert_eq!(raw.h, data.humidity);
assert_eq!(raw.hi, data.heat_index);
}
}