-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
428 lines (316 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!DOCTYPE html>
<html>
<head>
<title>Outdoor Lights</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.2/jquery.mobile.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jquerymobile/1.4.2/jquery.mobile.min.css" />
<script src="javascript-relative-time-helpers/date.extensions.js"></script>
<script src="json_formatter.js"></script>
<script type="text/javascript">
var statusUpdateInterval = 15000;
var lastStatus = {};
var lastFunctionInfo = {};
var API_URL = 'https://api.particle.io';
var ACCESS_TOKEN = '';
// jQuery mobile page init
$(document).on('pagecreate', function() {
show_load();
// Redirect to settings if access_token is missing
if(localStorage.access_token==undefined)
$.mobile.navigate('#settings');
else {
ACCESS_TOKEN = localStorage.access_token;
// Auto-append access_token header to Ajax requests
$.ajaxSetup({
headers: {
Authorization: 'Bearer '+ACCESS_TOKEN
}
});
// Kick off the function to populate everything
updateStatuses();
}
});
function updateStatuses() {
$.get(API_URL+'/v1/devices', function(resp) {
hide_load();
var filter = 'false';
if(resp.length>5)
filter = 'true';
var html = '<div><ul data-role="listview" data-filter="'+filter+'" data-inset="true">';
var icon = '';
var bgcolor = '';
var label = '';
var d;
if(JSON.stringify(resp)!=JSON.stringify(lastStatus)) {
for(var i in resp) {
if(resp[i].connected==true) {
icon = 'check';
bgcolor = '#005500';
} else {
icon = 'minus';
bgcolor = '#770000';
}
label = '<h2>'+resp[i].name+'</h2>';
if(resp[i].last_heard!=null) {
d = new Date(resp[i].last_heard);
label += '<p>Last seen '+d.toRelativeTime().toLowerCase()+'</p>';
//label += ' ('+d.toLocaleString()+')</p>';
}
html += '<li data-icon="'+icon+'"><a href="#core-attributes" data-coreid="'+resp[i].id+'" style="background:'+bgcolor+'">'+label+'</a></li>';
}
html += '</ul></div>';
$('#list').html($(html).enhanceWithin());
updateOverviewLinks();
lastStatus = resp;
}
$('#home h4').html('Last updated @ '+(new Date()).toLocaleTimeString());
window.setTimeout(updateStatuses, statusUpdateInterval);
});
}
function updateOverviewLinks() {
$('#home').find('ul li a').each(function() {
$(this).click(function() {
loadOverview($(this).data('coreid'));
});
});
}
function loadOverview(core_id) {
show_load();
// Get the core data that we already have
var core_info = {};
for(var i in lastStatus)
if(lastStatus[i].id==core_id)
core_info = lastStatus[i];
$('#core-attributes h1').html(core_info.name);
var footer_html = '';
if(core_info.last_heard!=null) {
d = new Date(core_info.last_heard);
footer_html = 'Last seen '+d.toRelativeTime().toLowerCase();
}
$('#attr-footer h4').html(footer_html);
// Fetch the core attributes
$.get(API_URL+'/v1/devices/'+core_id, function(resp) {
var html = '';
var filter = '';
// General info
$('#attr-core_name').text(resp.name);
$('#attr-core_id').text(resp.id);
if(resp.connected==true)
$('#attr-core_connected').text('Yes');
else
$('#attr-core_connected').text('No');
// Variables
filter = 'false';
if(resp.variables.count>5)
filter = 'true';
html = '<div><ul data-role="listview" data-filter="'+filter+'" data-inset="true">';
for(var i in resp.variables) {
html += '<li><a href="#attr-variable-popup" data-rel="popup" data-variable="'+i+'"> '+i+'<p class="ui-aside">'+resp.variables[i]+'</p></a></li>';
}
html += '</ul></div>';
$('#attr-variables').html($(html).enhanceWithin());
variablePopups(core_id);
// Functions
filter = 'false';
if(resp.functions.count>5)
filter = 'true';
html = '<div><ul data-role="listview" data-filter="'+filter+'" data-inset="true">';
for(var i in resp.functions) {
html += '<li><a href="#attr-function-popup" data-rel="popup" data-function="'+resp.functions[i]+'">'+resp.functions[i]+'</a></li>';
}
html += '</ul></div>';
$('#attr-functions').html($(html).enhanceWithin());
functionPopups(core_id);
hide_load();
});
}
function variablePopups(core_id) {
$('#attr-variables ul li a').each(function() {
$(this).click(function(e) {
e.preventDefault();
$.get(API_URL+'/v1/devices/'+core_id+'/'+$(this).data('variable'), function(resp) {
$('#attr-variable-popup h1').html(resp.name);
$('#attr-variable-popup div.ui-content').html('<pre>'+FormatJSON(resp)+'</pre>');
$('#attr-variable-popup').popup('open');
});
});
});
}
function functionPopups(core_id) {
$('#attr-functions ul li a').each(function() {
$(this).click(function(e) {
e.preventDefault();
var args = prompt('Please enter the function argument(s):');
lastFunctionInfo = {
name: $(this).text(),
"args": args
};
$.post(API_URL+'/v1/devices/'+core_id+'/'+$(this).data('function'), {"args":args}, function(resp) {
$('#attr-function-popup h1').html(lastFunctionInfo.name);
$('#attr-function-popup div.ui-content').html('<p>Argument(s): '+lastFunctionInfo.args+'</p><pre>'+FormatJSON(resp)+'</pre>');
$('#attr-function-popup').popup('open');
});
});
});
}
// Shortcut for $.mobile.loading.('show', ...)
function show_load() {
$.mobile.loading('show', {
textonly: false,
textvisible: false,
msgtext: '',
html: ''
});
}
// Shortcut for $.mobile.loading('hide');
function hide_load() {
$.mobile.loading('hide');
}
</script>
</head>
<body>
<div data-role="page" data-theme="b" id="home">
<!-- Header -->
<div data-role="header" data-position="fixed">
<!-- Settings Button -->
<button class="ui-btn-left ui-btn ui-btn-b ui-btn-inline ui-mini ui-corner-all ui-btn-icon-left ui-icon-gear ui-btn-icon-notext" onclick="$.mobile.navigate('#settings')"></button>
<h1>Control Panel</h1>
<!-- Refresh button -->
<button class="ui-btn-right ui-btn ui-btn-b ui-btn-inline ui-mini ui-corner-all ui-btn-icon-right ui-icon-refresh ui-btn-icon-notext" onclick="window.location.reload()"></button>
</div>
<!-- Main content body -->
<div role="main" class="ui-content">
<h3>Cores</h3>
<div id="list">
<ul data-role="listview" data-filter="false" data-inset="true">
<li>Loading . . .</li>
</ul>
</div>
</div>
<!-- Footer -->
<div data-role="footer" data-position="fixed"><h4>...</h4></div>
</div>
<script type="text/javascript">
$(document).on('pagecreate', function() {
// Auto-populate e-mail address and access token
if(localStorage.email!=undefined)
$('#username').val(localStorage.username);
if(localStorage.access_token!=undefined)
$('#access_token').val(localStorage.access_token);
$('#login').click(function(e) {
e.preventDefault();
$.post(API_URL+'/oauth/token', {
username: $('#username').val(),
password: $('#password').val(),
grant_type: 'password',
client_id: 'DEADBEEF',
client_secret: 'spark_core_mobile_control_panel'
}, function(resp) {
if(resp.access_token!=undefined) {
localStorage.access_token = resp.access_token;
localStorage.username = $('#username').val();
$('#access_token').val(localStorage.access_token);
$.mobile.navigate('#home');
}
});
});
$('#logout').click(function(e) {
e.preventDefault();
localStorage.removeItem('access_token');
localStorage.removeItem('username');
window.location.reload();
});
});
</script>
<div data-role="page" data-theme="b" id="settings">
<!-- Header -->
<div data-role="header" data-position="fixed">
<button class="ui-btn-left ui-btn ui-btn-b ui-btn-inline ui-mini ui-corner-all ui-btn-icon-left ui-icon-back ui-btn-icon-notext" onclick="$.mobile.navigate('#home')"></button>
<h1>Settings</h1>
<!-- Log out button -->
<button class="ui-btn-right ui-btn ui-btn-b ui-btn-inline ui-mini ui-corner-all ui-btn-icon-right ui-icon-user" id="logout">Log Out</button>
</div>
<!-- Main content body -->
<div role="main" class="ui-content">
<h3>E-mail Address and Password</h3>
<form>
<input type="email" id="username" placeholder="E-mail Address">
<input type="password" id="password" placeholder="Password">
<button id="login">Log In</button>
</form>
<h3>Access Token</h3>
<form>
<input type="text" id="access_token" placeholder="Access Token">
<button id="update_access_token">Update</button>
</form>
</div>
<!-- Footer -->
<div data-role="footer" data-position="fixed"></div>
</div>
<div data-role="page" data-theme="b" id="core-attributes">
<!-- Header -->
<div data-role="header" data-position="fixed" data-add-back-btn="true">
<h1>...</h1>
</div>
<!-- Main content body -->
<div role="main" class="ui-content">
<!-- General core info -->
<h3>General Information</h3>
<ul data-role="listview" data-inset="true">
<!-- Core Name -->
<li class="ui-field-contain">
<div class="ui-grid-a">
<div class="ui-block-a">Name:</div>
<div class="ui-block-b" id="attr-core_name"></div>
</div>
</li>
<!-- Core ID -->
<li class="ui-field-contain">
<div class="ui-grid-a">
<div class="ui-block-a">ID:</div>
<div class="ui-block-b" id="attr-core_id"></div>
</div>
</li>
<!-- Core connected -->
<li class="ui-field-contain">
<div class="ui-grid-a">
<div class="ui-block-a">Connected?</div>
<div class="ui-block-b" id="attr-core_connected"></div>
</div>
</li>
</ul>
<!-- Variables -->
<h3>Variables</h3>
<div id="attr-variables"></div>
<!-- Variable Pop-up -->
<div data-role="popup" id="attr-variable-popup" class="ui-content" data-overlay-theme="b" data-theme="a" data-dismissable="false" style="padding:0">
<div data-role="header" data-theme="b"><h1></h1></div>
<div data-role="main" class="ui-content" data-theme="a" style="overflow:auto"></div>
<div data-role="footer" data-theme="b" id="attr-variable-footer">
<button class="ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-delete ui-btn-inline" onclick="$('#attr-variable-popup').popup('close')">Close</button>
</div>
</div>
<!-- Functions -->
<h3>Functions</h3>
<div id="attr-functions"></div>
<!-- Function Pop-up -->
<div data-role="popup" id="attr-function-popup" class="ui-content" data-overlay-theme="b" data-theme="a" data-dismissable="false" style="padding:0">
<div data-role="header" data-theme="b"><h1></h1></div>
<div data-role="main" class="ui-content" data-theme="a" style="overflow:auto"></div>
<div data-role="footer" data-theme="b" id="attr-variable-footer">
<button class="ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-delete ui-btn-inline" onclick="$('#attr-function-popup').popup('close')">Close</button>
</div>
</div>
</div>
<!-- Footer -->
<div data-role="footer" data-position="fixed" id="attr-footer">
<h4>...</h4>
</div>
</div>
</body>
</html>