Switch to the thirtyfour(_sync) crate
Also use color_eyre for error handling/reporting.
This commit is contained in:
parent
9feb8adf79
commit
6cdc8e0929
3 changed files with 1065 additions and 221 deletions
1223
Cargo.lock
generated
1223
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -4,7 +4,6 @@ version = "0.1.0"
|
||||||
authors = ["Paul van Tilburg <paul@luon.net>"]
|
authors = ["Paul van Tilburg <paul@luon.net>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
webdriver_client = "0.2.4"
|
color-eyre = "0.5.6"
|
||||||
|
thirtyfour_sync = "0.19.0"
|
||||||
|
|
58
src/main.rs
58
src/main.rs
|
@ -1,25 +1,59 @@
|
||||||
|
use color_eyre::Result;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
use webdriver_client::firefox::GeckoDriver;
|
use thirtyfour_sync::prelude::*;
|
||||||
use webdriver_client::messages::{LocationStrategy, NewSessionCmd};
|
|
||||||
use webdriver_client::{DriverSession, Error};
|
|
||||||
|
|
||||||
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 URL: &'static str = "https://my.autarco.com/";
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
const GECKO_DRIVER_PORT: u16 = 18019;
|
||||||
let driver = Box::new(GeckoDriver::spawn()?);
|
|
||||||
let session = DriverSession::create_session(driver, &NewSessionCmd::default())?;
|
|
||||||
|
|
||||||
session.go(URL)?;
|
use std::process::{Child, Command, Stdio};
|
||||||
|
|
||||||
|
struct GeckoDriver(Child);
|
||||||
|
|
||||||
|
impl GeckoDriver {
|
||||||
|
pub fn spawn() -> Result<Self> {
|
||||||
|
// This is taken from the webdriver-client crate.
|
||||||
|
let child = Command::new("geckodriver")
|
||||||
|
.arg("-b")
|
||||||
|
.arg("firefox")
|
||||||
|
.arg("--port")
|
||||||
|
.arg(format!("{}", GECKO_DRIVER_PORT))
|
||||||
|
.stdin(Stdio::null())
|
||||||
|
.stderr(Stdio::null())
|
||||||
|
.stdout(Stdio::null())
|
||||||
|
.spawn()?;
|
||||||
|
thread::sleep(Duration::new(1, 500));
|
||||||
|
|
||||||
|
Ok(GeckoDriver(child))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for GeckoDriver {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let _ = self.0.kill();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
color_eyre::install()?;
|
||||||
|
|
||||||
|
let _gecko_driver = GeckoDriver::spawn()?;
|
||||||
|
let mut caps = DesiredCapabilities::firefox();
|
||||||
|
caps.set_headless()?;
|
||||||
|
let driver = WebDriver::new(&format!("http://localhost:{}", GECKO_DRIVER_PORT), &caps)?;
|
||||||
|
|
||||||
|
// Got to the My Autarco site
|
||||||
|
driver.get(URL)?;
|
||||||
|
|
||||||
// Log in
|
// Log in
|
||||||
let input = session.find_element("input#username", LocationStrategy::Css)?;
|
let input = driver.find_element(By::Id("username"))?;
|
||||||
input.send_keys(USERNAME)?;
|
input.send_keys(USERNAME)?;
|
||||||
let input = session.find_element("input#password", LocationStrategy::Css)?;
|
let input = driver.find_element(By::Id("password"))?;
|
||||||
input.send_keys(PASSWORD)?;
|
input.send_keys(PASSWORD)?;
|
||||||
let input = session.find_element("button[type=submit]", LocationStrategy::Css)?;
|
let input = driver.find_element(By::Css("button[type=submit]"))?;
|
||||||
input.click()?;
|
input.click()?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -35,10 +69,10 @@ fn main() -> Result<(), Error> {
|
||||||
.as_secs();
|
.as_secs();
|
||||||
println!("time: {}", time);
|
println!("time: {}", time);
|
||||||
|
|
||||||
let current = session.find_element("h2#pv-now b", LocationStrategy::Css)?;
|
let current = driver.find_element(By::Css("h2#pv-now b"))?;
|
||||||
println!("current: {} W", current.text()?);
|
println!("current: {} W", current.text()?);
|
||||||
|
|
||||||
let total = session.find_element("h2#pv-to-date b", LocationStrategy::Css)?;
|
let total = driver.find_element(By::Css("h2#pv-to-date b"))?;
|
||||||
println!("total: {} kWh", total.text()?);
|
println!("total: {} kWh", total.text()?);
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
|
|
Reference in a new issue