Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more progress
  • Loading branch information
Sticks committed Mar 4, 2024
1 parent 7b0cec4 commit 0c2a7af
Show file tree
Hide file tree
Showing 11 changed files with 568 additions and 9 deletions.
15 changes: 15 additions & 0 deletions bruno/BambuAPI/Get Devices.bru
@@ -0,0 +1,15 @@
meta {
name: Get Devices
type: http
seq: 1
}

get {
url: https://api.bambulab.com/v1/iot-service/api/user/bind
body: none
auth: bearer
}

auth:bearer {
token:
}
9 changes: 9 additions & 0 deletions bruno/BambuAPI/bruno.json
@@ -0,0 +1,9 @@
{
"version": "1",
"name": "BambuAPI",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}
89 changes: 89 additions & 0 deletions src-tauri/Cargo.lock

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

3 changes: 3 additions & 0 deletions src-tauri/Cargo.toml
Expand Up @@ -21,6 +21,9 @@ tauri = { version = "1.6.0", features = [ "dialog-confirm", "dialog-ask", "dialo
dirs = "5.0.1"
reqwest = "0.11.24"
paho-mqtt = "0.12.3"
lazy_static = "1.4.0"
tokio = "1.36.0"
jsonwebtoken = "9.2.0"

[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
71 changes: 69 additions & 2 deletions src-tauri/src/commands/bambu/mod.rs
@@ -1,10 +1,17 @@
use crate::handlers::bambu::BambuClient;
use std::borrow::Borrow;

use crate::handlers::bambu::{BambuClient, BambuDevice};
use lazy_static::lazy_static;

lazy_static! {
static ref BAMBU_CLIENT: BambuClient = BambuClient::new();
}

#[tauri::command]
pub async fn login_to_bambu(username: String, password: String) -> Result<String, String> {
println!("[commands::bambu::login_to_bambu] trying to login to bambu with username: {} and password: {}", username, password);

let client = BambuClient::new();
let client = BAMBU_CLIENT.borrow();
let response = client.login(&username, &password).await;

println!("[commands::bambu::login_to_bambu] response: {:?}", response);
Expand All @@ -18,3 +25,63 @@ pub async fn login_to_bambu(username: String, password: String) -> Result<String
Err(e) => Err(e.to_string()),
}
}

#[tauri::command]
pub async fn set_jwt(jwt: String) -> Result<String, String> {
println!("[commands::bambu::set_jwt] setting jwt: {}", jwt);

let client = BAMBU_CLIENT.borrow();
client.set_jwt(jwt).await;

Ok("".to_string())
}

#[tauri::command]
pub async fn get_jwt() -> Result<String, String> {
println!("[commands::bambu::get_jwt] getting jwt");

let client = BAMBU_CLIENT.borrow();
let jwt = client.get_jwt().await;

println!("[commands::bambu::get_jwt] jwt: {:?}", jwt);
match jwt {
Some(jwt) => Ok(jwt),
None => Err("No jwt found".to_string()),
}
}

#[tauri::command]
pub async fn fetch_devices() -> Result<String, String> {
println!("[commands::bambu::fetch_devices] fetching devices");

let client = BAMBU_CLIENT.borrow();
let devices = client.get_devices().await;
println!("[commands::bambu::fetch_devices] devices: {:?}", devices);

match devices {
Ok(devices) => {
// Serialize the response to JSON
let serialized_devices = serde_json::to_string(&devices).map_err(|e| e.to_string())?;
Ok(serialized_devices)
}
Err(e) => Err(e.to_string()),
}
}

#[tauri::command]
pub async fn discover_devices(devices: Vec<BambuDevice>) -> Result<String, String> {
println!("[commands::bambu::discover_devices] discovering devices");

let client = BAMBU_CLIENT.borrow();
let devices = client.get_device_ips(devices).await;
println!("[commands::bambu::discover_devices] devices: {:?}", devices);

match devices {
Ok(devices) => {
// Serialize the response to JSON
let serialized_devices = serde_json::to_string(&devices).map_err(|e| e.to_string())?;
Ok(serialized_devices)
}
Err(e) => Err(e.to_string()),
}
}
1 change: 1 addition & 0 deletions src-tauri/src/constants.rs
@@ -1,4 +1,5 @@
pub static BAMBU_API_URL: &str = "https://api.bambulab.com";
pub static BAMBU_AUDIENCE: &str = "account";
pub static BAMBU_LOGIN_URL: &str = "https://bambulab.com/api/sign-in/form";
pub static BAMBU_MQTT_URL: &str = "mqtts://us.mqtt.bambulab.com:8883";
pub static BAMBU_MQTT_INIT_PAYLOAD: &str =
Expand Down

0 comments on commit 0c2a7af

Please sign in to comment.