Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
MQTT Listner
  • Loading branch information
Sticks committed Mar 6, 2024
1 parent 4cbb3eb commit 0fe7688
Show file tree
Hide file tree
Showing 5 changed files with 473 additions and 14 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

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

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Expand Up @@ -24,6 +24,7 @@ lazy_static = "1.4.0"
tokio = { version = "1.36.0", features = ["full"] }
jsonwebtoken = "9.2.0"
paho-mqtt = "0.12.3"
futures = "0.3.30"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.
Expand Down
89 changes: 86 additions & 3 deletions src-tauri/src/commands/bambu/mod.rs
@@ -1,11 +1,12 @@
use std::borrow::Borrow;

use crate::handlers::bambu::{BambuClient, BambuDevice};
use crate::handlers::bambu::{BambuClient, BambuDevice, BambuMQTTClient};
use lazy_static::lazy_static;
use serde_json::json;
use std::borrow::Borrow;
use tokio::sync::Mutex;

lazy_static! {
static ref BAMBU_CLIENT: BambuClient = BambuClient::new();
static ref BAMBU_MQTT_CLIENT: Mutex<BambuMQTTClient> = Mutex::new(BambuMQTTClient::new());
}

#[tauri::command]
Expand Down Expand Up @@ -89,3 +90,85 @@ pub async fn discover_devices(devices: Vec<BambuDevice>) -> Result<String, Strin
Err(e) => Err(e.to_string()),
}
}

#[tauri::command]
pub async fn init_mqtt_worker() -> Result<String, String> {
println!("[commands::bambu::init_mqtt_worker] initializing mqtt worker");

let result: Result<(), ()> = async {
let mut client = BAMBU_MQTT_CLIENT.lock().await;
client.initialize().await;

Ok(())
}
.await;

match result {
Ok(_) => Ok("".to_string()),
Err(_) => Err("Failed to initialize mqtt worker".to_string()),
}
}

#[tauri::command]
pub async fn watch_device(device: BambuDevice) -> Result<String, String> {
println!(
"[commands::bambu::watch_device] watching device: {:?}",
device.name
);

let result: Result<(), std::io::Error> = async {
let mut client = BAMBU_MQTT_CLIENT.lock().await;
client.watch_device(device).await
}
.await;

match result {
Ok(_) => Ok("".to_string()), // Return an empty string on success
Err(e) => {
println!("[commands::bambu::watch_device] error watching: {:?}", e);
Err(e.to_string()) // Return the error as is
}
}
}

#[tauri::command]
pub async fn unwatch_device(device: BambuDevice) -> Result<String, String> {
println!(
"[commands::bambu::unwatch_device] unwatching device: {:?}",
device.name
);

let result: Result<(), std::io::Error> = async {
let mut client = BAMBU_MQTT_CLIENT.lock().await;
client.unwatch_device(device).await
}
.await;

match result {
Ok(_) => Ok("".to_string()), // Return an empty string on success
Err(e) => {
println!(
"[commands::bambu::unwatch_device] error unwatching: {:?}",
e
);
Err(e.to_string()) // Return the error as is
}
}
}

#[tauri::command]
pub async fn deinit_mqtt_worker() -> Result<String, String> {
println!("[commands::bambu::deinit_mqtt_worker] deinitializing mqtt worker");

let result: Result<(), ()> = async {
let mut client = BAMBU_MQTT_CLIENT.lock().await;
client.deinitialize().await;
Ok(())
}
.await;

match result {
Ok(_) => Ok("".to_string()),
Err(_) => Err("Failed to deinitialize mqtt worker".to_string()),
}
}

0 comments on commit 0fe7688

Please sign in to comment.