This commit is contained in:
Robin Appelman 2024-01-27 17:14:44 +01:00
commit 6a31d536bf
3 changed files with 22 additions and 5 deletions

View file

@ -8,7 +8,7 @@ rust-version = "1.70.0"
[dependencies]
rumqttc = { version = "0.23.0", features = ["use-rustls"] }
thiserror = "1.0.56"
tokio = { version = "1.35.1", features = ["rt-multi-thread", "sync"] }
tokio = { version = "1.35.1", features = ["sync"] }
tracing = "0.1.40"
async-stream = "0.3.5"
tokio-stream = { version = "0.1.14", features = ["sync"] }
@ -21,4 +21,5 @@ md-5 = "0.10.6"
[dev-dependencies]
clap = { version = "4.4.18", features = ["derive"] }
tokio = { version = "1.35.1", features = ["rt-multi-thread", "macros"] }
hex_fmt = "0.3.0"

View file

@ -4,6 +4,7 @@ use crate::Result;
use bytes::{Bytes, BytesMut};
use md5::{Digest, Md5};
use serde::{Deserialize, Serialize};
use tracing::debug;
#[derive(Serialize)]
struct SendDownloadPayload<'a> {
@ -68,6 +69,7 @@ pub async fn download_config(
let msg = rx.recv().await.unwrap();
if let Ok(response) = serde_json::from_slice::<DownloadResponse>(msg.payload.as_ref()) {
debug!(message = ?response, "processing download status message");
if let Some(status) = response.file_download {
match status {
"Started" => {
@ -111,6 +113,7 @@ pub async fn download_config(
continue;
}
} else {
debug!(size = msg.payload.len(), "processing download chunk");
state.data.extend(msg.payload);
}

View file

@ -11,6 +11,7 @@ use rumqttc::MqttOptions;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use std::collections::BTreeSet;
use std::fmt::Debug;
use std::net::IpAddr;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
@ -20,6 +21,7 @@ use tokio::sync::broadcast::{channel, Sender};
use tokio::time::timeout;
use tokio_stream::wrappers::BroadcastStream;
use tokio_stream::{Stream, StreamExt};
use tracing::debug;
pub struct TasmotaClient {
mqtt: MqttHelper,
@ -58,6 +60,11 @@ impl TasmotaClient {
continue;
};
debug!(
message = payload,
device = device,
"processing discovery message"
);
match payload {
"Online" => {
if edit_devices.lock().unwrap().insert(device.into()) {
@ -84,7 +91,7 @@ impl TasmotaClient {
/// Set the timeout used for one-show commands
///
/// The default timeout is 1 seccond
/// The default timeout is 1 second
pub fn set_timeout(&mut self, timeout: Duration) {
self.timeout = timeout;
}
@ -92,6 +99,7 @@ impl TasmotaClient {
/// Download the config backup from a device
///
/// The password is the mqtt password used by the device, which might be different from the mqtt password used by this client
#[tracing::instrument(skip(self))]
pub async fn download_config(&self, client: &str, password: &str) -> Result<DownloadedFile> {
download_config(&self.mqtt, client, password).await
}
@ -116,7 +124,8 @@ impl TasmotaClient {
}
/// Send a command that expect a single reply message
pub async fn command<T: DeserializeOwned>(
#[tracing::instrument(skip(self))]
pub async fn command<T: DeserializeOwned + Debug>(
&self,
device: &str,
command: &str,
@ -142,8 +151,10 @@ impl TasmotaClient {
.map_err(|_| Error::Timeout)?
}
/// Get the ip address for the device
#[tracing::instrument(skip(self))]
pub async fn device_ip(&self, device: &str) -> Result<IpAddr> {
#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
struct IpAddressResponse {
#[serde(rename = "IPAddress1")]
ip_address_1: String,
@ -164,8 +175,10 @@ impl TasmotaClient {
Ok(ip)
}
/// Get the name for the device
#[tracing::instrument(skip(self))]
pub async fn device_name(&self, device: &str) -> Result<String> {
#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
struct NameResponse {
#[serde(rename = "DeviceName")]
device_name: String,