refactor: split up cli, more crate version updates

This commit is contained in:
gak 2023-08-29 11:33:03 +10:00
parent 0778ba268a
commit e3c1044dd3
No known key found for this signature in database
7 changed files with 146 additions and 133 deletions

View file

@ -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]

View file

@ -1 +1,4 @@
pub mod calendar_history;
pub mod energy;
pub mod powerwall;
pub mod vehicle;

View file

@ -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<String>,
#[clap(short, long)]
pub end: Option<String>,
}

51
src/cli/energy.rs Normal file
View file

@ -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(())
}
}

43
src/cli/powerwall.rs Normal file
View file

@ -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(())
}
}

View file

@ -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,
}

View file

@ -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<String>,
#[clap(short, long)]
end: Option<String>,
}
#[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();