Skip to content

v15: Add co2 sensor #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/sensors/carbondioxide.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! Module providing carbondioxide sensor functionality.

use super::{FromSensorTemplate, SensorMetadataWithLocation, SensorTemplate, SensorTemplateError, Sensors};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
pub struct CarbondioxideSensor {
#[serde(flatten)]
pub metadata: SensorMetadataWithLocation,
pub unit: String,
pub value: u64,
}

#[derive(Debug, Clone)]
pub struct CarbondioxideSensorTemplate {
pub metadata: SensorMetadataWithLocation,
pub unit: String,
}

impl FromSensorTemplate<CarbondioxideSensorTemplate> for CarbondioxideSensor {
fn try_from_template(
template: &CarbondioxideSensorTemplate,
value: &str,
) -> Result<Self, SensorTemplateError> {
Ok(Self {
metadata: template.metadata.clone(),
unit: template.unit.clone(),
value: value.parse()?,
})
}
}

impl SensorTemplate for CarbondioxideSensorTemplate {
fn try_to_sensor(&self, value_str: &str, sensors: &mut Sensors) -> Result<(), SensorTemplateError> {
sensors
.carbondioxide
.push(CarbondioxideSensor::try_from_template(self, value_str)?);
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_template() {
let template = CarbondioxideSensorTemplate {
metadata: SensorMetadataWithLocation {
location: "Main Room".into(),
description: Some("Centre of main room on ground floor".into()),
..Default::default()
},
unit: "ppm".into(),
};

let mut sensors = Sensors::default();
template.to_sensor("1234", &mut sensors);

assert_eq!(
"[{\"location\":\"Main Room\",\"description\":\"Centre of main room on ground floor\",\"unit\":\"ppm\",\"value\":1234}]",
serde_json::to_string(&sensors.carbondioxide).unwrap()
);
}

#[test]
fn test_template_bad_float() {
let template = CarbondioxideSensorTemplate {
metadata: SensorMetadataWithLocation {
location: "Main Room".into(),
description: Some("Centre of main room on ground floor".into()),
..Default::default()
},
unit: "ppm".into(),
};

let mut sensors = Sensors::default();
let result = template.try_to_sensor("one thousand two hundred thirty four", &mut sensors);

assert!(result.is_err());
assert_eq!(
"sensor integer value cannot be parsed",
result.err().unwrap().to_string()
);
}
}
4 changes: 4 additions & 0 deletions src/sensors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod account_balance;
mod barometer;
mod beverage_supply;
mod carbondioxide;
mod door_locked;
mod humidity;
mod network_connections;
Expand All @@ -17,6 +18,7 @@ mod wind;
pub use account_balance::{AccountBalanceSensor, AccountBalanceSensorTemplate};
pub use barometer::{BarometerSensor, BarometerSensorTemplate};
pub use beverage_supply::{BeverageSupplySensor, BeverageSupplySensorTemplate};
pub use carbondioxide::{CarbondioxideSensor, CarbondioxideSensorTemplate};
pub use door_locked::{DoorLockedSensor, DoorLockedSensorTemplate};
pub use humidity::{HumiditySensor, HumiditySensorTemplate};
pub use network_connections::{
Expand Down Expand Up @@ -125,6 +127,8 @@ pub struct Sensors {
pub people_now_present: Vec<PeopleNowPresentSensor>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub network_traffic: Vec<NetworkTrafficSensor>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub carbondioxide: Vec<CarbondioxideSensor>,
}

#[cfg(test)]
Expand Down