diff --git a/src/constants/enums.rs b/src/constants/enums.rs index b9f756e..61c996e 100644 --- a/src/constants/enums.rs +++ b/src/constants/enums.rs @@ -9,4 +9,5 @@ pub enum OfficeDevices { StandingLeftLED(Device), WindowLED(Device), BoardLED(Device), + Humidifier(Device), } diff --git a/src/main.rs b/src/main.rs index c5005e5..97c50d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use routes::home_routes::home; use routes::office_routes::{ 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_window_on_handler, office_humidifier_on_handler, office_humidifier_off_handler, }; use routes::standing_routes::{ standing_left_off_handler, standing_left_on_handler, standing_right_off_handler, @@ -66,7 +66,9 @@ fn rocket() -> _ { office_window_on_handler, office_window_off_handler, office_board_on_handler, - office_board_off_on_handler + office_board_off_on_handler, + office_humidifier_on_handler, + office_humidifier_off_handler ], ) .mount( diff --git a/src/routes/office_routes.rs b/src/routes/office_routes.rs index c63d2b3..f13d9d7 100644 --- a/src/routes/office_routes.rs +++ b/src/routes/office_routes.rs @@ -4,7 +4,7 @@ use rocket::serde::json::Json; use crate::constants::enums::OfficeDevices; use crate::implementations::access_token::Token; -use crate::services::light_setup_service::office_light_setup; +use crate::services::light_setup_service::office_setup; use crate::GOVEE_API_KEY; #[get("/on")] @@ -18,13 +18,20 @@ pub async fn office_on_handler(_token: Token) -> Json { ]; let payloads: Vec = devices .iter() - .map(|device| office_light_setup(device, "on")) + .map(|device| office_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); } + // appliences need to be added manually + let humidifier = OfficeDevices::humidifier(); + let payload = office_setup(&humidifier, "on"); + let result = govee_client.control_appliance(payload).await; + if let Err(err) = result { + panic!("Error occurred: {:?}", err); + } Json(serde_json::json!({"device": "all", "status": "on"})) } @@ -39,20 +46,26 @@ pub async fn office_off_handler(_token: Token) -> Json { ]; let payloads: Vec = devices .iter() - .map(|device| office_light_setup(device, "off")) + .map(|device| office_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", "status": "on"})) + let humidifier = OfficeDevices::humidifier(); + let payload = office_setup(&humidifier, "off"); + let result = govee_client.control_appliance(payload).await; + if let Err(err) = result { + panic!("Error occurred: {:?}", err); + } + Json(serde_json::json!({"device": "all", "status": "off"})) } #[get("/board/on")] pub async fn office_board_on_handler(_token: Token) -> Json { let board_led = OfficeDevices::board_led(); - let payload = office_light_setup(&board_led, "on"); + let payload = office_setup(&board_led, "on"); let govee_client = GoveeClient::new(&GOVEE_API_KEY); let result = govee_client.control_device(payload).await; if let Err(err) = result { @@ -64,7 +77,7 @@ pub async fn office_board_on_handler(_token: Token) -> Json { #[get("/board/off")] pub async fn office_board_off_on_handler(_token: Token) -> Json { let board_led = OfficeDevices::board_led(); - let payload = office_light_setup(&board_led, "off"); + let payload = office_setup(&board_led, "off"); let govee_client = GoveeClient::new(&GOVEE_API_KEY); let result = govee_client.control_device(payload).await; if let Err(err) = result { @@ -77,7 +90,7 @@ pub async fn office_board_off_on_handler(_token: Token) -> Json Json { let table_led = OfficeDevices::table_led(); - let payload = office_light_setup(&table_led, "on"); + let payload = office_setup(&table_led, "on"); let govee_client = GoveeClient::new(&GOVEE_API_KEY); let result = govee_client.control_device(payload).await; if let Err(err) = result { @@ -89,7 +102,7 @@ pub async fn office_table_on_handler(_token: Token) -> Json { #[get("/table/off")] pub async fn office_table_off_handler(_token: Token) -> Json { let table_led = OfficeDevices::table_led(); - let payload = office_light_setup(&table_led, "off"); + let payload = office_setup(&table_led, "off"); let govee_client = GoveeClient::new(&GOVEE_API_KEY); let result = govee_client.control_device(payload).await; if let Err(err) = result { @@ -101,7 +114,7 @@ pub async fn office_table_off_handler(_token: Token) -> Json #[get("/window/on")] pub async fn office_window_on_handler(_token: Token) -> Json { let window_led = OfficeDevices::window_led(); - let payload = office_light_setup(&window_led, "on"); + let payload = office_setup(&window_led, "on"); let govee_client = GoveeClient::new(&GOVEE_API_KEY); let result = govee_client.control_device(payload).await; if let Err(err) = result { @@ -113,7 +126,7 @@ pub async fn office_window_on_handler(_token: Token) -> Json #[get("/window/off")] pub async fn office_window_off_handler(_token: Token) -> Json { let window_led = OfficeDevices::window_led(); - let payload = office_light_setup(&window_led, "off"); + let payload = office_setup(&window_led, "off"); let govee_client = GoveeClient::new(&GOVEE_API_KEY); let result = govee_client.control_device(payload).await; if let Err(err) = result { @@ -121,3 +134,28 @@ pub async fn office_window_off_handler(_token: Token) -> Json } Json(serde_json::json!({"device": "window_led", "status": "off"})) } + + +#[get("/humidifier/on")] +pub async fn office_humidifier_on_handler(_token: Token) -> Json { + let humidifier = OfficeDevices::humidifier(); + let payload = office_setup(&humidifier, "on"); + let govee_client = GoveeClient::new(&GOVEE_API_KEY); + let result = govee_client.control_appliance(payload).await; + if let Err(err) = result { + panic!("Error occurred: {:?}", err); + } + Json(serde_json::json!({"applience": "humidifier", "status": "on"})) +} + +#[get("/humidifier/off")] +pub async fn office_humidifier_off_handler(_token: Token) -> Json { + let humidifier = OfficeDevices::humidifier(); + let payload = office_setup(&humidifier, "off"); + let govee_client = GoveeClient::new(&GOVEE_API_KEY); + let result = govee_client.control_appliance(payload).await; + if let Err(err) = result { + panic!("Error occurred: {:?}", err); + } + Json(serde_json::json!({"applience": "humidifier", "status": "off"})) +} diff --git a/src/routes/standing_routes.rs b/src/routes/standing_routes.rs index af3eb80..7f6db19 100644 --- a/src/routes/standing_routes.rs +++ b/src/routes/standing_routes.rs @@ -1,6 +1,6 @@ use crate::constants::enums::OfficeDevices; use crate::implementations::access_token::Token; -use crate::services::light_setup_service::office_light_setup; +use crate::services::light_setup_service::office_setup; use crate::GOVEE_API_KEY; use govee_api::structs::govee::PayloadBody; use govee_api::GoveeClient; @@ -14,7 +14,7 @@ pub async fn standing_on_handler(_token: Token) -> Json { ]; let payloads: Vec = devices .iter() - .map(|device| office_light_setup(device, "on")) + .map(|device| office_setup(device, "on")) .collect(); let govee_client = GoveeClient::new(&GOVEE_API_KEY); @@ -32,7 +32,7 @@ pub async fn standing_off_handler(_token: Token) -> Json { ]; let payloads: Vec = devices .iter() - .map(|device| office_light_setup(device, "off")) + .map(|device| office_setup(device, "off")) .collect(); let govee_client = GoveeClient::new(&GOVEE_API_KEY); @@ -45,7 +45,7 @@ pub async fn standing_off_handler(_token: Token) -> Json { #[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 payload = office_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 { @@ -57,7 +57,7 @@ pub async fn standing_right_on_handler(_token: Token) -> Json #[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 payload = office_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 { @@ -69,7 +69,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"); + let payload = office_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 { @@ -81,7 +81,7 @@ pub async fn standing_left_on_handler(_token: Token) -> Json #[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 payload = office_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 { diff --git a/src/services/light_setup_service.rs b/src/services/light_setup_service.rs index 7c6b9e6..fe543eb 100644 --- a/src/services/light_setup_service.rs +++ b/src/services/light_setup_service.rs @@ -5,6 +5,7 @@ use govee_api::structs::govee::{GoveeCommand, PayloadBody}; use crate::constants::enums::{Device, OfficeDevices}; pub fn tv_light_setup(command: &str) -> PayloadBody { + // TODO: Do I need this? let goove_api_device = var("GOVEE_DEVICE_ID_TV_LIGHT").expect("GOVEE_DEVICE_ID_TV_LIGHT must be set"); let goove_model = var("GOVEE_MODEL_TV_LIGHT").expect("GOVEE_MODEL_TV_LIGHT must be set"); @@ -79,9 +80,22 @@ impl OfficeDevices { }; OfficeDevices::WindowLED(window_led) } + + pub fn humidifier() -> Self { + let office_humidifier_id= + env::var("OFFICE_HUMIDIFIER_ID").expect("OFFICE_HUMIDIFIER_ID must be set"); + let office_humidifier_model = + env::var("OFFICE_HUMIDIFIER_MODEL").expect("OFFICE_HUMIDIFIER_MODEL must be set"); + let humidifier = Device { + device_id: office_humidifier_id, + model: office_humidifier_model, + }; + OfficeDevices::WindowLED(humidifier) + } + } -pub fn office_light_setup(device: &OfficeDevices, command: &str) -> PayloadBody { +pub fn office_setup(device: &OfficeDevices, command: &str) -> PayloadBody { let command = GoveeCommand { name: "turn".to_string(), value: command.to_string(), @@ -112,5 +126,10 @@ pub fn office_light_setup(device: &OfficeDevices, command: &str) -> PayloadBody model: standing_left_led.model.clone(), cmd: command, }, + OfficeDevices::Humidifier(humidifier) => PayloadBody { + device: humidifier.device_id.clone(), + model: humidifier.model.clone(), + cmd: command, + }, } }