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
//! Serializable messages representing DHT sensor data.

use std::collections::{HashMap, HashSet};
use std::io::{Error, ErrorKind};

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use super::Result;

/// Serde JSON from the DHT sensor over serial.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DhtDataRaw {
    pub t: f32,
    pub h: f32,
    pub hi: f32,
}

/// A single reading for a DHT sensor.
#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct SensorData {
    pub temperature: f32,
    pub humidity: f32,
    pub heat_index: f32,
}

/// Convert the RAW Json to SensorData so it can be re-serialized with full field names.
impl From<DhtDataRaw> for SensorData {
    fn from(data: DhtDataRaw) -> Self {
        SensorData {
            temperature: data.t,
            humidity: data.h,
            heat_index: data.hi,
        }
    }
}

/// Container of measurements from all DHT sensors in one reading.
///
/// The JSON serialization is not compact. For smaller JSON messages, use `DhtSensorsSerde`.
#[derive(Debug, Deserialize, Serialize)]
pub struct DhtSensors {
    pub timestamp: DateTime<Utc>,
    pub data: HashMap<String, SensorData>,
}

impl DhtSensors {
    /// Decode a `DntSensorsSerde` struct into DhtSensors.
    ///
    /// If not all hashmaps in DhtSensorsPacked have
    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,
        })
    }
}

/// A more compactly serialized verson of DhtSensors for serializing via JSON
///
/// This is not intended on being human-readable. For human-readability, use `DhtSensors` instead.
#[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,
}

/// Data container for a DHT sensor measurement that contains either an error or data.
/// ```
/// use dht_logger::{Measurement, SensorData};
/// // Example test data
/// let error = "test";
/// let data = SensorData {
///     temperature: 0.0,
///     humidity: 0.0,
///     heat_index: 0.0,
/// };
///
/// // Create a measurement containing an error
/// let measurement = Measurement::new(None, Some(error));
/// assert!(measurement.get_data().is_none());
/// assert!(measurement.get_error().is_some());
/// assert_eq!(measurement.get_error().unwrap(), error);
///
/// // Create a measurement containing data
/// let measurement = Measurement::new(Some(data), None);
/// assert!(measurement.get_data().is_some());
/// assert!(measurement.get_error().is_none());
/// assert_eq!(measurement.get_data().unwrap(), data);
/// ```
pub struct Measurement<'a> {
    error: bool,
    data: DhtDataUnion<'a>,
}

impl<'a> Measurement<'a> {
    /// Create a new measurement of a DHT sensor.
    ///
    /// Args:
    /// * `data`: Sensor data from one DHT sensor.
    /// * `error`: Error indicating a failure to read a DHT sensor.
    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 },
            };
        }

        // This should never happen
        Measurement {
            error: true,
            data: DhtDataUnion {
                error: "initialization error",
            },
        }
    }

    /// Get the data contained by the measurement, if it exists.
    pub fn get_data(&self) -> Option<SensorData> {
        if self.has_data() {
            unsafe { Some(self.data.data) }
        } else {
            None
        }
    }

    /// Get the error contained by the measurement, if it exists.
    pub fn get_error(&self) -> Option<&'a str> {
        if self.has_error() {
            unsafe { Some(self.data.error) }
        } else {
            None
        }
    }

    /// Check if the measurement has data.
    pub fn has_data(&self) -> bool {
        !self.error
    }

    /// Check if the measurement has an error.
    pub fn has_error(&self) -> bool {
        self.error
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    // Test that Measurement panics when giving None twice
    #[test]
    #[should_panic]
    fn test_measurement_new_both_none() {
        Measurement::new(None, None);
    }

    // Test that Measurement panics when giving Some twice
    #[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 that SensorData can be converted from a DhtDataRaw
    #[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);
    }
}