From a04f6e1d9e48992956dd1e355063cd5a67ada71f Mon Sep 17 00:00:00 2001 From: Maciej Gierada Date: Wed, 13 Sep 2023 11:01:25 +0200 Subject: [PATCH 1/3] feat: refactor office routes - simplify bulk request to turn on and off office light --- src/routes/office_routes.rs | 60 ++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/src/routes/office_routes.rs b/src/routes/office_routes.rs index 5ff4fa6..28fd979 100644 --- a/src/routes/office_routes.rs +++ b/src/routes/office_routes.rs @@ -7,25 +7,20 @@ use crate::services::light_setup_service::office_light_setup; use crate::GOVEE_API_KEY; #[get("/on")] -pub async fn office_on_handler(_token: Token) -> Json { - let corner_led = OfficeDevices::corner_led(); - let table_led = OfficeDevices::table_led(); - let window_led = OfficeDevices::window_led(); - let board_led = OfficeDevices::board_led(); - let payload_corner = office_light_setup(&corner_led, "on"); - let payload_table = office_light_setup(&table_led, "on"); - let payload_window = office_light_setup(&window_led, "on"); - let payload_board = office_light_setup(&board_led, "on"); +pub async fn office_off_handler(_token: Token) -> Json { + let devices = [ + OfficeDevices::corner_led(), + OfficeDevices::table_led(), + OfficeDevices::window_led(), + OfficeDevices::board_led(), + ]; + let payloads: Vec = devices + .iter() + .map(|device| office_light_setup(device, "on")) + .collect(); + let govee_client = GoveeClient::new(&GOVEE_API_KEY); - let resuts = govee_client - .bulk_control_devices(vec![ - payload_corner, - payload_table, - payload_window, - payload_board, - ]) - .await; - if let Err(err) = resuts { + if let Err(err) = govee_client.bulk_control_devices(payloads.clone()).await { panic!("Error occurred: {:?}", err); } Json(serde_json::json!({"device": "all", "status": "on"})) @@ -33,24 +28,19 @@ pub async fn office_on_handler(_token: Token) -> Json { #[get("/off")] pub async fn office_off_handler(_token: Token) -> Json { - let corner_led = OfficeDevices::corner_led(); - let table_led = OfficeDevices::table_led(); - let window_led = OfficeDevices::window_led(); - let board_led = OfficeDevices::board_led(); - let payload_corner = office_light_setup(&corner_led, "off"); - let payload_table = office_light_setup(&table_led, "off"); - let payload_window = office_light_setup(&window_led, "off"); - let payload_board = office_light_setup(&board_led, "off"); + let devices = [ + OfficeDevices::corner_led(), + OfficeDevices::table_led(), + OfficeDevices::window_led(), + OfficeDevices::board_led(), + ]; + let payloads: Vec = devices + .iter() + .map(|device| office_light_setup(device, "off")) + .collect(); + let govee_client = GoveeClient::new(&GOVEE_API_KEY); - let resuts = govee_client - .bulk_control_devices(vec![ - payload_corner, - payload_table, - payload_window, - payload_board, - ]) - .await; - if let Err(err) = resuts { + if let Err(err) = govee_client.bulk_control_devices(payloads.clone()).await { panic!("Error occurred: {:?}", err); } Json(serde_json::json!({"device": "all", "status": "on"})) From 8d5b23cc88c9fe3d8f2001d1bfbb97341b7d15ed Mon Sep 17 00:00:00 2001 From: Maciej Gierada Date: Wed, 13 Sep 2023 11:12:36 +0200 Subject: [PATCH 2/3] fix: typos and imports --- src/routes/office_routes.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/routes/office_routes.rs b/src/routes/office_routes.rs index 28fd979..4563bcb 100644 --- a/src/routes/office_routes.rs +++ b/src/routes/office_routes.rs @@ -1,4 +1,5 @@ use govee_api::GoveeClient; +use govee_api::structs::govee::PayloadBody; use rocket::serde::json::Json; use crate::constants::enums::OfficeDevices; @@ -7,7 +8,7 @@ use crate::services::light_setup_service::office_light_setup; use crate::GOVEE_API_KEY; #[get("/on")] -pub async fn office_off_handler(_token: Token) -> Json { +pub async fn office_on_handler(_token: Token) -> Json { let devices = [ OfficeDevices::corner_led(), OfficeDevices::table_led(), From 500aa33e91019ec3103eb538b62e8897f06b42a7 Mon Sep 17 00:00:00 2001 From: Maciej Gierada Date: Wed, 13 Sep 2023 21:45:58 +0200 Subject: [PATCH 3/3] feat: use govee-api v.1.3.0 --- Cargo.lock | 6 +++--- Cargo.toml | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c4f176..64b1f05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -454,9 +454,9 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] name = "govee-api" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae8d6582c76370dea793021019f555c094dfdc5d613859efc88a72d9bde69441" +checksum = "8fc89541d0c89ab07adbd25e1326ccf46b10d8fb3c2347d3cde3ed3c3357b3a2" dependencies = [ "futures", "lazy_static", @@ -1233,7 +1233,7 @@ dependencies = [ [[package]] name = "rust_that_light" -version = "0.9.0" +version = "0.10.0" dependencies = [ "dotenv", "govee-api", diff --git a/Cargo.toml b/Cargo.toml index cf38043..5f599cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rust_that_light" authors = ["Maciej Gierada @mgierada"] -version = "0.9.0" +version = "0.10.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -12,8 +12,8 @@ reqwest = {version = "0.11.20", features = ["blocking", "json"]} serde = {version = "1.0.188", features = ["derive"]} serde_json = "1.0.106" lazy_static = "1.4.0" -govee-api = {version = "1.2.0"} -# govee-api = {version = "1.2.0", path = "../../govee-api.git/bulk_control_device"} +govee-api = {version = "1.3.0"} +# govee-api = {version = "1.3.0", path = "../../govee-api.git/add_clone_derive_to_PayloadBody/"} [dependencies.rocket] version = "0.5.0-rc.1"