Skip to content

Commit e7bed87

Browse files
committed
Add co2 sensor
1 parent bce8b14 commit e7bed87

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/sensors/carbondioxide.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//! Module providing carbondioxide sensor functionality.
2+
3+
use super::{FromSensorTemplate, SensorMetadataWithLocation, SensorTemplate, SensorTemplateError, Sensors};
4+
use serde::{Deserialize, Serialize};
5+
6+
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
7+
pub struct CarbondioxideSensor {
8+
#[serde(flatten)]
9+
pub metadata: SensorMetadataWithLocation,
10+
pub unit: String,
11+
pub value: u64,
12+
}
13+
14+
#[derive(Debug, Clone)]
15+
pub struct CarbondioxideSensorTemplate {
16+
pub metadata: SensorMetadataWithLocation,
17+
pub unit: String,
18+
}
19+
20+
impl FromSensorTemplate<CarbondioxideSensorTemplate> for CarbondioxideSensor {
21+
fn try_from_template(
22+
template: &CarbondioxideSensorTemplate,
23+
value: &str,
24+
) -> Result<Self, SensorTemplateError> {
25+
Ok(Self {
26+
metadata: template.metadata.clone(),
27+
unit: template.unit.clone(),
28+
value: value.parse()?,
29+
})
30+
}
31+
}
32+
33+
impl SensorTemplate for CarbondioxideSensorTemplate {
34+
fn try_to_sensor(&self, value_str: &str, sensors: &mut Sensors) -> Result<(), SensorTemplateError> {
35+
sensors
36+
.carbondioxide
37+
.push(CarbondioxideSensor::try_from_template(self, value_str)?);
38+
Ok(())
39+
}
40+
}
41+
42+
#[cfg(test)]
43+
mod test {
44+
use super::*;
45+
46+
#[test]
47+
fn test_template() {
48+
let template = CarbondioxideSensorTemplate {
49+
metadata: SensorMetadataWithLocation {
50+
location: "Main Room".into(),
51+
description: Some("Centre of main room on ground floor".into()),
52+
..Default::default()
53+
},
54+
unit: "ppm".into(),
55+
};
56+
57+
let mut sensors = Sensors::default();
58+
template.to_sensor("1234", &mut sensors);
59+
60+
assert_eq!(
61+
"[{\"location\":\"Main Room\",\"description\":\"Centre of main room on ground floor\",\"unit\":\"ppm\",\"value\":1234}]",
62+
serde_json::to_string(&sensors.carbondioxide).unwrap()
63+
);
64+
}
65+
66+
#[test]
67+
fn test_template_bad_float() {
68+
let template = CarbondioxideSensorTemplate {
69+
metadata: SensorMetadataWithLocation {
70+
location: "Main Room".into(),
71+
description: Some("Centre of main room on ground floor".into()),
72+
..Default::default()
73+
},
74+
unit: "ppm".into(),
75+
};
76+
77+
let mut sensors = Sensors::default();
78+
let result = template.try_to_sensor("one thousand two hundred thirty four", &mut sensors);
79+
80+
assert!(result.is_err());
81+
assert_eq!(
82+
"sensor integer value cannot be parsed",
83+
result.err().unwrap().to_string()
84+
);
85+
}
86+
}

src/sensors/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod radiation;
1313
mod temperature;
1414
mod total_member_count;
1515
mod wind;
16+
mod carbondioxide;
1617

1718
pub use account_balance::{AccountBalanceSensor, AccountBalanceSensorTemplate};
1819
pub use barometer::{BarometerSensor, BarometerSensorTemplate};
@@ -33,6 +34,7 @@ pub use radiation::{RadiationSensor, RadiationSensorUnit, RadiationSensors};
3334
pub use temperature::{TemperatureSensor, TemperatureSensorTemplate};
3435
pub use total_member_count::{TotalMemberCountSensor, TotalMemberCountSensorTemplate};
3536
pub use wind::{WindSensor, WindSensorMeasurement, WindSensorProperties};
37+
pub use carbondioxide::{CarbondioxideSensor, CarbondioxideSensorTemplate};
3638

3739
use log::warn;
3840
use serde::{Deserialize, Serialize};
@@ -125,6 +127,8 @@ pub struct Sensors {
125127
pub people_now_present: Vec<PeopleNowPresentSensor>,
126128
#[serde(default, skip_serializing_if = "Vec::is_empty")]
127129
pub network_traffic: Vec<NetworkTrafficSensor>,
130+
#[serde(default, skip_serializing_if = "Vec::is_empty")]
131+
pub carbondioxide: Vec<CarbondioxideSensor>,
128132
}
129133

130134
#[cfg(test)]

0 commit comments

Comments
 (0)