-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
77 lines (68 loc) · 2 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
let offStateColor = 'ff4444';
let onStateColor = '#22ff77';
let loadingStateColor = '#ffdd66';
let currentEffect = 'flat';
let makeSwitch = function(device, status) {
if (status == 0) {
$(device).find('.image svg g path').css({fill: offStateColor});
} else if (status == 1) {
$(device).find('.image svg g path').css({fill: onStateColor});
} else {
$(device).find('.image svg g path').css({fill: loadingStateColor});
}
};
let nextState = function(currentState) {
if (currentState == 1) {
return 0;
} else {
return 1;
}
};
let checkDeviceStatus = function() {
$.get('http://' + $('#mcuip').val() + '/devices', function(data) {
if (data) {
data.forEach(function(d) {
makeSwitch(d.device, d.status);
});
}
});
};
$('.device').on('click', function() {
let currentDevice = $(this);
let data = {};
data.device = currentDevice.data('device');
data.status = nextState(currentDevice.data('state'));
makeSwitch(this, 1);
$.get('http://' + $('#mcuip').val() + '/devices', data, function(response) {
makeSwitch(response.device, response.state);
});
});
$('#effects-list .item').on('click', function(){
currentEffect = $(this).data('effect');
$('#effects-list .item').removeClass('active');
$(this).addClass('active');
})
$('.color').on('click', function() {
let currentColor = $(this);
let colorRGB = {};
colorRGB.r = currentColor.data('r');
colorRGB.g = currentColor.data('g');
colorRGB.b = currentColor.data('b');
colorRGB.effect = currentEffect;
$.get('http://' + $('#mcuip').val() + '/strip', colorRGB, function(response) {
console.log('Color changed !');
});
});
$('#check-device-status').on('click', function() {
checkDeviceStatus();
});
$('#connect-device').on('click', function() {
checkDeviceStatus();
});
$('#turn-off-devices').on('click', function() {
$.get('http://' + $('#mcuip').val() + '/devices?device=all&status=0', function(data) {
data.forEach(function(d) {
makeSwitch(d.device, d.status);
});
});
});