diff --git a/Cargo.lock b/Cargo.lock index ac0d8b6..425b82d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1233,7 +1233,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 19ef3b2..4e8dd8a 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/main.rs b/src/main.rs index 64f36a7..c5005e5 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, standing_on_handler, standing_off_handler, }; -use routes::tv_lamp_routes::{tv_off_handler, tv_on_handler}; use std::env::var; pub mod constants; @@ -42,14 +45,22 @@ 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_on_handler, + standing_off_handler, + 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.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/office_routes.rs b/src/routes/office_routes.rs index 4563bcb..c63d2b3 100644 --- a/src/routes/office_routes.rs +++ b/src/routes/office_routes.rs @@ -10,7 +10,8 @@ use crate::GOVEE_API_KEY; #[get("/on")] pub async fn office_on_handler(_token: Token) -> Json { let devices = [ - OfficeDevices::corner_led(), + OfficeDevices::standing_left_led(), + OfficeDevices::standing_right_led(), OfficeDevices::table_led(), OfficeDevices::window_led(), OfficeDevices::board_led(), @@ -30,7 +31,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_left_led(), + OfficeDevices::standing_right_led(), OfficeDevices::table_led(), OfficeDevices::window_led(), OfficeDevices::board_led(), @@ -71,29 +73,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 new file mode 100644 index 0000000..af3eb80 --- /dev/null +++ b/src/routes/standing_routes.rs @@ -0,0 +1,91 @@ +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(); + 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("/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("/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("/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"})) -} diff --git a/src/services/light_setup_service.rs b/src/services/light_setup_service.rs index 7892dac..7c6b9e6 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_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_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_LED_ID") + .expect("OFFICE_STANDING_LEFT_LED_ID must be set"); + let office_standing_left_led_model = + 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, + }; + OfficeDevices::StandingLeftLED(corner_led) } pub fn table_led() -> Self { @@ -80,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(), @@ -95,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, + }, } } 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");