Skip to content

Commit

Permalink
Merge pull request #69 from mgierada/add_new_standing_light
Browse files Browse the repository at this point in the history
Add new standing light
  • Loading branch information
mgierada authored Oct 9, 2023
2 parents 5ab290f + 60975fb commit 05f784f
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/constants/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pub struct Device {

pub enum OfficeDevices {
TableLED(Device),
CornerLED(Device),
StandingRightLED(Device),
StandingLeftLED(Device),
WindowLED(Device),
BoardLED(Device),
}
23 changes: 17 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
29 changes: 4 additions & 25 deletions src/routes/office_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use crate::GOVEE_API_KEY;
#[get("/on")]
pub async fn office_on_handler(_token: Token) -> Json<serde_json::Value> {
let devices = [
OfficeDevices::corner_led(),
OfficeDevices::standing_left_led(),
OfficeDevices::standing_right_led(),
OfficeDevices::table_led(),
OfficeDevices::window_led(),
OfficeDevices::board_led(),
Expand All @@ -30,7 +31,8 @@ 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::corner_led(),
OfficeDevices::standing_left_led(),
OfficeDevices::standing_right_led(),
OfficeDevices::table_led(),
OfficeDevices::window_led(),
OfficeDevices::board_led(),
Expand Down Expand Up @@ -71,29 +73,6 @@ pub async fn office_board_off_on_handler(_token: Token) -> Json<serde_json::Valu
Json(serde_json::json!({"device": "board_led", "status": "off"}))
}

#[get("/corner/on")]
pub async fn office_corner_on_handler(_token: Token) -> Json<serde_json::Value> {
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<serde_json::Value> {
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<serde_json::Value> {
Expand Down
91 changes: 91 additions & 0 deletions src/routes/standing_routes.rs
Original file line number Diff line number Diff line change
@@ -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<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();
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<serde_json::Value> {
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<serde_json::Value> {
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<serde_json::Value> {
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"}))
}
27 changes: 0 additions & 27 deletions src/routes/tv_lamp_routes.rs

This file was deleted.

43 changes: 30 additions & 13 deletions src/services/light_setup_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -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,
},
}
}
2 changes: 1 addition & 1 deletion src/tests/test_error_handlers/test_error_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 05f784f

Please sign in to comment.