Skip to content

v15: Add power generation sensor #126

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
4 changes: 4 additions & 0 deletions src/sensors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod network_connections;
mod network_traffic;
mod people_now_present;
mod power_consumption;
mod power_generation;
mod radiation;
mod temperature;
mod total_member_count;
Expand All @@ -31,6 +32,7 @@ pub use network_traffic::{
};
pub use people_now_present::{PeopleNowPresentSensor, PeopleNowPresentSensorTemplate};
pub use power_consumption::{PowerConsumptionSensor, PowerConsumptionSensorTemplate};
pub use power_generation::{PowerGenerationSensor, PowerGenerationSensorTemplate};
pub use radiation::{RadiationSensor, RadiationSensorUnit, RadiationSensors};
pub use temperature::{TemperatureSensor, TemperatureSensorTemplate};
pub use total_member_count::{TotalMemberCountSensor, TotalMemberCountSensorTemplate};
Expand Down Expand Up @@ -120,6 +122,8 @@ pub struct Sensors {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub power_consumption: Vec<PowerConsumptionSensor>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub power_generation: Vec<PowerGenerationSensor>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub wind: Vec<WindSensor>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub network_connections: Vec<NetworkConnectionsSensor>,
Expand Down
40 changes: 40 additions & 0 deletions src/sensors/power_generation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! Module providing power generation sensor functionality.

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

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

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

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

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