auth (maybe?)
This commit is contained in:
parent
871ba961cc
commit
ed2fee6571
2095
Cargo.lock
generated
2095
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -24,3 +24,9 @@ assets = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = { version = "4.0", features = ["derive"] }
|
||||||
|
ron = "0.8"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
tokio = { version = "1.35.1", features = ["full"] }
|
||||||
|
teslatte = "0.1.11"
|
||||||
|
thiserror = "1.0"
|
||||||
|
|
2
debian/service
vendored
2
debian/service
vendored
|
@ -8,7 +8,7 @@ Type=simple
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=1
|
RestartSec=1
|
||||||
User=tesla
|
User=tesla
|
||||||
ExecStart=/usr/bin/tesla-charge-controller
|
ExecStart=/usr/bin/tesla-charge-controller watch
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|
2
rust-toolchain.toml
Normal file
2
rust-toolchain.toml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
[toolchain]
|
||||||
|
channel = "nightly"
|
7
src/config.rs
Normal file
7
src/config.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub struct Config {}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub struct Auth {}
|
91
src/main.rs
91
src/main.rs
|
@ -1,3 +1,90 @@
|
||||||
fn main() {
|
use std::{io::BufRead, path::PathBuf};
|
||||||
println!("Hello, world!");
|
|
||||||
|
use clap::{Parser, Subcommand};
|
||||||
|
use teslatte::{auth::AccessToken, OwnerApi};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
mod config;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug, Clone)]
|
||||||
|
#[clap(author, version, about, long_about = None)]
|
||||||
|
struct Args {
|
||||||
|
#[command(subcommand)]
|
||||||
|
command: Commands,
|
||||||
|
#[clap(long, default_value = "/etc/tesla-charge-controller")]
|
||||||
|
config_dir: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand, Debug, Clone)]
|
||||||
|
enum Commands {
|
||||||
|
/// Run charge controller server
|
||||||
|
Watch,
|
||||||
|
/// Authenticate with Tesla login
|
||||||
|
Auth {},
|
||||||
|
}
|
||||||
|
|
||||||
|
fn press_y_to_continue() -> bool {
|
||||||
|
println!("Continue? [y/N]");
|
||||||
|
let mut line = String::new();
|
||||||
|
let stdin = std::io::stdin();
|
||||||
|
stdin.lock().read_line(&mut line).unwrap();
|
||||||
|
if line.to_uppercase() == "Y\n" {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
println!("Exiting now!");
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
let args = Args::parse();
|
||||||
|
let auth_path = args.config_dir.join("auth");
|
||||||
|
let config_path = args.config_dir.join("config");
|
||||||
|
match args.command {
|
||||||
|
Commands::Auth {} => {
|
||||||
|
if auth_path.exists() {
|
||||||
|
println!("Auth file already exists");
|
||||||
|
if !press_y_to_continue() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Err(e) = log_in(auth_path).await {
|
||||||
|
println!("Error: {e:#?}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Commands::Watch => match get_auth(auth_path) {
|
||||||
|
Ok(api) => {
|
||||||
|
println!("got products: {:#?}", api.products().await)
|
||||||
|
}
|
||||||
|
Err(e) => println!("error getting auth: {e:#?}"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum TeslaError {
|
||||||
|
#[error("teslatte error")]
|
||||||
|
Teslatte(#[from] teslatte::error::TeslatteError),
|
||||||
|
#[error("ron error")]
|
||||||
|
Ron(#[from] ron::Error),
|
||||||
|
#[error("stdio error")]
|
||||||
|
StdIo(#[from] std::io::Error),
|
||||||
|
#[error("ron - spanned error")]
|
||||||
|
RonSpanned(#[from] ron::error::SpannedError),
|
||||||
|
#[error("unknown error")]
|
||||||
|
Unknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_auth(auth_path: PathBuf) -> Result<OwnerApi, TeslaError> {
|
||||||
|
let key: AccessToken = ron::from_str(&std::fs::read_to_string(auth_path)?)?;
|
||||||
|
Ok(OwnerApi::new(key, None))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn log_in(auth_path: PathBuf) -> Result<(), TeslaError> {
|
||||||
|
let v = OwnerApi::from_interactive_url().await?;
|
||||||
|
|
||||||
|
std::fs::write(auth_path, ron::ser::to_string(&v.access_token)?)?;
|
||||||
|
println!("Auth successfully saved");
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue