Compare commits

..

No commits in common. "main" and "main" have entirely different histories.
main ... main

4 changed files with 11 additions and 18 deletions

View file

@ -2,7 +2,7 @@ use clap::{Parser, Subcommand};
#[derive(Parser)] #[derive(Parser)]
pub struct PowerCommand { pub struct PowerCommand {
name: String, index: usize,
#[command(subcommand)] #[command(subcommand)]
state: power::types::PowerState, state: power::types::PowerState,
} }
@ -23,9 +23,9 @@ impl Commands {
}), }),
Self::Set(command) => { Self::Set(command) => {
// let c = Controller::new_from_file(config_file).unwrap(); // let c = Controller::new_from_file(config_file).unwrap();
tokio::spawn( tokio::spawn(async move {
async move { s.try_set_power(&command.name, command.state).await.unwrap() }, s.try_set_power(command.index, command.state).await.unwrap();
) })
} }
}; };
handle.await.unwrap(); handle.await.unwrap();

View file

@ -50,14 +50,9 @@ impl System {
Ok(()) Ok(())
} }
pub async fn try_set_power(&self, name: &str, state: types::PowerState) -> Result<(), Error> { pub async fn try_set_power(&self, index: usize, state: types::PowerState) -> Result<(), Error> {
match self //TODO: check bounds
.components self.components[index].set_power(state).await?;
.iter() Ok(())
.find(|&component| component.config.name == name)
{
Some(component) => component.set_power(state).await,
None => Err(Error::DeviceNameNotFound(String::from(name))),
}
} }
} }

View file

@ -29,8 +29,8 @@ pub struct StatusResponse {
#[derive(Deserialize, Clone)] #[derive(Deserialize, Clone)]
pub struct TasmotaInterfaceConfig { pub struct TasmotaInterfaceConfig {
pub name: String, name: String,
pub target: String, target: String,
} }
impl TasmotaInterfaceConfig { impl TasmotaInterfaceConfig {
@ -41,7 +41,7 @@ impl TasmotaInterfaceConfig {
} }
pub struct TasmotaInterface { pub struct TasmotaInterface {
pub config: TasmotaInterfaceConfig, config: TasmotaInterfaceConfig,
client: Client, client: Client,
} }

View file

@ -12,8 +12,6 @@ pub enum Error {
RequestError(#[from] reqwest::Error), RequestError(#[from] reqwest::Error),
#[error("JSON Parse error")] #[error("JSON Parse error")]
JsonParseError(#[from] serde_json::Error), JsonParseError(#[from] serde_json::Error),
#[error("device {0} not found")]
DeviceNameNotFound(String),
} }
#[derive(Serialize, Deserialize, Parser, Clone)] #[derive(Serialize, Deserialize, Parser, Clone)]