Use a lazy static to store the last status

This commit is contained in:
Paul van Tilburg 2020-10-09 17:07:18 +02:00
parent 390e31d51c
commit 063b8c5fe6
3 changed files with 16 additions and 5 deletions

1
Cargo.lock generated
View File

@ -40,6 +40,7 @@ name = "autarco-scraper"
version = "0.1.0"
dependencies = [
"color-eyre",
"lazy_static",
"thirtyfour_sync",
]

View File

@ -6,4 +6,5 @@ edition = "2018"
[dependencies]
color-eyre = "0.5.6"
lazy_static = "1.4.0"
thirtyfour_sync = "0.19.0"

View File

@ -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<u32> {
Ok(value)
}
lazy_static! {
static ref STATUS: Mutex<Option<Status>> = 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));
}
}