From 063b8c5fe657ae049869532829208e2725e73287 Mon Sep 17 00:00:00 2001 From: Paul van Tilburg Date: Fri, 9 Oct 2020 17:07:18 +0200 Subject: [PATCH] Use a lazy static to store the last status --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 19 ++++++++++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e6896c3..cc16ce0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,6 +40,7 @@ name = "autarco-scraper" version = "0.1.0" dependencies = [ "color-eyre", + "lazy_static", "thirtyfour_sync", ] diff --git a/Cargo.toml b/Cargo.toml index 03a78e4..2b9ec2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2018" [dependencies] color-eyre = "0.5.6" +lazy_static = "1.4.0" thirtyfour_sync = "0.19.0" diff --git a/src/main.rs b/src/main.rs index 520090f..36847b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,14 @@ use color_eyre::Result; +use lazy_static::lazy_static; +use std::sync::Mutex; use std::thread; use std::time::{Duration, SystemTime}; use thirtyfour_sync::prelude::*; +const URL: &'static str = "https://my.autarco.com/"; const USERNAME: &'static str = "pja@vtilburg.net"; const PASSWORD: &'static str = "XXXXXXXXXXXXXXXX"; -const URL: &'static str = "https://my.autarco.com/"; +const POLL_INTERVAL: u64 = 300; const GECKO_DRIVER_PORT: u16 = 18019; @@ -65,6 +68,10 @@ fn element_value(driver: &WebDriver, by: By) -> Result { Ok(value) } +lazy_static! { + static ref STATUS: Mutex> = Mutex::new(None); +} + fn main() -> Result<()> { color_eyre::install()?; @@ -77,9 +84,6 @@ fn main() -> Result<()> { login(&driver)?; loop { - // // Take a screenshot! - // driver.screenshot(&std::path::PathBuf::from("screenshot.png"))?; - // Retrieve the data from the elements let last_updated = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) @@ -100,13 +104,18 @@ fn main() -> Result<()> { } }; + // Update the status + let mut status_guard = STATUS.lock().expect("Status mutex was poisoned"); let status = Status { current_w, total_kwh, last_updated, }; dbg!(&status); + status_guard.replace(status); + drop(status_guard); - thread::sleep(Duration::from_secs(60)); + // Wait the poll interval to check again! + thread::sleep(Duration::from_secs(POLL_INTERVAL)); } }