Skip to content

Commit

Permalink
feat: updated tapo, added timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
WhySoBad committed Mar 31, 2024
1 parent de2990a commit 9caf5bd
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
19 changes: 12 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ prost = "0.12.3"
serde = { version = "1.0.197", features = ["serde_derive"]}
serde_json = "1.0.114"
spinoff = "0.8.0"
tapo = { version = "0.7.9"}
tapo = { git = "https://github.com/mihai-dinculescu/tapo"}
tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros"]}
toml = { version = "0.8.11"}
tonic = { version = "0.11.0"}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ For the following devices the support is coming soon™:
The cli supports the following commands:

* `devices`: List all devices registered on the server
* `events`: Subscribe to live events (device change, auth change)
* `set <device>`: Update one or more properties of a light bulb <br>
`--brightness`: Brightness value between 1 and 100<br>
`--hue`: Hue value between 1 and 360<br>
Expand Down Expand Up @@ -109,6 +110,7 @@ type="L530" # The device type of the light bulb (L530, L520, ...)
address="10.255.255.10" # The address under which the device can be reached

port=19191 # Optional port to listen on. Default: 19191
timeout=5000 # Optional timeout for requests to the tapo api in milliseconds. Default: 5000
```

>[!TIP]
Expand Down
10 changes: 7 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;
use std::path::PathBuf;
use std::process::exit;
use anyhow::Context;
use log::{debug, error, warn};
use log::{debug, error};
use serde::Deserialize;

const CONFIG_PATH: &str = "tapoctl/config.toml";
Expand Down Expand Up @@ -40,7 +40,9 @@ pub struct ServerConfig {
pub auth: Authentication,
pub devices: HashMap<String, DeviceDefinition>,
#[serde(default = "default_port")]
pub port: u16
pub port: u16,
#[serde(default = "default_timeout")]
pub timeout: u32
}

#[derive(Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -112,4 +114,6 @@ fn default_address() -> String {

fn default_port() -> u16 {
19191
}
}

fn default_timeout() -> u32 { 5000 }
11 changes: 3 additions & 8 deletions src/tapo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use std::process::exit;
use std::sync::Arc;
use std::time::Duration;
use colored::{Colorize, CustomColor};
use colorsys::Rgb;
use log::{error, info};
Expand All @@ -15,7 +16,7 @@ use crate::cli::SpinnerOpt;
use crate::config::ServerConfig;
use crate::device;
use crate::device::Device;
use crate::tapo::server::rpc::{Color, EventRequest, EventResponse, EventType, InfoResponse, SessionStatus, UsageResponse};
use crate::tapo::server::rpc::{Color, EventResponse, EventType, InfoResponse, SessionStatus, UsageResponse};
use crate::tapo::server::rpc::tapo_server::TapoServer;
use crate::tapo::server::{rpc, TapoService};

Expand All @@ -29,13 +30,7 @@ pub async fn start_server(port: Option<u16>, config: Option<ServerConfig>) {
exit(1);
};

let client = match ApiClient::new(&config.auth.username, &config.auth.password) {
Ok(client) => client,
Err(_) => {
error!("Unable to create tapo api client");
exit(1);
}
};
let client = ApiClient::new(&config.auth.username, &config.auth.password).with_timeout(Duration::from_millis(config.timeout as u64));

let mut devices = HashMap::<String, Arc<Mutex<Device>>>::new();
let (tx, rx) = tokio::sync::broadcast::channel(10);
Expand Down
1 change: 0 additions & 1 deletion src/tapo/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::cmp::{max, min};
use std::collections::HashMap;
use std::sync::Arc;
use futures::future::join_all;
use log::debug;
use tokio::sync::{Mutex, MutexGuard};
use tonic::{Request, Response, Status};
use tonic::codegen::tokio_stream::wrappers::ReceiverStream;
Expand Down

0 comments on commit 9caf5bd

Please sign in to comment.