Skip to content

Commit

Permalink
feat: add general standing light on and off handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mgierada committed Oct 9, 2023
1 parent ee90949 commit 116e9c1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/routes/office_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use crate::GOVEE_API_KEY;
#[get("/on")]
pub async fn office_on_handler(_token: Token) -> Json<serde_json::Value> {
let devices = [
OfficeDevices::standing_left_led(),
OfficeDevices::standing_right_led(),
OfficeDevices::standing_right_led(),
OfficeDevices::table_led(),
OfficeDevices::window_led(),
OfficeDevices::board_led(),
];
Expand All @@ -30,7 +31,7 @@ pub async fn office_on_handler(_token: Token) -> Json<serde_json::Value> {
#[get("/off")]
pub async fn office_off_handler(_token: Token) -> Json<serde_json::Value> {
let devices = [
OfficeDevices::standing_right_led(),
OfficeDevices::standing_left_led(),
OfficeDevices::standing_right_led(),
OfficeDevices::table_led(),
OfficeDevices::window_led(),
Expand Down
37 changes: 37 additions & 0 deletions src/routes/standing_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<serde_json::Value> {
let devices = [
OfficeDevices::standing_left_led(),
OfficeDevices::standing_right_led(),
];
let payloads: Vec<PayloadBody> = 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<serde_json::Value> {
let devices = [
OfficeDevices::standing_left_led(),
OfficeDevices::standing_right_led(),
];
let payloads: Vec<PayloadBody> = 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<serde_json::Value> {
let standing_right_led = OfficeDevices::standing_right_led();
Expand Down

0 comments on commit 116e9c1

Please sign in to comment.