From 116e9c107adae8e99b3b276151e6296895ba8ac9 Mon Sep 17 00:00:00 2001 From: Maciej Gierada Date: Mon, 9 Oct 2023 17:04:12 +0200 Subject: [PATCH] feat: add general standing light on and off handler --- src/main.rs | 4 +++- src/routes/office_routes.rs | 5 +++-- src/routes/standing_routes.rs | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index c6140f0..c5005e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use routes::office_routes::{ }; use routes::standing_routes::{ standing_left_off_handler, standing_left_on_handler, standing_right_off_handler, - standing_right_on_handler, + standing_right_on_handler, standing_on_handler, standing_off_handler, }; use std::env::var; @@ -48,6 +48,8 @@ fn rocket() -> _ { .mount( "/standing", routes![ + standing_on_handler, + standing_off_handler, standing_left_on_handler, standing_left_off_handler, standing_right_on_handler, diff --git a/src/routes/office_routes.rs b/src/routes/office_routes.rs index 17db086..c63d2b3 100644 --- a/src/routes/office_routes.rs +++ b/src/routes/office_routes.rs @@ -10,8 +10,9 @@ use crate::GOVEE_API_KEY; #[get("/on")] pub async fn office_on_handler(_token: Token) -> Json { let devices = [ + OfficeDevices::standing_left_led(), OfficeDevices::standing_right_led(), - OfficeDevices::standing_right_led(), + OfficeDevices::table_led(), OfficeDevices::window_led(), OfficeDevices::board_led(), ]; @@ -30,7 +31,7 @@ pub async fn office_on_handler(_token: Token) -> Json { #[get("/off")] pub async fn office_off_handler(_token: Token) -> Json { let devices = [ - OfficeDevices::standing_right_led(), + OfficeDevices::standing_left_led(), OfficeDevices::standing_right_led(), OfficeDevices::table_led(), OfficeDevices::window_led(), diff --git a/src/routes/standing_routes.rs b/src/routes/standing_routes.rs index 385ce1d..af3eb80 100644 --- a/src/routes/standing_routes.rs +++ b/src/routes/standing_routes.rs @@ -2,9 +2,46 @@ use crate::constants::enums::OfficeDevices; use crate::implementations::access_token::Token; use crate::services::light_setup_service::office_light_setup; use crate::GOVEE_API_KEY; +use govee_api::structs::govee::PayloadBody; use govee_api::GoveeClient; use rocket::serde::json::Json; +#[get("/on")] +pub async fn standing_on_handler(_token: Token) -> Json { + let devices = [ + OfficeDevices::standing_left_led(), + OfficeDevices::standing_right_led(), + ]; + let payloads: Vec = devices + .iter() + .map(|device| office_light_setup(device, "on")) + .collect(); + + let govee_client = GoveeClient::new(&GOVEE_API_KEY); + if let Err(err) = govee_client.bulk_control_devices(payloads.clone()).await { + panic!("Error occurred: {:?}", err); + } + Json(serde_json::json!({"device": "all_standing_led", "status": "on"})) +} + +#[get("/off")] +pub async fn standing_off_handler(_token: Token) -> Json { + let devices = [ + OfficeDevices::standing_left_led(), + OfficeDevices::standing_right_led(), + ]; + let payloads: Vec = devices + .iter() + .map(|device| office_light_setup(device, "off")) + .collect(); + + let govee_client = GoveeClient::new(&GOVEE_API_KEY); + if let Err(err) = govee_client.bulk_control_devices(payloads.clone()).await { + panic!("Error occurred: {:?}", err); + } + Json(serde_json::json!({"device": "all_standing_led", "status": "off"})) +} + #[get("/right/on")] pub async fn standing_right_on_handler(_token: Token) -> Json { let standing_right_led = OfficeDevices::standing_right_led();