Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add window light handler #58

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.5.0"
version = "0.6.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions src/constants/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub struct Device {
pub enum OfficeDevices {
TableLED(Device),
CornerLED(Device),
WindowLED(Device)
}
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use routes::healthcheck_routes::healthcheck_handler;
use routes::home_routes::home;
use routes::office_routes::{
office_corner_off_handler, office_corner_on_handler, office_table_off_handler,
office_table_on_handler,
office_table_on_handler, office_window_on_handler, office_window_off_handler,
};
use routes::tv_lamp_routes::{tv_off_handler, tv_on_handler};
use std::env::var;
Expand Down Expand Up @@ -48,7 +48,9 @@ fn rocket() -> _ {
office_corner_on_handler,
office_corner_off_handler,
office_table_on_handler,
office_table_off_handler
office_table_off_handler,
office_window_on_handler,
office_window_off_handler
],
)
.mount(
Expand Down
24 changes: 24 additions & 0 deletions src/routes/office_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,27 @@ pub async fn office_table_off_handler(_token: Token) -> Json<serde_json::Value>
}
Json(serde_json::json!({"device": "table_led", "status": "off"}))
}

#[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 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": "on"}))
}

#[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 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"}))
}
19 changes: 19 additions & 0 deletions src/services/light_setup_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ impl OfficeDevices {

OfficeDevices::TableLED(table_led)
}

pub fn window_led() -> Self {
let office_window_led_id =
env::var("OFFICE_WINDOW_LED_ID").expect("OFFICE_WINDOW_LED_ID must be set");
let office_window_led_model =
env::var("OFFICE_WINDOW_LED_MODEL").expect("OFFICE_WINDOW_LED_MODEL must be set");

let window_led = Device {
device_id: office_window_led_id,
model: office_window_led_model,
};

OfficeDevices::WindowLED(window_led)
}
}

pub fn office_light_setup(device: &OfficeDevices, command: &str) -> PayloadBody {
Expand All @@ -65,5 +79,10 @@ pub fn office_light_setup(device: &OfficeDevices, command: &str) -> PayloadBody
model: table_led.model.clone(),
cmd: command,
},
OfficeDevices::WindowLED(window_led) => PayloadBody {
device: window_led.device_id.clone(),
model: window_led.model.clone(),
cmd: command,
},
}
}