|
| 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 | +} |
0 commit comments