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" diff --git a/src/routes/office_routes.rs b/src/routes/office_routes.rs index 5ff4fa6..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; @@ -8,24 +9,19 @@ 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"); + 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 +29,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"}))