diff --git a/src/bin/cli.rs b/src/bin/cli.rs new file mode 100644 index 0000000..533519b --- /dev/null +++ b/src/bin/cli.rs @@ -0,0 +1,53 @@ +use clap::{Parser, Subcommand}; + +#[derive(Parser)] +pub struct PowerCommand { + index: usize, + #[command(subcommand)] + state: power::types::PowerState, +} + +#[derive(Subcommand)] +pub enum Commands { + Monitor, + Set(PowerCommand), +} + +impl Commands { + pub async fn execute(self, config_file: &str) { + let s = power::system::System::new_from_file(config_file).unwrap(); + + let handle = match self { + Self::Monitor => tokio::spawn(async move { + s.try_get_power().await.unwrap(); + }), + Self::Set(command) => { + // let c = Controller::new_from_file(config_file).unwrap(); + tokio::spawn(async move { + s.try_set_power(command.index, command.state).await.unwrap(); + }) + } + }; + handle.await.unwrap(); + } +} + +#[derive(Parser)] +pub struct Cli { + #[arg(short, long)] + config_file: String, + #[command(subcommand)] + command: Commands, +} + +impl Cli { + pub async fn execute(self) { + self.command.execute(&self.config_file).await; + } +} + +#[tokio::main] +async fn main() { + let cli = Cli::parse(); + cli.execute().await; +} diff --git a/src/control.rs b/src/control.rs index 7b9ae29..05f44a9 100644 --- a/src/control.rs +++ b/src/control.rs @@ -1,5 +1,8 @@ use crate::types::{Error, PowerState}; pub trait Control { - async fn set_power(&self, state: PowerState) -> Result<(), Error>; + fn set_power( + &self, + state: PowerState, + ) -> impl std::future::Future> + Send; } diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..aa9de85 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +pub mod control; +pub mod monitor; +pub mod system; +pub mod tasmota; +pub mod types; diff --git a/src/monitor.rs b/src/monitor.rs index 6d121d9..ce999ff 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -1,9 +1,5 @@ -use crate::tasmota::{PowerStatusData, StatusResponse, TasmotaInterface, TasmotaInterfaceConfig}; use crate::types::Error; -use reqwest::Client; -use serde::Deserialize; -use std::fs; pub trait Monitoring { - async fn get_power(&self) -> Result; + fn get_power(&self) -> impl std::future::Future> + Send; } diff --git a/src/system.rs b/src/system.rs index b5ffa8c..e917d22 100644 --- a/src/system.rs +++ b/src/system.rs @@ -2,7 +2,6 @@ use crate::control::Control; use crate::monitor::Monitoring; use crate::tasmota::{TasmotaInterface, TasmotaInterfaceConfig}; use crate::types::{self, Error}; -use reqwest::Client; use serde::Deserialize; use std::fs; @@ -11,6 +10,7 @@ pub struct SystemConfig { components: Vec, } impl SystemConfig { + #[allow(unused)] fn print(&self) { for t in &self.components { t.print();