Skip to content

Commit

Permalink
create initial hue philip web api auth js skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
insanj authored Sep 13, 2018
1 parent 4433f61 commit 4597a1e
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions rocker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

function authenticate() {
var rocker = new Rocker();
rocker.authenticate(function() {
rocker.turnLightsOff();
});
}



class Rocker {
var bridgeIPAddress = "192.168.0.101";
var bridgeUsername = "";

function authenticate(callback) {
authenticateWithBridge(function(result) {
console.log("Sent auth request! User needs to press button -- " + result);
});

setTimeout(function() {
authenticateWithBridge(function(result) {
console.log("Checked if user pressed button, result -- " + result);
var parsedUsername = result["success"]["username"];
if (parsedUsername) {
bridgeUsername = authResult["success"]["username"];
callback();
}
});
}, 500); // wait 1/2 a sec

function authenticateWithBridge(callback) {
$.ajax({
url: bridgeIPAddress + "/api/",
method: "POST",
always: function (data) {
callback(data);
}
});
}

function turnLightsOff() {
$.ajax({
url: bridgeIPAddress + "/api/" + bridgeUsername + "/groups/0/action",
data: {"on":false},
method: "PUT",
always: function (data) {
callback(data);
}
});
}
}

0 comments on commit 4597a1e

Please sign in to comment.