Skip to content

Commit

Permalink
Cleanup configuration of sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
koarlchen committed Oct 6, 2024
1 parent 0182f08 commit 8ca02e0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 38 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ The configuration takes place within the file `config.json`:
},
"sensors": {
"door": {
"name": "", // Name/ID of sensor within database
"entity": "", // Entity ID of sensor within database
"unit": "state", // Unit of measurement within database
"validity": 0 // Period of validity in seconds, set to 0 to disable check
},
"temperature": {
"name": [ // List of sensors
"id": [ // List of sensors
{
"id": "", // Name/ID of sensor within database
"location": "" // Location of sensor
"entity": "", // Entity ID of sensor within database
"location": "" // Human readable location of sensor
}
],
"unit": "°C", // Unit of measurements within database
"validity": 0 // Period of validity in seconds, set to 0 to disable check
},
"humidity": {
"name": [ // List of sensors
"id": [ // List of sensors
{
"id": "", // Name/ID of sensor within database
"location": "" // Location of sensor
"entity": "", // Entity ID of sensor within database
"location": "" // Human readable location of sensor
}
],
"unit": "%", // Unit of measurement within database
Expand Down
32 changes: 19 additions & 13 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
{
"database": {
"connection": "http://localhost:8086",
"database": "...",
"username": "...",
"password": "..."
"connection": "",
"database": "",
"username": "",
"password": ""
},
"server": {
"hostname": "localhost",
"port": 3000
},
"sensors": {
"door": {
"name": "",
"entity": "",
"unit": "state",
"validity": 0
},
"temperature": {
"name": [
{ "id": "", "location": "" }
"id": [
{
"entity": "",
"location": ""
}
],
"unit": "°C",
"validity": 7200
"validity": 0
},
"humidity": {
"name": [
{ "id": "", "location": "" }
"id": [
{
"entity": "",
"location": ""
}
],
"unit": "%",
"validity": 7200
"validity": 0
}
},
"cache_time": {
"status.json": 15,
"health": 15
"status.json": 0,
"health": 0
}
}
22 changes: 15 additions & 7 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,30 @@ pub struct Server {

#[derive(Deserialize, Clone)]
pub struct Sensors {
pub door: SensorDescription<String>,
pub temperature: SensorDescription<Vec<SensorName>>,
pub humidity: SensorDescription<Vec<SensorName>>,
pub door: DoorSettings,
pub temperature: SensorSettings,
pub humidity: SensorSettings,
}

#[derive(Deserialize, Clone)]
pub struct SensorDescription<T> {
pub name: T,
pub struct DoorSettings {
pub entity: String,
pub unit: String,
#[serde(deserialize_with = "parse_timedelta")]
pub validity: chrono::TimeDelta,
}

#[derive(Deserialize, Clone)]
pub struct SensorName {
pub id: String,
pub struct SensorSettings {
pub id: Vec<SensorIdentification>,
pub unit: String,
#[serde(deserialize_with = "parse_timedelta")]
pub validity: chrono::TimeDelta,
}

#[derive(Deserialize, Clone)]
pub struct SensorIdentification {
pub entity: String,
pub location: String,
}

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ pub enum StatusError {
#[error("Failed to interact with database: {0}")]
Database(String),

#[error("Failed to parse template file: {0}")]
#[error("Invalid template: {0}")]
Template(String),

#[error("Failed to parse configuration file: {0}")]
#[error("Invalid configuration: {0}")]
Configuration(String),

#[error("Failed to start server: {0}")]
Expand Down
18 changes: 9 additions & 9 deletions src/server/routes/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ async fn get_temperature(

let mut sensors: Vec<sensors::TemperatureSensor> = Vec::new();

for sensor in config.temperature.name.iter() {
for sensor in config.temperature.id.iter() {
match database
.get_temperature(&sensor.id, &config.temperature.unit)
.get_temperature(&sensor.entity, &config.temperature.unit)
.await
{
Ok(temp) => match validate_time(&temp, config.temperature.validity) {
Expand All @@ -100,15 +100,15 @@ async fn get_temperature(
}),
None => log::warn!(
"Latest sensor measurement too old: id='{}' unit='{}' sample_time='{}' validity='{}'",
sensor.id,
sensor.entity,
config.temperature.unit,
temp.time,
config.temperature.validity
),
},
Err(err) => log::warn!(
"Failed to query temperature: id='{}' unit='{}' ({})",
sensor.id,
sensor.entity,
config.temperature.unit,
err
),
Expand All @@ -126,9 +126,9 @@ async fn get_humidity(

let mut sensors: Vec<sensors::HumiditySensor> = Vec::new();

for sensor in config.humidity.name.iter() {
for sensor in config.humidity.id.iter() {
match database
.get_humidity(&sensor.id, &config.humidity.unit)
.get_humidity(&sensor.entity, &config.humidity.unit)
.await
{
Ok(temp) => match validate_time(&temp, config.humidity.validity) {
Expand All @@ -142,15 +142,15 @@ async fn get_humidity(
}),
None => log::warn!(
"Latest sensor measurement too old: id='{}' unit='{}' sample_time='{}' validity='{}'",
sensor.id,
sensor.entity,
config.humidity.unit,
temp.time,
config.humidity.validity
),
},
Err(err) => log::warn!(
"Failed to query humidity: id='{}' unit='{}' ({})",
sensor.id,
sensor.entity,
config.humidity.unit,
err
),
Expand All @@ -167,7 +167,7 @@ async fn get_door(
log::debug!("Query door status");

match database
.get_door_status(&config.door.name, &config.door.unit)
.get_door_status(&config.door.entity, &config.door.unit)
.await
{
Ok(door) => validate_time(&door, config.door.validity).map(|val| spaceapi::State {
Expand Down

0 comments on commit 8ca02e0

Please sign in to comment.