Skip to content

Commit

Permalink
wireup rocker networker, main auth cycle, jquery ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
insanj committed Sep 13, 2018
1 parent a578338 commit b0d4e87
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 34 deletions.
5 changes: 4 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<html>
<body>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="jquery-3.3.1.min.js"></script>
<script src="rocker.js"></script>
<script>
rocker_main();
</script>

</body>
</html>
2 changes: 2 additions & 0 deletions jquery-3.3.1.min.js

Large diffs are not rendered by default.

112 changes: 79 additions & 33 deletions rocker.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,96 @@

function authenticate() {
var rocker = new Rocker();
rocker.authenticate(function() {
rocker.turnLightsOff();
function rocker_main() {
let config = new RockerConfig("http://192.168.0.101", 5, 5000);
let rocker = new Rocker(config);
rocker.authenticate(function(username) {
if (username == null) {
console.log("Tried to do that auth thing and failed :(");
} else {
console.log("Finished auth cycle!! :)");
rocker.toggleLights(true, username);
}
});
}


class RockerConfig {
constructor(authLocation, authAttempts, authTimeout) {
this.authLocation = authLocation;
this.authAttempts = authAttempts;
this.authTimeout = authTimeout;
}
}

class Rocker {
var bridgeIPAddress = "192.168.0.101";
var bridgeUsername = "";
constructor(config){
this.config = config;
}

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

var checkIfAuthenticated = function(authBlock, successCallback, failureCallback, numberOfAttempts) {
};

checkIfAuthenticated = function(authBlock, successCallback, failureCallback, numberOfAttempts) {
authBlock(function(result) {
console.log("Checked if user pressed button, result -- " + JSON.stringify(result, null));
if (result && result["responseJSON"] && result["responseJSON"][0] && result["responseJSON"][0]["success"] && result["responseJSON"][0]["success"]["username"]) {
successCallback(result["responseJSON"][0]["success"]["username"]); // username in response! continue...
} else {
if (numberOfAttempts-1>0) {
checkIfAuthenticated(authBlock, successCallback, failureCallback, numberOfAttempts-1);
} else {
failureCallback(); // unable to auth after attempts <= 0
}
}
});
};

var networker = new RockerNetworker(this.config);
checkIfAuthenticated(function(callback) {
networker.authenticateWithBridge(callback);
}, function(username) {
callback(username);
}, function() {
callback(null);
}, this.config.authAttempts); // check n times
}

toggleLights(lightsOnOrOffState, username) {
var networker = new RockerNetworker(this.config);
networker.toggleLights(lightsOnOrOffState, username);
}
}

class RockerNetworker {
constructor(config){
this.config = config;
}

authenticateWithBridge(callback) {
var authLocation = this.config.authLocation + "/api/";
var authBody = {"devicetype" : "rocker#debug julian"};
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();
$.ajax({
url: authLocation,
method: "POST",
data: JSON.stringify(authBody),
complete: function (data) {
callback(data);
}
});
}, 500); // wait 1/2 a sec

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

function turnLightsOff() {
$.ajax({
url: bridgeIPAddress + "/api/" + bridgeUsername + "/groups/0/action",
data: {"on":false},
toggleLights(lightsOnOrOffState, bridgeUsername) {
var lightsOffLocation = this.config.authLocation + "/api/" + bridgeUsername + "/groups/0/action/";
var lightsOffBody = {"on": lightsOnOrOffState};
$.ajax({
url: lightsOffLocation,
data: JSON.stringify(lightsOffBody),
method: "PUT",
always: function (data) {
callback(data);
complete: function (data) {
console.log("Attempted to toggle lights! " + data);
}
});
}
Expand Down

0 comments on commit b0d4e87

Please sign in to comment.