From a41f31230d6e8e2ac837ee4cec0b6d9a207a5671 Mon Sep 17 00:00:00 2001 From: Maciej Gierada Date: Mon, 9 Oct 2023 16:29:11 +0200 Subject: [PATCH 1/5] feat: rename corner ligtht to standing right --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/constants/enums.rs | 3 ++- src/services/light_setup_service.rs | 28 ++++++++++++++++++++-------- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d0acd2..c4c9ccb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1232,7 +1232,7 @@ dependencies = [ [[package]] name = "rust_that_light" -version = "0.10.1" +version = "0.11.0" dependencies = [ "dotenv", "govee-api", diff --git a/Cargo.toml b/Cargo.toml index b6bb71c..33ab643 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rust_that_light" authors = ["Maciej Gierada @mgierada"] -version = "0.10.1" +version = "0.11.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/constants/enums.rs b/src/constants/enums.rs index 435eb11..b9f756e 100644 --- a/src/constants/enums.rs +++ b/src/constants/enums.rs @@ -5,7 +5,8 @@ pub struct Device { pub enum OfficeDevices { TableLED(Device), - CornerLED(Device), + StandingRightLED(Device), + StandingLeftLED(Device), WindowLED(Device), BoardLED(Device), } diff --git a/src/services/light_setup_service.rs b/src/services/light_setup_service.rs index 7892dac..4b12587 100644 --- a/src/services/light_setup_service.rs +++ b/src/services/light_setup_service.rs @@ -32,16 +32,28 @@ impl OfficeDevices { OfficeDevices::BoardLED(board_led) } - pub fn corner_led() -> Self { - let office_corner_light_id = - env::var("OFFICE_CORNER_LIGHT_ID").expect("OFFICE_CORNER_LIGHT_ID must be set"); - let office_corner_light_model = - env::var("OFFICE_CORNER_LIGHT_MODEL").expect("OFFICE_CORNER_LIGHT_MODEL must be set"); + pub fn standing_right_led() -> Self { + let office_standing_right_led_id= + env::var("OFFICE_STANDING_RIGHT_LIGHT_ID").expect("OFFICE_STANDING_RIGHT_LIGHT_ID must be set"); + let office_standing_right_led_model = + env::var("OFFICE_STANDING_RIGHT_MODEL").expect("OFFICE_STANDING_RIGHT_MODEL must be set"); let corner_led = Device { - device_id: office_corner_light_id, - model: office_corner_light_model, + device_id: office_standing_right_led_id, + model: office_standing_right_led_model, }; - OfficeDevices::CornerLED(corner_led) + OfficeDevices::StandingRightLED(corner_led) + } + + pub fn standing_left_led() -> Self { + let office_standing_left_led_id= + env::var("OFFICE_STANDING_LEFT_LIGHT_ID").expect("OFFICE_STANDING_LEFT_LIGHT_ID must be set"); + let office_standing_left_led_model = + env::var("OFFICE_STANDING_LEFT_MODEL").expect("OFFICE_STANDING_LEFT_MODEL must be set"); + let corner_led = Device { + device_id: office_standing_left_led_id, + model: office_standing_left_led_model, + }; + OfficeDevices::StandingLeftLED(corner_led) } pub fn table_led() -> Self { From 332c0fe15e26d6396ebfebfbc59625ae692c9985 Mon Sep 17 00:00:00 2001 From: Maciej Gierada Date: Mon, 9 Oct 2023 16:41:34 +0200 Subject: [PATCH 2/5] feat: redo routes for standing light --- src/routes.rs | 2 +- src/routes/standing_routes.rs | 54 +++++++++++++++++++++++++++++++++++ src/routes/tv_lamp_routes.rs | 27 ------------------ 3 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 src/routes/standing_routes.rs delete mode 100644 src/routes/tv_lamp_routes.rs diff --git a/src/routes.rs b/src/routes.rs index f372d97..8191160 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -2,4 +2,4 @@ pub mod all_devices_routes; pub mod healthcheck_routes; pub mod home_routes; pub mod office_routes; -pub mod tv_lamp_routes; +pub mod standing_routes; diff --git a/src/routes/standing_routes.rs b/src/routes/standing_routes.rs new file mode 100644 index 0000000..17f7a69 --- /dev/null +++ b/src/routes/standing_routes.rs @@ -0,0 +1,54 @@ +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::GoveeClient; +use rocket::serde::json::Json; + +#[get("/standing/right/on")] +pub async fn standing_right_on_handler(_token: Token) -> Json { + let standing_right_led = OfficeDevices::standing_right_led(); + let payload = office_light_setup(&standing_right_led, "on"); + let govee_client = GoveeClient::new(&GOVEE_API_KEY); + let result = govee_client.control_device(payload).await; + if let Err(err) = result { + panic!("Error occurred: {:?}", err); + } + Json(serde_json::json!({"device": "standing_right_led", "status": "on"})) +} + +#[get("/standing/right/off")] +pub async fn standing_right_off_handler(_token: Token) -> Json { + let standing_right_led = OfficeDevices::standing_right_led(); + let payload = office_light_setup(&standing_right_led, "off"); + let govee_client = GoveeClient::new(&GOVEE_API_KEY); + let result = govee_client.control_device(payload).await; + if let Err(err) = result { + panic!("Error occurred: {:?}", err); + } + Json(serde_json::json!({"device": "standing_right_led", "status": "off"})) +} + +#[get("/standing/left/on")] +pub async fn standing_left_on_handler(_token: Token) -> Json { + let standing_left_led = OfficeDevices::standing_left_led(); + let payload = office_light_setup(&standing_left_led, "on"); + let govee_client = GoveeClient::new(&GOVEE_API_KEY); + let result = govee_client.control_device(payload).await; + if let Err(err) = result { + panic!("Error occurred: {:?}", err); + } + Json(serde_json::json!({"device": "standing_left_led", "status": "on"})) +} + +#[get("/standing/left/off")] +pub async fn standing_left_off_handler(_token: Token) -> Json { + let standing_left_led = OfficeDevices::standing_left_led(); + let payload = office_light_setup(&standing_left_led, "off"); + let govee_client = GoveeClient::new(&GOVEE_API_KEY); + let result = govee_client.control_device(payload).await; + if let Err(err) = result { + panic!("Error occurred: {:?}", err); + } + Json(serde_json::json!({"device": "standing_left_led", "status": "off"})) +} diff --git a/src/routes/tv_lamp_routes.rs b/src/routes/tv_lamp_routes.rs deleted file mode 100644 index 0ab8165..0000000 --- a/src/routes/tv_lamp_routes.rs +++ /dev/null @@ -1,27 +0,0 @@ -use crate::implementations::access_token::Token; -use crate::services::light_setup_service::tv_light_setup; -use crate::GOVEE_API_KEY; -use govee_api::GoveeClient; -use rocket::serde::json::Json; - -#[get("/on")] -pub async fn tv_on_handler(_token: Token) -> Json { - let payload = tv_light_setup("on"); - let govee_client = GoveeClient::new(&GOVEE_API_KEY); - let result = govee_client.control_device(payload).await; - if let Err(err) = result { - panic!("Error occurred: {:?}", err); - } - Json(serde_json::json!({"device": "tv_light", "status": "on"})) -} - -#[get("/off")] -pub async fn tv_off_handler(_token: Token) -> Json { - let payload = tv_light_setup("off"); - let govee_client = GoveeClient::new(&GOVEE_API_KEY); - let result = govee_client.control_device(payload).await; - if let Err(err) = result { - panic!("Error occurred: {:?}", err); - } - Json(serde_json::json!({"device": "tv_light", "status": "off"})) -} From ee909498a67c1cc96689fe48e51f6350de811faf Mon Sep 17 00:00:00 2001 From: Maciej Gierada Date: Mon, 9 Oct 2023 16:56:53 +0200 Subject: [PATCH 3/5] feat: compiled version --- src/main.rs | 21 +++++++++++++------ src/routes/office_routes.rs | 30 ++++------------------------ src/routes/standing_routes.rs | 8 ++++---- src/services/light_setup_service.rs | 31 +++++++++++++++++------------ 4 files changed, 41 insertions(+), 49 deletions(-) diff --git a/src/main.rs b/src/main.rs index 64f36a7..c6140f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,11 +9,14 @@ use routes::all_devices_routes::{ use routes::healthcheck_routes::healthcheck_handler; use routes::home_routes::home; use routes::office_routes::{ - office_corner_off_handler, office_corner_on_handler, office_off_handler, office_on_handler, + office_board_off_on_handler, office_board_on_handler, office_off_handler, office_on_handler, office_table_off_handler, office_table_on_handler, office_window_off_handler, - office_window_on_handler, office_board_on_handler, office_board_off_on_handler, + office_window_on_handler, +}; +use routes::standing_routes::{ + standing_left_off_handler, standing_left_on_handler, standing_right_off_handler, + standing_right_on_handler, }; -use routes::tv_lamp_routes::{tv_off_handler, tv_on_handler}; use std::env::var; pub mod constants; @@ -42,14 +45,20 @@ fn rocket() -> _ { dotenv().ok(); rocket::build() .register("/", catchers![ununauthorized, not_found, server_error]) - .mount("/tv", routes![tv_on_handler, tv_off_handler]) + .mount( + "/standing", + routes![ + standing_left_on_handler, + standing_left_off_handler, + standing_right_on_handler, + standing_right_off_handler + ], + ) .mount( "/office", routes![ office_on_handler, office_off_handler, - office_corner_on_handler, - office_corner_off_handler, office_table_on_handler, office_table_off_handler, office_window_on_handler, diff --git a/src/routes/office_routes.rs b/src/routes/office_routes.rs index 4563bcb..17db086 100644 --- a/src/routes/office_routes.rs +++ b/src/routes/office_routes.rs @@ -10,8 +10,8 @@ use crate::GOVEE_API_KEY; #[get("/on")] pub async fn office_on_handler(_token: Token) -> Json { let devices = [ - OfficeDevices::corner_led(), - OfficeDevices::table_led(), + OfficeDevices::standing_right_led(), + OfficeDevices::standing_right_led(), OfficeDevices::window_led(), OfficeDevices::board_led(), ]; @@ -30,7 +30,8 @@ pub async fn office_on_handler(_token: Token) -> Json { #[get("/off")] pub async fn office_off_handler(_token: Token) -> Json { let devices = [ - OfficeDevices::corner_led(), + OfficeDevices::standing_right_led(), + OfficeDevices::standing_right_led(), OfficeDevices::table_led(), OfficeDevices::window_led(), OfficeDevices::board_led(), @@ -71,29 +72,6 @@ pub async fn office_board_off_on_handler(_token: Token) -> Json Json { - let corner_led = OfficeDevices::corner_led(); - let payload = office_light_setup(&corner_led, "on"); - let govee_client = GoveeClient::new(&GOVEE_API_KEY); - let result = govee_client.control_device(payload).await; - if let Err(err) = result { - panic!("Error occurred: {:?}", err); - } - Json(serde_json::json!({"device": "corner_led", "status": "on"})) -} - -#[get("/corner/off")] -pub async fn office_corner_off_handler(_token: Token) -> Json { - let corner_led = OfficeDevices::corner_led(); - let payload = office_light_setup(&corner_led, "off"); - let govee_client = GoveeClient::new(&GOVEE_API_KEY); - let result = govee_client.control_device(payload).await; - if let Err(err) = result { - panic!("Error occurred: {:?}", err); - } - Json(serde_json::json!({"device": "corner_led", "status": "off"})) -} #[get("/table/on")] pub async fn office_table_on_handler(_token: Token) -> Json { diff --git a/src/routes/standing_routes.rs b/src/routes/standing_routes.rs index 17f7a69..385ce1d 100644 --- a/src/routes/standing_routes.rs +++ b/src/routes/standing_routes.rs @@ -5,7 +5,7 @@ use crate::GOVEE_API_KEY; use govee_api::GoveeClient; use rocket::serde::json::Json; -#[get("/standing/right/on")] +#[get("/right/on")] pub async fn standing_right_on_handler(_token: Token) -> Json { let standing_right_led = OfficeDevices::standing_right_led(); let payload = office_light_setup(&standing_right_led, "on"); @@ -17,7 +17,7 @@ pub async fn standing_right_on_handler(_token: Token) -> Json Json(serde_json::json!({"device": "standing_right_led", "status": "on"})) } -#[get("/standing/right/off")] +#[get("/right/off")] pub async fn standing_right_off_handler(_token: Token) -> Json { let standing_right_led = OfficeDevices::standing_right_led(); let payload = office_light_setup(&standing_right_led, "off"); @@ -29,7 +29,7 @@ pub async fn standing_right_off_handler(_token: Token) -> Json Json { let standing_left_led = OfficeDevices::standing_left_led(); let payload = office_light_setup(&standing_left_led, "on"); @@ -41,7 +41,7 @@ pub async fn standing_left_on_handler(_token: Token) -> Json Json(serde_json::json!({"device": "standing_left_led", "status": "on"})) } -#[get("/standing/left/off")] +#[get("/left/off")] pub async fn standing_left_off_handler(_token: Token) -> Json { let standing_left_led = OfficeDevices::standing_left_led(); let payload = office_light_setup(&standing_left_led, "off"); diff --git a/src/services/light_setup_service.rs b/src/services/light_setup_service.rs index 4b12587..7c6b9e6 100644 --- a/src/services/light_setup_service.rs +++ b/src/services/light_setup_service.rs @@ -33,22 +33,22 @@ impl OfficeDevices { } pub fn standing_right_led() -> Self { - let office_standing_right_led_id= - env::var("OFFICE_STANDING_RIGHT_LIGHT_ID").expect("OFFICE_STANDING_RIGHT_LIGHT_ID must be set"); - let office_standing_right_led_model = - env::var("OFFICE_STANDING_RIGHT_MODEL").expect("OFFICE_STANDING_RIGHT_MODEL must be set"); + let office_standing_right_led_id = env::var("OFFICE_STANDING_RIGHT_LED_ID") + .expect("OFFICE_STANDING_RIGHT_LED_ID must be set"); + let office_standing_right_led_model = env::var("OFFICE_STANDING_RIGHT_LED_MODEL") + .expect("OFFICE_STANDING_RIGHT_LED_MODEL must be set"); let corner_led = Device { device_id: office_standing_right_led_id, model: office_standing_right_led_model, }; OfficeDevices::StandingRightLED(corner_led) } - + pub fn standing_left_led() -> Self { - let office_standing_left_led_id= - env::var("OFFICE_STANDING_LEFT_LIGHT_ID").expect("OFFICE_STANDING_LEFT_LIGHT_ID must be set"); + let office_standing_left_led_id = env::var("OFFICE_STANDING_LEFT_LED_ID") + .expect("OFFICE_STANDING_LEFT_LED_ID must be set"); let office_standing_left_led_model = - env::var("OFFICE_STANDING_LEFT_MODEL").expect("OFFICE_STANDING_LEFT_MODEL must be set"); + env::var("OFFICE_STANDING_LEFT_LED_MODEL").expect("OFFICE_STANDING_LEFT_LED_MODEL must be set"); let corner_led = Device { device_id: office_standing_left_led_id, model: office_standing_left_led_model, @@ -92,11 +92,6 @@ pub fn office_light_setup(device: &OfficeDevices, command: &str) -> PayloadBody model: board_led.model.clone(), cmd: command, }, - OfficeDevices::CornerLED(corner_led) => PayloadBody { - device: corner_led.device_id.clone(), - model: corner_led.model.clone(), - cmd: command, - }, OfficeDevices::TableLED(table_led) => PayloadBody { device: table_led.device_id.clone(), model: table_led.model.clone(), @@ -107,5 +102,15 @@ pub fn office_light_setup(device: &OfficeDevices, command: &str) -> PayloadBody model: window_led.model.clone(), cmd: command, }, + OfficeDevices::StandingRightLED(standing_right_led) => PayloadBody { + device: standing_right_led.device_id.clone(), + model: standing_right_led.model.clone(), + cmd: command, + }, + OfficeDevices::StandingLeftLED(standing_left_led) => PayloadBody { + device: standing_left_led.device_id.clone(), + model: standing_left_led.model.clone(), + cmd: command, + }, } } From 116e9c107adae8e99b3b276151e6296895ba8ac9 Mon Sep 17 00:00:00 2001 From: Maciej Gierada Date: Mon, 9 Oct 2023 17:04:12 +0200 Subject: [PATCH 4/5] 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(); From 1f9726002baf5b6cefe1345c0373f03a2d4b5760 Mon Sep 17 00:00:00 2001 From: Maciej Gierada Date: Mon, 9 Oct 2023 17:15:55 +0200 Subject: [PATCH 5/5] feat: improve unit tests --- src/tests/test_error_handlers/test_error_handlers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/test_error_handlers/test_error_handlers.rs b/src/tests/test_error_handlers/test_error_handlers.rs index 00f8e9c..acae742 100644 --- a/src/tests/test_error_handlers/test_error_handlers.rs +++ b/src/tests/test_error_handlers/test_error_handlers.rs @@ -8,7 +8,7 @@ mod tests { #[test] fn test_unauthorized_handler() { let client = Client::untracked(rocket()).expect("valid rocket instance"); - let response = client.get("/office/corner/on").dispatch(); + let response = client.get("/standing/right/on").dispatch(); assert_eq!(response.status(), Status::Unauthorized); let body = response.into_string().expect("response into string"); let error: AuthError = serde_json::from_str(&body).expect("deserialize error");