-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
158 lines (130 loc) · 3.72 KB
/
app.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
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
// Settings to show the logs in the UI
var debug = false;
// Constants
var ACCESS_TOKEN = "1cbecd9ffc3dce4352254089a1ca360f8bf99012"
var DEVICE_ID = "190029001247353236343033"
$(document).ready(function(){
// Show the spinner
$(".spinner").fadeIn();
// Hide the rgbValues from the UI if you're not debugging
if(!debug){
$('.debug').hide()
}
})
// Get and listen to state changes from Firebase Database to sync with the UI's background
firebase.database().ref().child("devices").child(DEVICE_ID).on('value', function syncBackground(snap){
// Current state of the device
var command = snap.val();
// Get the color of the command button
if(command == "off") {
}
if(command !== 'play'){
var color = $('.' + command).css('background-color')
$('body').css('background-color', color)
}
if(command == "play") {
//
} else if(command == "off") {
$('body').css('background-color', "#222");
} else {
var color = button.css('background-color')
$('body').css('background-color', color)
}
// Hide the spinner
$(".spinner").hide();
});
var interval;
// Change the color upon click
$('.color').click(function(){
/*
* Gets the command from the date-command attribute of the clicked button
* Command values could be play, red, green, blue, yellow, and off
*/
if(!interval)
clearInterval(interval);
var command = this.attributes['data-command'].value;
// Show the spinner
if(command == "play" || command == "off"){
var spinner = $('.spinner')
spinner.fadeIn();
} else {
var insideSpinner = $(this).find('.inside-spinner')
insideSpinner.fadeIn();
}
if(command == "play") {
var i = 1;
interval = setInterval(function() {
switch(i) {
case 1:
$("body").css({"background-color": "#4885ed"});
break;
case 2:
$("body").css({"background-color": "#db3236"});
break;
case 3:
$("body").css({"background-color": "#f4c20d"});
break;
case 4:
$("body").css({"background-color": "#4885ed"});
break;
case 5:
$("body").css({"background-color": "#3cba54"});
break;
case 6:
$("body").css({"background-color": "#db3236"});
break;
default:
clearInterval(interval);
}
i++;
}, 1000);
} else if(command == "off") {
$('body').css('background-color', "#222");
} else {
var color = button.css('background-color')
$('body').css('background-color', color)
}
// Inline function to hide spinner
function hideSpinner(){
// Hide the spinner
if(command == "play" || command == "off"){
spinner.hide();
} else {
insideSpinner.hide();
}
}
// Build the URL with the DEVICE_ID and the ACCESS_TOKEN
var url = "https://api.particle.io/v1/devices/"
+ DEVICE_ID + "/strip?access_token=" + ACCESS_TOKEN;
// Change the background color of the body
var button = $(this)
// Send an AJAX POST request to particle
$.ajax({
url: url,
data: {
args: command
},
method: 'POST',
timeout: 10000,
success: function() {
},
error: function(response){
// Hide the spinner
hideSpinner();
alert("Sorry, we lost connection with the device.")
}
})
// After the AJAX request
.done(function(response){
// Check if the device is connected
if(response.connected == true){
// Save device's current state to Firebase
firebase.database().ref("devices/"+DEVICE_ID).set(command).then(function(){
console.log('State saved to database')
})
} else {
alert("Sorry, we lost connection with the device.")
}
hideSpinner()
})
})