use crate::energy_sites::{CalendarHistoryValues, HistoryKind, HistoryPeriod};
use crate::products::EnergySiteId;
use crate::OwnerApi;
use chrono::DateTime;
use clap::{Args, Subcommand};
use miette::{IntoDiagnostic, WrapErr};
#[derive(Debug, Subcommand)]
pub enum EnergySiteCommand {
SiteStatus,
LiveStatus,
SiteInfo,
CalendarHistory(CalendarHistoryArgs),
}
#[derive(Debug, Args)]
pub struct EnergySiteArgs {
pub id: EnergySiteId,
#[clap(subcommand)]
pub command: EnergySiteCommand,
}
impl EnergySiteArgs {
pub async fn run(&self, api: &OwnerApi) -> miette::Result<()> {
match &self.command {
EnergySiteCommand::SiteStatus => {
api.energy_sites_site_status(&self.id).await?;
}
EnergySiteCommand::LiveStatus => {
api.energy_sites_live_status(&self.id).await?;
}
EnergySiteCommand::SiteInfo => {
api.energy_sites_site_info(&self.id).await?;
}
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,
};
api.energy_sites_calendar_history(&values).await?;
}
}
Ok(())
}
}
#[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>,
}