Skip to content

Commit

Permalink
Added log and env_logger crates
Browse files Browse the repository at this point in the history
  • Loading branch information
eko committed Feb 27, 2020
1 parent 6de8d59 commit 53f44cd
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 15 deletions.
89 changes: 89 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ reqwest = { version = "0.10.2", features = ["json"] }
serde = "1.0.104"
serde_derive = "1.0.104"
serde_json = "1.0.48"
ticker = "0.1.1"
ticker = "0.1.1"
log = "0.4.8"
env_logger = "0.7.1"
15 changes: 9 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[macro_use]
extern crate log;

#[macro_use]
extern crate prometheus;

Expand All @@ -9,6 +12,7 @@ extern crate serde_json;
mod config;
mod tado;

use env_logger::Env;
use tokio;
use std::convert::Infallible;
use std::time::Duration;
Expand All @@ -21,14 +25,16 @@ use tado::client::Client as TadoClient;

#[tokio::main]
async fn main() {
env_logger::from_env(Env::default().default_filter_or("info")).init();

let config = config_loader::load();

// Start ticker
run_ticker(config);

// set up http server
let addr = ([0, 0, 0, 0], 9898).into();
println!("starting tado° exporter on address: {:?}", addr);
info!("starting tado° exporter on address: {:?}", addr);

let make_svc = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(renderer))
Expand All @@ -38,18 +44,15 @@ async fn main() {

// start HTTP server
if let Err(e) = server.await {
eprintln!("a server error occured: {}", e);
error!("a server error occured: {}", e);
}
}

fn run_ticker(config: config_loader::Config) {
tokio::spawn(async move {
let mut tado_client = TadoClient::new(config.username, config.password, config.client_secret);

let ticker = Ticker::new(0.., Duration::from_secs(config.ticker));
for i in ticker {
println!("{:?}", i);

for _ in ticker {
tado_client.retrieve().await;
}
});
Expand Down
44 changes: 36 additions & 8 deletions src/tado/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate reqwest;

use log::{info, error};
use super::metrics::{TEMPERATURE_GAUGE, HUMIDITY_PERCENTAGE};
use super::model::{AuthApiResponse, MeApiResponse, ZonesApiResponse, ZoneStateApiResponse};

Expand Down Expand Up @@ -82,35 +83,62 @@ impl Client {

pub async fn retrieve(&mut self) {
// retrieve an access token to use the tado API
let api_response = self.authenticate().await.unwrap();
let api_response = match self.authenticate().await {
Ok(resp) => resp,
Err(e) => {
error!("[tado° client] unable to authenticate: {}", e);
return;
}
};

self.access_token = api_response.access_token;

// retrieve home details (only if we don't already have a home identifier)
if self.home_id == 0 {
let me_response = self.me().await.unwrap();
let me_response = match self.me().await {
Ok(resp) => resp,
Err(e) => {
error!("[tado° client] unable to retrieve home identifier: {}", e);
return;
}
};

self.home_id = me_response.homes.first().unwrap().id;
}

// retrieve home different zones
let zones_response = self.zones().await.unwrap();
let zones_response = match self.zones().await {
Ok(resp) => resp,
Err(e) => {
error!("[tado° client] unable to retrieve home zones: {}", e);
return;
}
};

for zone in zones_response {
println!("[tado° client] retrieving zone details for {}...", zone.name);
let zone_state_response = self.zone_state(zone.id).await.unwrap();
info!("[tado° client] retrieving zone details for {}...", zone.name);
let zone_state_response = match self.zone_state(zone.id).await {
Ok(resp) => resp,
Err(e) => {
error!("[tado° client] unable to retrieve home zone '{}' state: {}", zone.name, e);
return;
}
};

// temperature: celsius
let temperature_celsius: f64 = zone_state_response.sensorDataPoints.insideTemperature.celsius;
TEMPERATURE_GAUGE.with_label_values(&[zone.name.as_str(), "celsius"]).set(temperature_celsius);
println!("-> temperature (celsius): {}", temperature_celsius);
info!("-> temperature (celsius): {}", temperature_celsius);

// temperature: fahrenheit
let temperature_fahrenheit: f64 = zone_state_response.sensorDataPoints.insideTemperature.fahrenheit;
TEMPERATURE_GAUGE.with_label_values(&[zone.name.as_str(), "fahrenheit"]).set(temperature_celsius);
println!("-> temperature (fahrenheit): {}", temperature_fahrenheit);
info!("-> temperature (fahrenheit): {}", temperature_fahrenheit);

// humidity percentage
let humidity_percentage: f64 = zone_state_response.sensorDataPoints.humidity.percentage;
HUMIDITY_PERCENTAGE.with_label_values(&[zone.name.as_str()]).set(humidity_percentage);
println!("-> humidity: {}%", humidity_percentage);
info!("-> humidity: {}%", humidity_percentage);
}
}
}

0 comments on commit 53f44cd

Please sign in to comment.