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

Refactor office on and off #63

Merged
merged 3 commits into from
Sep 13, 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
6 changes: 3 additions & 3 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions 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.9.0"
version = "0.10.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -12,8 +12,8 @@ reqwest = {version = "0.11.20", features = ["blocking", "json"]}
serde = {version = "1.0.188", features = ["derive"]}
serde_json = "1.0.106"
lazy_static = "1.4.0"
govee-api = {version = "1.2.0"}
# govee-api = {version = "1.2.0", path = "../../govee-api.git/bulk_control_device"}
govee-api = {version = "1.3.0"}
# govee-api = {version = "1.3.0", path = "../../govee-api.git/add_clone_derive_to_PayloadBody/"}

[dependencies.rocket]
version = "0.5.0-rc.1"
Expand Down
59 changes: 25 additions & 34 deletions src/routes/office_routes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use govee_api::GoveeClient;
use govee_api::structs::govee::PayloadBody;
use rocket::serde::json::Json;

use crate::constants::enums::OfficeDevices;
Expand All @@ -8,49 +9,39 @@ use crate::GOVEE_API_KEY;

#[get("/on")]
pub async fn office_on_handler(_token: Token) -> Json<serde_json::Value> {
let corner_led = OfficeDevices::corner_led();
let table_led = OfficeDevices::table_led();
let window_led = OfficeDevices::window_led();
let board_led = OfficeDevices::board_led();
let payload_corner = office_light_setup(&corner_led, "on");
let payload_table = office_light_setup(&table_led, "on");
let payload_window = office_light_setup(&window_led, "on");
let payload_board = office_light_setup(&board_led, "on");
let devices = [
OfficeDevices::corner_led(),
OfficeDevices::table_led(),
OfficeDevices::window_led(),
OfficeDevices::board_led(),
];
let payloads: Vec<PayloadBody> = devices
.iter()
.map(|device| office_light_setup(device, "on"))
.collect();

let govee_client = GoveeClient::new(&GOVEE_API_KEY);
let resuts = govee_client
.bulk_control_devices(vec![
payload_corner,
payload_table,
payload_window,
payload_board,
])
.await;
if let Err(err) = resuts {
if let Err(err) = govee_client.bulk_control_devices(payloads.clone()).await {
panic!("Error occurred: {:?}", err);
}
Json(serde_json::json!({"device": "all", "status": "on"}))
}

#[get("/off")]
pub async fn office_off_handler(_token: Token) -> Json<serde_json::Value> {
let corner_led = OfficeDevices::corner_led();
let table_led = OfficeDevices::table_led();
let window_led = OfficeDevices::window_led();
let board_led = OfficeDevices::board_led();
let payload_corner = office_light_setup(&corner_led, "off");
let payload_table = office_light_setup(&table_led, "off");
let payload_window = office_light_setup(&window_led, "off");
let payload_board = office_light_setup(&board_led, "off");
let devices = [
OfficeDevices::corner_led(),
OfficeDevices::table_led(),
OfficeDevices::window_led(),
OfficeDevices::board_led(),
];
let payloads: Vec<PayloadBody> = devices
.iter()
.map(|device| office_light_setup(device, "off"))
.collect();

let govee_client = GoveeClient::new(&GOVEE_API_KEY);
let resuts = govee_client
.bulk_control_devices(vec![
payload_corner,
payload_table,
payload_window,
payload_board,
])
.await;
if let Err(err) = resuts {
if let Err(err) = govee_client.bulk_control_devices(payloads.clone()).await {
panic!("Error occurred: {:?}", err);
}
Json(serde_json::json!({"device": "all", "status": "on"}))
Expand Down