Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/gmallios/huest
Browse files Browse the repository at this point in the history
  • Loading branch information
gmallios committed Jul 31, 2022
2 parents a8066db + 4c34660 commit e81fee4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
30 changes: 11 additions & 19 deletions src/hue_api/hue_config_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,22 @@ impl HueConfigController {
self.device_map = device_list;
}

pub fn is_link_button_pressed(&self) -> bool {
// TODO: Fix this mess
// let unix_timestamp = SystemTime::now()
// .duration_since(UNIX_EPOCH)
// .unwrap()
// .as_secs();
// let millis_ellapsed = unix_timestamp - &self.bridge_config.linkbutton.lastlinkbuttonpushed;

// self.bridge_config.linkbutton.pressed = false;

// if ((millis_ellapsed as i64) / 1000) <= 30 {
// self.bridge_config.linkbutton.pressed = true;
// }
pub fn is_link_button_pressed(&mut self) -> bool {
let timestamp = Utc::now().timestamp();
let millis_ellapsed = timestamp - &self.bridge_config.linkbutton.lastlinkbuttonpushed;

self.bridge_config.linkbutton.pressed = false;

if (millis_ellapsed <= 30) {
self.bridge_config.linkbutton.pressed = true;
}
self.bridge_config.linkbutton.pressed
}

pub fn press_link_button(&mut self) {
let unix_timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
self.bridge_config.linkbutton.lastlinkbuttonpushed = unix_timestamp;
let timestamp = Utc::now().timestamp();
self.bridge_config.linkbutton.lastlinkbuttonpushed = timestamp;
self.bridge_config.linkbutton.pressed = true;
println!("Link button pressed");
}

pub fn add_user(&mut self, devicetype: &str, generate_client_key: &Option<bool>) -> (String, Option<String>) {
Expand Down
2 changes: 1 addition & 1 deletion src/hue_api/hue_routes/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn create_user(
api_state: SharedController,
) -> impl Responder {
let resp: String;
if api_state.get_controller_read().is_link_button_pressed() == false {
if api_state.get_controller_write().is_link_button_pressed() == false {
// 101 Error - Link button not pressed
// TODO: Define error codes with messages
// TODO: Implement macro for error response
Expand Down
4 changes: 2 additions & 2 deletions src/hue_api/hue_routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ async fn is_link_button_pressed(
) -> String {
println!(
"is_link_button_pressed {} ",
api_state.get_controller_read().is_link_button_pressed()
api_state.get_controller_write().is_link_button_pressed()
);
if api_state.get_controller_read().is_link_button_pressed() {
if api_state.get_controller_write().is_link_button_pressed() {
"Link button pressed".to_string()
} else {
"Link button not pressed".to_string()
Expand Down
2 changes: 1 addition & 1 deletion src/hue_api/hue_types/Config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct BridgeConfig {

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct LinkButton {
pub lastlinkbuttonpushed: u64,
pub lastlinkbuttonpushed: i64,
pub pressed: bool
}

Expand Down

0 comments on commit e81fee4

Please sign in to comment.