Skip to content

Commit

Permalink
Merge pull request #87 from mgierada/add_humidifire
Browse files Browse the repository at this point in the history
Add humidifier
  • Loading branch information
mgierada authored Jan 12, 2024
2 parents cdd077b + d625d93 commit 1491438
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/constants/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub enum OfficeDevices {
StandingLeftLED(Device),
WindowLED(Device),
BoardLED(Device),
Humidifier(Device),
}
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
58 changes: 48 additions & 10 deletions src/routes/office_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -18,13 +18,20 @@ pub async fn office_on_handler(_token: Token) -> Json<serde_json::Value> {
];
let payloads: Vec<PayloadBody> = 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"}))
}

Expand All @@ -39,20 +46,26 @@ pub async fn office_off_handler(_token: Token) -> Json<serde_json::Value> {
];
let payloads: Vec<PayloadBody> = 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<serde_json::Value> {
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 {
Expand All @@ -64,7 +77,7 @@ pub async fn office_board_on_handler(_token: Token) -> Json<serde_json::Value> {
#[get("/board/off")]
pub async fn office_board_off_on_handler(_token: Token) -> Json<serde_json::Value> {
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 {
Expand All @@ -77,7 +90,7 @@ pub async fn office_board_off_on_handler(_token: Token) -> Json<serde_json::Valu
#[get("/table/on")]
pub async fn office_table_on_handler(_token: Token) -> Json<serde_json::Value> {
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 {
Expand All @@ -89,7 +102,7 @@ pub async fn office_table_on_handler(_token: Token) -> Json<serde_json::Value> {
#[get("/table/off")]
pub async fn office_table_off_handler(_token: Token) -> Json<serde_json::Value> {
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 {
Expand All @@ -101,7 +114,7 @@ pub async fn office_table_off_handler(_token: Token) -> Json<serde_json::Value>
#[get("/window/on")]
pub async fn office_window_on_handler(_token: Token) -> Json<serde_json::Value> {
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 {
Expand All @@ -113,11 +126,36 @@ pub async fn office_window_on_handler(_token: Token) -> Json<serde_json::Value>
#[get("/window/off")]
pub async fn office_window_off_handler(_token: Token) -> Json<serde_json::Value> {
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 {
panic!("Error occurred: {:?}", err);
}
Json(serde_json::json!({"device": "window_led", "status": "off"}))
}


#[get("/humidifier/on")]
pub async fn office_humidifier_on_handler(_token: Token) -> Json<serde_json::Value> {
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<serde_json::Value> {
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"}))
}
14 changes: 7 additions & 7 deletions src/routes/standing_routes.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,7 +14,7 @@ pub async fn standing_on_handler(_token: Token) -> Json<serde_json::Value> {
];
let payloads: Vec<PayloadBody> = devices
.iter()
.map(|device| office_light_setup(device, "on"))
.map(|device| office_setup(device, "on"))
.collect();

let govee_client = GoveeClient::new(&GOVEE_API_KEY);
Expand All @@ -32,7 +32,7 @@ pub async fn standing_off_handler(_token: Token) -> Json<serde_json::Value> {
];
let payloads: Vec<PayloadBody> = devices
.iter()
.map(|device| office_light_setup(device, "off"))
.map(|device| office_setup(device, "off"))
.collect();

let govee_client = GoveeClient::new(&GOVEE_API_KEY);
Expand All @@ -45,7 +45,7 @@ pub async fn standing_off_handler(_token: Token) -> Json<serde_json::Value> {
#[get("/right/on")]
pub async fn standing_right_on_handler(_token: Token) -> Json<serde_json::Value> {
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 {
Expand All @@ -57,7 +57,7 @@ pub async fn standing_right_on_handler(_token: Token) -> Json<serde_json::Value>
#[get("/right/off")]
pub async fn standing_right_off_handler(_token: Token) -> Json<serde_json::Value> {
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 {
Expand All @@ -69,7 +69,7 @@ pub async fn standing_right_off_handler(_token: Token) -> Json<serde_json::Value
#[get("/left/on")]
pub async fn standing_left_on_handler(_token: Token) -> Json<serde_json::Value> {
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 {
Expand All @@ -81,7 +81,7 @@ pub async fn standing_left_on_handler(_token: Token) -> Json<serde_json::Value>
#[get("/left/off")]
pub async fn standing_left_off_handler(_token: Token) -> Json<serde_json::Value> {
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 {
Expand Down
21 changes: 20 additions & 1 deletion src/services/light_setup_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
},
}
}

0 comments on commit 1491438

Please sign in to comment.