-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRockerObjects.js
More file actions
113 lines (98 loc) 路 3.6 KB
/
Copy pathRockerObjects.js
File metadata and controls
113 lines (98 loc) 路 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class RockerConfig {
constructor(authLocation, authAttempts, authTimeout) {
this.authLocation = authLocation;
this.authAttempts = authAttempts;
this.authTimeout = authTimeout;
}
}
class RockerAuthenticator {
constructor(networker) {
this.networker = networker;
}
authenticate(callback) {
var checkIfAuthenticated = function(authBlock, successCallback, failureCallback, numberOfAttempts) {
};
checkIfAuthenticated = function(authBlock, successCallback, failureCallback, numberOfAttempts) {
authBlock(function(result) {
rocker_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 = this.networker;
checkIfAuthenticated(function(aCallback) {
networker.authenticateWithBridge(aCallback);
}, function(username) {
callback(username);
}, function() {
callback(null);
}, networker.config.authAttempts); // check n times
}
}
class RockerNetworker {
constructor(config){
this.config = config;
}
authenticateWithBridge(callback) {
var authLocation = this.config.authLocation + "/api/";
var authBody = {"devicetype" : "rocker#debug julian"};
setTimeout(function() {
$.ajax({
url: authLocation,
method: "POST",
data: JSON.stringify(authBody),
complete: function (data) {
callback(data);
}
});
}, this.config.authTimeout);
}
toggleLights(lightsOnOrOffState, bridgeUsername, xyVal) {
var lightsOffLocation = this.config.authLocation + "/api/" + bridgeUsername + "/groups/3/action";
rocker_log("Location = " + lightsOffLocation);
var lightsOffBody = {"on": lightsOnOrOffState, "xy": xyVal};
var lightsOffString = JSON.stringify(lightsOffBody);
rocker_log("Body = " + lightsOffBody + " String = " + lightsOffString);
$.ajax({
url: lightsOffLocation,
data: lightsOffString,
method: "PUT",
complete: function (data) {
rocker_log("Attempted to toggle lights! " + data);
}
});
}
turnOnColorLoop(bridgeUsername) {
var colorLoopLocation = this.config.authLocation + "/api/" + bridgeUsername + "/groups/3/action";
var colorLoopBody = {"on":true,"hue": 25500,"effect":"colorloop"};
var colorLoopString = JSON.stringify(colorLoopBody);
$.ajax({
url: colorLoopLocation,
data: colorLoopString,
method: "PUT",
complete: function (data) {
rocker_log("Attempted to enable colorloop! " + data);
}
});
}
pressLinkButton(bridgeUsername) {
var linkButtonLocation = this.config.authLocation + "/api/" + bridgeUsername + "/config";
var linkButtonBody = {"linkbutton": true};
var linkButtonString = JSON.stringify(linkButtonBody);
$.ajax({
url: linkButtonLocation,
data: linkButtonString,
method: "PUT",
complete: function (data) {
rocker_log("Attempted to press link button! " + data);
}
});
}
}