Skip to content

Commit

Permalink
fix: separate api client per device
Browse files Browse the repository at this point in the history
  • Loading branch information
WhySoBad committed Apr 2, 2024
1 parent 1cdd703 commit 8db978a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +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
timeout=10000 # Optional timeout for requests to the tapo api in milliseconds. Default: 10000
```

>[!TIP]
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ fn default_port() -> u16 {
19191
}

fn default_timeout() -> u32 { 5000 }
fn default_timeout() -> u32 { 10000 }
7 changes: 4 additions & 3 deletions src/tapo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ pub async fn start_server(port: Option<u16>, config: Option<ServerConfig>) {
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);

info!("Starting device login phase");

let devices_async = config.devices.into_iter().map(|(name, definition)| {
Device::new(name, definition, client.clone(), tx.clone())
// give every device its own client for more parallelism since it seems as if sharing the same client
// causes blocking when sending requests for multiple devices in a short period of time
let client = ApiClient::new(&config.auth.username, &config.auth.password).with_timeout(Duration::from_millis(config.timeout as u64));
Device::new(name, definition, client, tx.clone())
});

futures::future::join_all(devices_async).await.into_iter()
Expand Down

0 comments on commit 8db978a

Please sign in to comment.