diff --git a/.gitignore b/.gitignore index ea8c4bf..8928373 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ +# Rust stuff /target + +# Config +autarco.toml diff --git a/Cargo.lock b/Cargo.lock index 445960f..18d3b42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,6 +71,7 @@ dependencies = [ "rocket_contrib", "serde", "thirtyfour", + "toml", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 56c0e83..c07abcb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" color-eyre = "0.5.6" lazy_static = "1.4.0" serde = "1.0.116" +toml = "0.5.6" [dependencies.rocket] git = "https://github.com/SergioBenitez/Rocket" diff --git a/autarco.toml.example b/autarco.toml.example new file mode 100644 index 0000000..bd96589 --- /dev/null +++ b/autarco.toml.example @@ -0,0 +1,3 @@ +# Put your My Autarco credentials below +username = "foo@domain.tld" +password = "secret" diff --git a/src/main.rs b/src/main.rs index 64ade05..a8194d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,35 @@ use color_eyre::Result; use lazy_static::lazy_static; -use rocket::{get, launch, routes, Rocket}; +use rocket::{get, launch, routes, tokio, Rocket}; use rocket_contrib::json::Json; -use serde::Serialize; +use serde::{Deserialize, Serialize}; +use std::path::Path; use std::process::{Child, Command, Stdio}; use std::sync::Mutex; use std::thread; use std::time::{Duration, SystemTime}; use thirtyfour::prelude::*; +use tokio::fs::File; +use tokio::prelude::*; -const URL: &'static str = "https://my.autarco.com/"; -const USERNAME: &'static str = "pja@vtilburg.net"; -const PASSWORD: &'static str = "XXXXXXXXXXXXXXXX"; -const POLL_INTERVAL: u64 = 300; - +/// The port used by the Gecko Driver const GECKO_DRIVER_PORT: u16 = 18019; +/// The interval between data polls +/// +/// This depends on with which interval Autaurco processes new information from the convertor. +const POLL_INTERVAL: u64 = 300; + +/// The URL to the My Autarco site +const URL: &'static str = "https://my.autarco.com/"; + +#[derive(Debug, Deserialize)] +struct Config { + username: String, + password: String, +} + +#[derive(Debug)] struct GeckoDriver(Child); impl GeckoDriver { @@ -49,13 +63,26 @@ struct Status { last_updated: u64, } +async fn load_config() -> Result { + let config_file_name = Path::new(env!("CARGO_MANIFEST_DIR")).join("autarco.toml"); + let mut file = File::open(config_file_name).await?; + + let mut contents = String::new(); + file.read_to_string(&mut contents).await?; + let config = toml::from_str(&contents)?; + + Ok(config) +} + async fn login(driver: &WebDriver) -> Result<()> { + let config = load_config().await?; + driver.get(URL).await?; let input = driver.find_element(By::Id("username")).await?; - input.send_keys(USERNAME).await?; + input.send_keys(&config.username).await?; let input = driver.find_element(By::Id("password")).await?; - input.send_keys(PASSWORD).await?; + input.send_keys(&config.password).await?; let input = driver.find_element(By::Css("button[type=submit]")).await?; input.click().await?; @@ -123,14 +150,14 @@ async fn update_loop() -> Result<()> { } #[get("/", format = "application/json")] -fn status() -> Option> { +async fn status() -> Option> { let status_guard = STATUS.lock().expect("Status mutex was poisoned"); status_guard.map(|status| Json(status)) } #[launch] fn rocket() -> Rocket { - rocket::tokio::spawn(async { update_loop().await }); + tokio::spawn(update_loop()); rocket::ignite().mount("/", routes![status]) }