-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create initial hue philip web api auth js skeleton
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} | ||
|