Use a lazy static to store the last status
This commit is contained in:
parent
64c95854ea
commit
4c72fdab2e
3 changed files with 16 additions and 5 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -40,6 +40,7 @@ name = "autarco-scraper"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
|
"lazy_static",
|
||||||
"thirtyfour_sync",
|
"thirtyfour_sync",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,5 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
color-eyre = "0.5.6"
|
color-eyre = "0.5.6"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
thirtyfour_sync = "0.19.0"
|
thirtyfour_sync = "0.19.0"
|
||||||
|
|
19
src/main.rs
19
src/main.rs
|
@ -1,11 +1,14 @@
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use std::sync::Mutex;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
use thirtyfour_sync::prelude::*;
|
use thirtyfour_sync::prelude::*;
|
||||||
|
|
||||||
|
const URL: &'static str = "https://my.autarco.com/";
|
||||||
const USERNAME: &'static str = "pja@vtilburg.net";
|
const USERNAME: &'static str = "pja@vtilburg.net";
|
||||||
const PASSWORD: &'static str = "XXXXXXXXXXXXXXXX";
|
const PASSWORD: &'static str = "XXXXXXXXXXXXXXXX";
|
||||||
const URL: &'static str = "https://my.autarco.com/";
|
const POLL_INTERVAL: u64 = 300;
|
||||||
|
|
||||||
const GECKO_DRIVER_PORT: u16 = 18019;
|
const GECKO_DRIVER_PORT: u16 = 18019;
|
||||||
|
|
||||||
|
@ -65,6 +68,10 @@ fn element_value(driver: &WebDriver, by: By) -> Result<u32> {
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref STATUS: Mutex<Option<Status>> = Mutex::new(None);
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
color_eyre::install()?;
|
color_eyre::install()?;
|
||||||
|
|
||||||
|
@ -77,9 +84,6 @@ fn main() -> Result<()> {
|
||||||
login(&driver)?;
|
login(&driver)?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// // Take a screenshot!
|
|
||||||
// driver.screenshot(&std::path::PathBuf::from("screenshot.png"))?;
|
|
||||||
|
|
||||||
// Retrieve the data from the elements
|
// Retrieve the data from the elements
|
||||||
let last_updated = SystemTime::now()
|
let last_updated = SystemTime::now()
|
||||||
.duration_since(SystemTime::UNIX_EPOCH)
|
.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 {
|
let status = Status {
|
||||||
current_w,
|
current_w,
|
||||||
total_kwh,
|
total_kwh,
|
||||||
last_updated,
|
last_updated,
|
||||||
};
|
};
|
||||||
dbg!(&status);
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue