Skip to content

Commit

Permalink
Run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Weiden committed Feb 15, 2024
1 parent 8e5a2dc commit 840b79a
Showing 1 changed file with 103 additions and 31 deletions.
134 changes: 103 additions & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use std::env;
use std::time::{Duration, SystemTime};

use prometheus_exporter::{
self,
prometheus::register_gauge,
prometheus::register_gauge_vec,
};
use prometheus_exporter::{self, prometheus::register_gauge, prometheus::register_gauge_vec};
use reqwest::Client;
use serde::Deserialize;
use serde_json::from_str;
Expand Down Expand Up @@ -54,7 +50,6 @@ struct ShellyplugResponse {
sys: Sys,
}


async fn get_data(client: &Client, url: &String) -> Result<ShellyplugResponse, reqwest::Error> {
let response = client.get(url).send().await?;
let json = response.json::<ShellyplugResponse>().await?;
Expand All @@ -63,53 +58,130 @@ async fn get_data(client: &Client, url: &String) -> Result<ShellyplugResponse, r

#[tokio::main]
async fn main() {
let port = env::var("PORT").or::<String>(Ok("9185".to_string())).unwrap();
let period = Duration::from_secs(from_str::<u64>(&env::var("PERIOD").or::<String>(Ok("60".to_string())).unwrap()).unwrap());
let port = env::var("PORT")
.or::<String>(Ok("9185".to_string()))
.unwrap();
let period = Duration::from_secs(
from_str::<u64>(
&env::var("PERIOD")
.or::<String>(Ok("60".to_string()))
.unwrap(),
)
.unwrap(),
);
let binding = format!("0.0.0.0:{}", port).parse().unwrap();
println!("Listening on {}", binding);
println!("Updating every {:?}", period);
let exporter = prometheus_exporter::start(binding).unwrap();

let client = Client::new();
let url = format!("{}/rpc/Shelly.GetStatus", env::var("SHELLYPLUG_URL").expect("SHELLYPLUG_URL not set"));
let url = format!(
"{}/rpc/Shelly.GetStatus",
env::var("SHELLYPLUG_URL").expect("SHELLYPLUG_URL not set")
);

let a_power = register_gauge_vec!("shellyplug_apower", "Instantaneous power in W", &["mac"]).unwrap();
let voltage_shelly = register_gauge_vec!("shellyplug_voltage", "Voltage in V", &["mac"]).unwrap();
let current_shelly = register_gauge_vec!("shellyplug_current", "Current in A", &["mac"]).unwrap();
let a_energy_total_shelly = register_gauge_vec!("shellyplug_aenergy_total", "Total energy so far in Wh", &["mac"]).unwrap();
let temperature_shelly = register_gauge_vec!("shellyplug_temperature", "Temperature of Shellyplug in °C", &["mac"]).unwrap();
let output_shelly = register_gauge_vec!("shellyplug_output", "true if output channel is currently on, false otherwise", &["mac"]).unwrap();
let available_updates_shelly = register_gauge_vec!("shellyplug_available_updates_info", "Information about available updates", &["mac", "version"]).unwrap();
let last_updated_shelly = register_gauge_vec!("shellyplug_last_updated", "Last update of Shellyplug", &["mac"]).unwrap();
let process_start_time = register_gauge!("process_start_time_seconds", "Start time of the process").unwrap();
let a_power =
register_gauge_vec!("shellyplug_apower", "Instantaneous power in W", &["mac"]).unwrap();
let voltage_shelly =
register_gauge_vec!("shellyplug_voltage", "Voltage in V", &["mac"]).unwrap();
let current_shelly =
register_gauge_vec!("shellyplug_current", "Current in A", &["mac"]).unwrap();
let a_energy_total_shelly = register_gauge_vec!(
"shellyplug_aenergy_total",
"Total energy so far in Wh",
&["mac"]
)
.unwrap();
let temperature_shelly = register_gauge_vec!(
"shellyplug_temperature",
"Temperature of Shellyplug in °C",
&["mac"]
)
.unwrap();
let output_shelly = register_gauge_vec!(
"shellyplug_output",
"true if output channel is currently on, false otherwise",
&["mac"]
)
.unwrap();
let available_updates_shelly = register_gauge_vec!(
"shellyplug_available_updates_info",
"Information about available updates",
&["mac", "version"]
)
.unwrap();
let last_updated_shelly = register_gauge_vec!(
"shellyplug_last_updated",
"Last update of Shellyplug",
&["mac"]
)
.unwrap();
let process_start_time =
register_gauge!("process_start_time_seconds", "Start time of the process").unwrap();

let mut now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
let mut now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
process_start_time.set(now as f64);

loop {
match get_data(&client, &url).await {
Ok(data) => {
// println!("{:?}", data);
now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
let mac = data.sys.mac;
a_power.get_metric_with_label_values(&[&mac]).unwrap().set(data.switch0.apower);
voltage_shelly.get_metric_with_label_values(&[&mac]).unwrap().set(data.switch0.voltage);
current_shelly.get_metric_with_label_values(&[&mac]).unwrap().set(data.switch0.current);
a_energy_total_shelly.get_metric_with_label_values(&[&mac]).unwrap().set(data.switch0.aenergy.total);
temperature_shelly.get_metric_with_label_values(&[&mac]).unwrap().set(data.switch0.temperature.t_c);
a_power
.get_metric_with_label_values(&[&mac])
.unwrap()
.set(data.switch0.apower);
voltage_shelly
.get_metric_with_label_values(&[&mac])
.unwrap()
.set(data.switch0.voltage);
current_shelly
.get_metric_with_label_values(&[&mac])
.unwrap()
.set(data.switch0.current);
a_energy_total_shelly
.get_metric_with_label_values(&[&mac])
.unwrap()
.set(data.switch0.aenergy.total);
temperature_shelly
.get_metric_with_label_values(&[&mac])
.unwrap()
.set(data.switch0.temperature.t_c);
if data.switch0.output {
output_shelly.get_metric_with_label_values(&[&mac]).unwrap().set(1.);
output_shelly
.get_metric_with_label_values(&[&mac])
.unwrap()
.set(1.);
} else {
output_shelly.get_metric_with_label_values(&[&mac]).unwrap().set(0.);
output_shelly
.get_metric_with_label_values(&[&mac])
.unwrap()
.set(0.);
}
available_updates_shelly.reset();
match data.sys.available_updates.stable {
Some(v) => available_updates_shelly.get_metric_with_label_values(&[&mac, &v.version]).unwrap().set(1.),
None => available_updates_shelly.get_metric_with_label_values(&[&mac, "current"]).unwrap().set(1.)
Some(v) => available_updates_shelly
.get_metric_with_label_values(&[&mac, &v.version])
.unwrap()
.set(1.),
None => available_updates_shelly
.get_metric_with_label_values(&[&mac, "current"])
.unwrap()
.set(1.),
}
last_updated_shelly.get_metric_with_label_values(&[&mac]).unwrap().set(now as f64);
last_updated_shelly
.get_metric_with_label_values(&[&mac])
.unwrap()
.set(now as f64);
}
Err(err) => eprintln!("{}", err)
Err(err) => eprintln!("{}", err),
}
let _guard = exporter.wait_duration(period);
}
Expand Down

0 comments on commit 840b79a

Please sign in to comment.