-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
61 lines (55 loc) · 1.81 KB
/
script.js
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
var host = 'http://photon1.internal.redmountainmakers.org';
function doRequest(method, path, cb) {
$.ajax({
url: host + path,
method: method,
dataType: 'json',
error: function(xhr, status, error) {
console.log('AJAX error', xhr, status, error);
cb(error || new Error('AJAX error'));
},
success: function(data, status, xhr) {
if (data.status === 'ok') {
console.log('AJAX success', data);
cb(null, data);
} else {
console.log('Device error', data);
cb(new Error(data.message));
}
}
});
}
$(function() {
if (/embedded=true/.test(document.location.search)) {
$('body').addClass('embedded');
}
doRequest('GET', '/', function(err, data) {
if (err) {
// Try again for good measure
doRequest('GET', '/', function(err, data) {
if (err) {
// This is looking more like a real failure
$('#error').show();
$('#status-container, #controls').hide();
} else {
$('#status').html(data.lights);
}
});
} else {
$('#status').html(data.lights);
}
});
$('#controls').on('click', '.control', function() {
doRequest('POST', $(this).data('route'), function(err, data) {
if (err) {
// There are some intermittent errors here, so don't hide the controls
$('#error').show();
$('#status-container').hide();
} else {
$('#error').hide();
$('#status-container').show();
$('#status').html(data.lights);
}
});
});
});