From e3c1044dd3fab2a3e045164d81fe2a1452e0f0b4 Mon Sep 17 00:00:00 2001 From: gak Date: Tue, 29 Aug 2023 11:33:03 +1000 Subject: [PATCH] refactor: split up cli, more crate version updates --- Cargo.toml | 20 +++---- src/cli.rs | 3 ++ src/cli/calendar_history.rs | 16 ++++++ src/cli/energy.rs | 51 ++++++++++++++++++ src/cli/powerwall.rs | 43 +++++++++++++++ src/cli/vehicle.rs | 42 +++++++-------- src/main.rs | 104 +----------------------------------- 7 files changed, 146 insertions(+), 133 deletions(-) create mode 100644 src/cli/calendar_history.rs create mode 100644 src/cli/energy.rs create mode 100644 src/cli/powerwall.rs diff --git a/Cargo.toml b/Cargo.toml index 2c224a2..5fc22bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,23 +12,23 @@ fancy = ["miette/fancy"] cli = ["dep:clap", "dep:tracing-subscriber"] [dependencies] -miette = { version = "5.1", features = ["fancy"] } -thiserror = "1.0.31" -tokio = { version = "1.20", features = ["full"] } -tracing = "0.1" -reqwest = { version = "0.11", features = ["rustls-tls", "cookies", "json"] } -url = "2.2" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" +miette = { version = "5.10.0", features = ["fancy"] } +thiserror = "1.0.44" +tokio = { version = "1.29.1", features = ["full"] } +tracing = "0.1.37" +reqwest = { version = "0.11.18", features = ["rustls-tls", "cookies", "json"] } +url = "2.4.0" +serde = { version = "1.0.177", features = ["derive"] } +serde_json = "1.0.104" rustls = "0.21.5" rand = "0.8.5" chrono = { version = "0.4.26", features = ["serde"] } strum = { version = "0.25.0", features = ["derive"] } urlencoding = "2.1.3" -derive_more = "0.99" +derive_more = "0.99.17" pkce = "0.2.0" -clap = { version = "3.2", features = ["derive", "env"], optional = true } +clap = { version = "4.3.19", features = ["derive", "env"], optional = true } tracing-subscriber = { version = "0.3.17", optional = true } [dev-dependencies] diff --git a/src/cli.rs b/src/cli.rs index 1843cfc..faafcd9 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1 +1,4 @@ +pub mod calendar_history; +pub mod energy; +pub mod powerwall; pub mod vehicle; diff --git a/src/cli/calendar_history.rs b/src/cli/calendar_history.rs new file mode 100644 index 0000000..081a431 --- /dev/null +++ b/src/cli/calendar_history.rs @@ -0,0 +1,16 @@ +use crate::calendar_history::{HistoryKind, HistoryPeriod}; +use clap::Args; + +#[derive(Debug, Args)] +pub struct CalendarHistoryArgs { + pub kind: HistoryKind, + + #[clap(short, long, default_value = "day")] + pub period: HistoryPeriod, + + #[clap(short, long)] + pub start: Option, + + #[clap(short, long)] + pub end: Option, +} diff --git a/src/cli/energy.rs b/src/cli/energy.rs new file mode 100644 index 0000000..ad596a5 --- /dev/null +++ b/src/cli/energy.rs @@ -0,0 +1,51 @@ +use crate::calendar_history::CalendarHistoryValues; +use crate::cli::calendar_history::CalendarHistoryArgs; +use crate::energy::EnergySiteId; +use crate::Api; +use chrono::DateTime; +use clap::{Args, Subcommand}; +use miette::{IntoDiagnostic, WrapErr}; + +#[derive(Debug, Subcommand)] +pub enum EnergySiteCommand { + CalendarHistory(CalendarHistoryArgs), +} + +#[derive(Debug, Args)] +pub struct EnergySiteArgs { + pub id: EnergySiteId, + + #[clap(subcommand)] + pub command: EnergySiteCommand, +} + +impl EnergySiteArgs { + pub async fn run(&self, api: &Api) -> miette::Result<()> { + match &self.command { + EnergySiteCommand::CalendarHistory(args) => { + let start_date = args + .start + .as_ref() + .map(|s| DateTime::parse_from_rfc3339(&s).into_diagnostic()) + .transpose() + .wrap_err("start_date")?; + let end_date = args + .end + .as_ref() + .map(|s| DateTime::parse_from_rfc3339(&s).into_diagnostic()) + .transpose() + .wrap_err("end_date")?; + let values = CalendarHistoryValues { + site_id: self.id.clone(), + kind: args.kind.clone(), + period: args.period.clone(), + start_date, + end_date, + }; + let history = api.energy_sites_calendar_history(&values).await?; + println!("{:#?}", history); + } + } + Ok(()) + } +} diff --git a/src/cli/powerwall.rs b/src/cli/powerwall.rs new file mode 100644 index 0000000..b13f1e6 --- /dev/null +++ b/src/cli/powerwall.rs @@ -0,0 +1,43 @@ +use crate::calendar_history::{HistoryKind, HistoryPeriod}; +use crate::powerwall::{PowerwallEnergyHistoryValues, PowerwallId}; +use crate::Api; +use clap::{Args, Subcommand}; + +#[derive(Debug, Subcommand)] +pub enum PowerwallCommand { + /// Show the status of the Powerwall. + Status, + + History, +} + +#[derive(Debug, Args)] +pub struct PowerwallArgs { + pub id: PowerwallId, + + #[clap(subcommand)] + pub command: PowerwallCommand, +} + +impl PowerwallArgs { + pub async fn run(&self, api: &Api) -> miette::Result<()> { + match self.command { + PowerwallCommand::Status => { + dbg!(api.powerwall_status(&self.id).await?); + } + PowerwallCommand::History => { + dbg!( + api.powerwall_energy_history(&PowerwallEnergyHistoryValues { + powerwall_id: self.id.clone(), + period: HistoryPeriod::Day, + kind: HistoryKind::Power, + start_date: None, + end_date: None + }) + .await? + ); + } + } + Ok(()) + } +} diff --git a/src/cli/vehicle.rs b/src/cli/vehicle.rs index 2892f7d..7cb6d2a 100644 --- a/src/cli/vehicle.rs +++ b/src/cli/vehicle.rs @@ -2,6 +2,27 @@ use crate::vehicles::{SetChargeLimit, SetChargingAmps}; use crate::{Api, VehicleId}; use clap::{Args, Subcommand}; +#[derive(Debug, Subcommand)] +pub enum VehicleCommand { + /// Get vehicle data. + Data, + + /// Get charge state. + ChargeState, + + /// Set charge limit. + SetChargeLimit { percent: u8 }, + + /// Set charge amps. + SetChargingAmps { charging_amps: i64 }, + + /// Start charging. + ChargeStart, + + /// Stop charging. + ChargeStop, +} + #[derive(Debug, Args)] pub struct VehicleArgs { pub id: VehicleId, @@ -41,24 +62,3 @@ impl VehicleArgs { Ok(()) } } - -#[derive(Debug, Subcommand)] -pub enum VehicleCommand { - /// Get vehicle data. - Data, - - /// Get charge state. - ChargeState, - - /// Set charge limit. - SetChargeLimit { percent: u8 }, - - /// Set charge amps. - SetChargingAmps { charging_amps: i64 }, - - /// Start charging. - ChargeStart, - - /// Stop charging. - ChargeStop, -} diff --git a/src/main.rs b/src/main.rs index e121613..eb5db54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,9 @@ -use chrono::DateTime; use clap::{Args, Parser, Subcommand}; -use miette::{IntoDiagnostic, WrapErr}; use serde::{Deserialize, Serialize}; use teslatte::auth::{AccessToken, RefreshToken}; -use teslatte::calendar_history::{CalendarHistoryValues, HistoryKind, HistoryPeriod}; +use teslatte::cli::energy::EnergySiteArgs; +use teslatte::cli::powerwall::PowerwallArgs; use teslatte::cli::vehicle::VehicleArgs; -use teslatte::energy::EnergySiteId; -use teslatte::powerwall::{PowerwallEnergyHistoryValues, PowerwallId}; use teslatte::Api; /// Teslatte @@ -70,103 +67,6 @@ enum ApiCommand { Powerwall(PowerwallArgs), } -#[derive(Debug, Args)] -struct EnergySiteArgs { - pub id: EnergySiteId, - - #[clap(subcommand)] - pub command: EnergySiteCommand, -} - -impl EnergySiteArgs { - pub async fn run(&self, api: &Api) -> miette::Result<()> { - match &self.command { - EnergySiteCommand::CalendarHistory(args) => { - let start_date = args - .start - .as_ref() - .map(|s| DateTime::parse_from_rfc3339(&s).into_diagnostic()) - .transpose() - .wrap_err("start_date")?; - let end_date = args - .end - .as_ref() - .map(|s| DateTime::parse_from_rfc3339(&s).into_diagnostic()) - .transpose() - .wrap_err("end_date")?; - let values = CalendarHistoryValues { - site_id: self.id.clone(), - kind: args.kind.clone(), - period: args.period.clone(), - start_date, - end_date, - }; - let history = api.energy_sites_calendar_history(&values).await?; - println!("{:#?}", history); - } - } - Ok(()) - } -} - -#[derive(Debug, Subcommand)] -enum EnergySiteCommand { - CalendarHistory(CalendarHistoryArgs), -} - -#[derive(Debug, Args)] -struct CalendarHistoryArgs { - pub kind: HistoryKind, - - #[clap(short, long, default_value = "day")] - pub period: HistoryPeriod, - - #[clap(short, long)] - start: Option, - - #[clap(short, long)] - end: Option, -} - -#[derive(Debug, Args)] -struct PowerwallArgs { - pub id: PowerwallId, - - #[clap(subcommand)] - pub command: PowerwallCommand, -} - -impl PowerwallArgs { - pub async fn run(&self, api: &Api) -> miette::Result<()> { - match self.command { - PowerwallCommand::Status => { - dbg!(api.powerwall_status(&self.id).await?); - } - PowerwallCommand::History => { - dbg!( - api.powerwall_energy_history(&PowerwallEnergyHistoryValues { - powerwall_id: self.id.clone(), - period: HistoryPeriod::Day, - kind: HistoryKind::Power, - start_date: None, - end_date: None - }) - .await? - ); - } - } - Ok(()) - } -} - -#[derive(Debug, Subcommand)] -enum PowerwallCommand { - /// Show the status of the Powerwall. - Status, - - History, -} - #[tokio::main] async fn main() -> miette::Result<()> { tracing_subscriber::fmt::init();