From 38c742e46f53928bcbcd5080f653f5a5d6355f7c Mon Sep 17 00:00:00 2001 From: max Date: Sat, 21 Dec 2024 11:09:49 +0100 Subject: [PATCH 1/2] Add co2 sensor --- src/sensors/carbondioxide.rs | 86 ++++++++++++++++++++++++++++++++++++ src/sensors/mod.rs | 4 ++ 2 files changed, 90 insertions(+) create mode 100644 src/sensors/carbondioxide.rs diff --git a/src/sensors/carbondioxide.rs b/src/sensors/carbondioxide.rs new file mode 100644 index 0000000..4309db1 --- /dev/null +++ b/src/sensors/carbondioxide.rs @@ -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 for CarbondioxideSensor { + fn try_from_template( + template: &CarbondioxideSensorTemplate, + value: &str, + ) -> Result { + 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() + ); + } +} diff --git a/src/sensors/mod.rs b/src/sensors/mod.rs index 8637b13..cb58d9a 100644 --- a/src/sensors/mod.rs +++ b/src/sensors/mod.rs @@ -3,6 +3,7 @@ mod account_balance; mod barometer; mod beverage_supply; +mod carbondioxide; mod door_locked; mod humidity; mod network_connections; @@ -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::{ @@ -125,6 +127,8 @@ pub struct Sensors { pub people_now_present: Vec, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub network_traffic: Vec, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub carbondioxide: Vec, } #[cfg(test)] From ebccf4622c7cabdde14c60ce946c8ed0daf96c6a Mon Sep 17 00:00:00 2001 From: Raphael Nestler Date: Thu, 1 May 2025 19:53:56 +0200 Subject: [PATCH 2/2] Use raw string for expected JSON --- src/sensors/carbondioxide.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sensors/carbondioxide.rs b/src/sensors/carbondioxide.rs index 4309db1..38b1168 100644 --- a/src/sensors/carbondioxide.rs +++ b/src/sensors/carbondioxide.rs @@ -58,7 +58,7 @@ mod test { template.to_sensor("1234", &mut sensors); assert_eq!( - "[{\"location\":\"Main Room\",\"description\":\"Centre of main room on ground floor\",\"unit\":\"ppm\",\"value\":1234}]", + r#"[{"location":"Main Room","description":"Centre of main room on ground floor","unit":"ppm","value":1234}]"#, serde_json::to_string(&sensors.carbondioxide).unwrap() ); }