diff --git a/src/bin/cli.rs b/src/bin/cli.rs index 2ca186d..533519b 100644 --- a/src/bin/cli.rs +++ b/src/bin/cli.rs @@ -2,7 +2,7 @@ use clap::{Parser, Subcommand}; #[derive(Parser)] pub struct PowerCommand { - name: String, + index: usize, #[command(subcommand)] state: power::types::PowerState, } @@ -23,9 +23,9 @@ impl Commands { }), Self::Set(command) => { // let c = Controller::new_from_file(config_file).unwrap(); - tokio::spawn( - async move { s.try_set_power(&command.name, command.state).await.unwrap() }, - ) + tokio::spawn(async move { + s.try_set_power(command.index, command.state).await.unwrap(); + }) } }; handle.await.unwrap(); diff --git a/src/system.rs b/src/system.rs index 03c8de7..e917d22 100644 --- a/src/system.rs +++ b/src/system.rs @@ -50,14 +50,9 @@ impl System { Ok(()) } - pub async fn try_set_power(&self, name: &str, state: types::PowerState) -> Result<(), Error> { - match self - .components - .iter() - .find(|&component| component.config.name == name) - { - Some(component) => component.set_power(state).await, - None => Err(Error::DeviceNameNotFound(String::from(name))), - } + pub async fn try_set_power(&self, index: usize, state: types::PowerState) -> Result<(), Error> { + //TODO: check bounds + self.components[index].set_power(state).await?; + Ok(()) } } diff --git a/src/tasmota.rs b/src/tasmota.rs index 8498dde..b8bae8a 100644 --- a/src/tasmota.rs +++ b/src/tasmota.rs @@ -29,8 +29,8 @@ pub struct StatusResponse { #[derive(Deserialize, Clone)] pub struct TasmotaInterfaceConfig { - pub name: String, - pub target: String, + name: String, + target: String, } impl TasmotaInterfaceConfig { @@ -41,7 +41,7 @@ impl TasmotaInterfaceConfig { } pub struct TasmotaInterface { - pub config: TasmotaInterfaceConfig, + config: TasmotaInterfaceConfig, client: Client, } diff --git a/src/types.rs b/src/types.rs index f9c25c6..d25711e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -12,8 +12,6 @@ pub enum Error { RequestError(#[from] reqwest::Error), #[error("JSON Parse error")] JsonParseError(#[from] serde_json::Error), - #[error("device {0} not found")] - DeviceNameNotFound(String), } #[derive(Serialize, Deserialize, Parser, Clone)]