-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.html
304 lines (285 loc) · 10.9 KB
/
index.html
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!DOCTYPE html>
<html>
<head>
<title>Remote Particle</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<script type="text/javascript" src="http://codeorigin.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
var statusTimer = null;
var timer1, timer2, timer3, timer4;
var baseURL = "https://api.particle.io/v1/devices/";
//--------------------------------------------------------------
// START - EDITABLE USER DATA
//
var accessToken = "2345234523452345234523452345234523452345";
var deviceID = "123412341234123412341234";
// App Heading
var appHeading = "Remote Particle";
// Function 1
var funcKey1 = "start";
var args1 = "";
var label1 = "Start/Stop Engine"; // button label 1 (set to "" to disable)
// Function 2
var funcKey2 = "trunk";
var args2 = "";
var label2 = "Open/Close Trunk"; // button label 2 (set to "" to disable)
// Function 3
var funcKey3 = "lock";
var args3 = "";
var label3 = "Lock Doors"; // button label 3 (set to "" to disable)
// Function 4
var funcKey4 = "unlock";
var args4 = "";
var label4 = "Unlock Doors"; // button label 4 (set to "" to disable)
// Variable 1
var varKey1 = "startstate";
var var1onLabel = "Running!";
var var1offLabel = "Stopped.";
var var1onState = 1; // Set to null to allow raw value to be displayed.
var var1offState = 0; // Set to null to allow raw value to be displayed.
var refresh1 = 5000; // variable 1 refresh rate in milliseconds (set to 0 to disable)
// Variable 2
var varKey2 = "trunkstate";
var var2onLabel = "Open!";
var var2offLabel = "Closed.";
var var2onState = 1; // Set to null to allow raw value to be displayed.
var var2offState = 0; // Set to null to allow raw value to be displayed.
var refresh2 = 5000; // variable 2 refresh rate in milliseconds (set to 0 to disable)
// Variable 3
var varKey3 = "lockstate";
var var3onLabel = "Locked.";
var var3offLabel = "Unlocked!";
var var3onState = 1; // Set to null to allow raw value to be displayed.
var var3offState = 0; // Set to null to allow raw value to be displayed.
var refresh3 = 5000; // variable 3 refresh rate in milliseconds (set to 0 to disable)
// Variable 4
var varKey4 = "";
var var4onLabel = "";
var var4offLabel = "";
var var4onState = null; // Set to null to allow raw value to be displayed.
var var4offState = null; // Set to null to allow raw value to be displayed.
var refresh4 = 0; // variable 4 refresh rate in milliseconds (set to 0 to disable)
//
// END - EDITABLE USER DATA
//--------------------------------------------------------------
// Update app heading
$("#app-heading").html(appHeading);
// Override button styles
$(".btn-lg").css({"width":"60%"});
// Update button labels to code definitions
// or hide them if not defined.
(label1) ? $("#button-1").html(label1) : $("#button-1").hide();
(label2) ? $("#button-2").html(label2) : $("#button-2").hide();
(label3) ? $("#button-3").html(label3) : $("#button-3").hide();
(label4) ? $("#button-4").html(label4) : $("#button-4").hide();
// Override text input styles
$(".form-control").css({"display":"inline","width":"35%"});
// Hide variable text fields if not in use.
if(refresh1 === 0) $("#var-val-1").hide();
if(refresh2 === 0) $("#var-val-2").hide();
if(refresh3 === 0) $("#var-val-3").hide();
if(refresh4 === 0) $("#var-val-4").hide();
// Auto-refresh
// Turn on/off the variable refresh if refresh rates are defined
if(refresh1) {
$("#get-var-1").attr("disabled", "disabled");
timer1 = setInterval(function () {
getVariable1();
}, refresh1);
}
if(refresh2) {
$("#get-var-2").attr("disabled", "disabled");
timer2 = setInterval(function () {
getVariable2();
}, refresh2);
}
if(refresh3) {
$("#get-var-3").attr("disabled", "disabled");
timer3 = setInterval(function () {
getVariable3();
}, refresh3);
}
if(refresh4) {
$("#get-var-4").attr("disabled", "disabled");
timer4 = setInterval(function () {
getVariable4();
}, refresh4);
}
////
// Alerts
////
$("#info-alert").alert();
$("#info-alert").affix();
////
// Methods
////
function onMethodSuccess() {
alert = $("#info-alert");
alert.text("Success!").removeClass("alert-danger").addClass("alert-success");
if(statusTimer != null) {
clearTimeout(statusTimer);
alert.hide();
setTimeout(function() {
alert.show();
statusTimer = setTimeout(function() {
statusTimer = null;
alert.hide();
}, 1750);
}, 250);
}
else {
alert.show();
statusTimer = setTimeout(function() {
statusTimer = null;
alert.hide();
}, 2000);
}
}
function onMethodFailure(data) {
alert = $("#info-alert");
alert.text((data)?"Error! "+data:"Error!").removeClass("alert-success").addClass("alert-danger");
if(statusTimer != null) {
clearTimeout(statusTimer);
alert.hide();
setTimeout(function() {
alert.show();
statusTimer = setTimeout(function() {
statusTimer = null;
alert.hide();
}, 1750);
}, 250);
}
else {
alert.show();
statusTimer = setTimeout(function() {
statusTimer = null;
alert.hide();
}, 2000);
}
}
// The base level run method command
function doMethod(method, data) {
var url = baseURL + deviceID + "/" + method;
$.ajax({
type: "POST",
url: url,
data: {
access_token: accessToken,
args: data
},
dataType: "json"
}).success(function(obj) {
console.log(obj);
(obj.return_value && obj.return_value == 200) ? onMethodSuccess() : onMethodFailure((obj.error)?obj.error:"");
}).fail(function(obj) {
onMethodFailure();
});
}
$("#button-1").on("click", function () {
doMethod(funcKey1, args1);
});
$("#button-2").on("click", function () {
doMethod(funcKey2, args2);
});
$("#button-3").on("click", function () {
doMethod(funcKey3, args3);
});
$("#button-4").on("click", function () {
doMethod(funcKey4, args4);
});
////
// Variables
////
function getVariable(variable, callback) {
var url = baseURL + deviceID + "/" + variable + "?access_token=" + accessToken;
$.ajax({
url: url,
dataType: "json"
}).success(function(obj) {
console.log(obj);
(obj.coreInfo.deviceID && obj.coreInfo.deviceID == deviceID) ? onMethodSuccess() : onMethodFailure((obj.error)?obj.error:"");
callback(obj.result);
}).fail(function(obj) {
onMethodFailure();
});
}
// Get variable methods
function getVariable1() {
getVariable(varKey1, function (res) {
if(res === var1onState)
$("#var-val-1").val(var1onLabel);
else if(res === var1offState)
$("#var-val-1").val(var1offLabel);
else
$("#var-val-1").val(res);
});
}
function getVariable2() {
getVariable(varKey2, function (res) {
if(res === var2onState)
$("#var-val-2").val(var2onLabel);
else if(res === var2offState)
$("#var-val-2").val(var2offLabel);
else
$("#var-val-2").val(res);
});
}
function getVariable3() {
getVariable(varKey3, function (res) {
if(res === var3onState)
$("#var-val-3").val(var3onLabel);
else if(res === var3offState)
$("#var-val-3").val(var3offLabel);
else
$("#var-val-3").val(res);
});
}
function getVariable4() {
getVariable(varKey4, function (res) {
if(res === var4onState)
$("#var-val-4").val(var4onLabel);
else if(res === var4offState)
$("#var-val-4").val(var4offLabel);
else
$("#var-val-4").val(res);
});
}
});
</script>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title" id="app-heading">
Control
</h4>
</div>
<div id="buttons" class="panel">
<div class="panel-body">
<button type="button" class="btn btn-primary btn-lg" id="button-1">BUTTON 1</button>
<input type="text" class="form-control" value="---" readonly id="var-val-1"><br/><br/>
<button type="button" class="btn btn-primary btn-lg" id="button-2">BUTTON 2</button>
<input type="text" class="form-control" value="---" readonly id="var-val-2"><br/><br/>
<button type="button" class="btn btn-primary btn-lg" id="button-3">BUTTON 3</button>
<input type="text" class="form-control" value="---" readonly id="var-val-3"><br/><br/>
<button type="button" class="btn btn-primary btn-lg" id="button-4">BUTTON 4</button>
<input type="text" class="form-control" value="---" readonly id="var-val-4">
</div>
</div>
</div>
<div class="alert fade in" id="info-alert" hidden data-spy="affix"></div>
</div>
</body>
</html>