2023-05-05 08:21:09 +02:00
|
|
|
var links = document.querySelectorAll('[id^="tc_on"]');
|
|
|
|
links.forEach(function(link) {
|
|
|
|
link.addEventListener('click', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', 'http://{{IP_ADDRESS}}/?tc=true', true);
|
2023-05-07 16:57:24 +02:00
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
var button = document.getElementById("tc_on");
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("success");
|
|
|
|
button.innerHTML = "Enabled!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("success");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "ON";
|
|
|
|
}, 2000);
|
|
|
|
console.log('Data successfully sent to server!');
|
|
|
|
} else {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("error");
|
|
|
|
button.innerHTML = "Error!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("error");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "ON";
|
|
|
|
}, 2000);
|
2023-05-08 09:46:25 +02:00
|
|
|
showToast('Error while sending data to server.', 'error');
|
2023-05-07 16:57:24 +02:00
|
|
|
console.log('Error while sending data to server.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-05-05 08:21:09 +02:00
|
|
|
xhr.send();
|
|
|
|
document.getElementById('tc_on').classList.add('pure-button-primary');
|
|
|
|
document.getElementById('tc_off').classList.remove('pure-button-primary');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
var links = document.querySelectorAll('[id^="tc_off"]');
|
|
|
|
links.forEach(function(link) {
|
|
|
|
link.addEventListener('click', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', 'http://{{IP_ADDRESS}}/?tc=false', true);
|
2023-05-07 16:57:24 +02:00
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
var button = document.getElementById("tc_off");
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("success");
|
|
|
|
button.innerHTML = "Disabled!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("success");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "OFF";
|
|
|
|
}, 2000);
|
|
|
|
console.log('Data successfully sent to server!');
|
|
|
|
} else {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("error");
|
|
|
|
button.innerHTML = "Error!";
|
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("error");
|
|
|
|
button.classList.add("pure-button-primary");
|
|
|
|
button.innerHTML = "OFF";
|
|
|
|
}, 2000);
|
2023-05-08 09:46:25 +02:00
|
|
|
showToast('Error while sending data to server.', 'error');
|
2023-05-07 16:57:24 +02:00
|
|
|
console.log('Error while sending data to server.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-05-05 08:21:09 +02:00
|
|
|
xhr.send();
|
|
|
|
document.getElementById('tc_off').classList.add('pure-button-primary');
|
|
|
|
document.getElementById('tc_on').classList.remove('pure-button-primary');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
let timeoutId;
|
|
|
|
function sendSliderValue(x) {
|
|
|
|
x = x - 1;
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
timeoutId = setTimeout(() => {
|
|
|
|
var value = document.getElementById(`bri${x}`).value;
|
2023-05-10 10:48:50 +02:00
|
|
|
var trans = document.getElementById('transition').value;
|
|
|
|
if (isNaN(trans) || parseInt(trans) != trans)
|
|
|
|
{
|
|
|
|
showToast("Error the transition is not a valid integer number.", "error");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var url = `http://{{IP_ADDRESS}}/?bri${x}=${value}&transition=`+parseInt(trans);
|
2023-05-05 08:21:09 +02:00
|
|
|
fetch(url).then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
}
|
|
|
|
}).catch(error => {
|
2023-05-08 09:46:25 +02:00
|
|
|
showToast(`Error sending slider value to ${url}: ${error}`, 'error');
|
2023-05-05 08:21:09 +02:00
|
|
|
console.error(`Error sending slider value to ${url}: ${error}`);
|
|
|
|
});
|
|
|
|
}, 500);
|
2023-05-10 13:33:03 +02:00
|
|
|
}
|
|
|
|
function rgb2Hex(r, g, b) {
|
|
|
|
var hex = "#" + ((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1);
|
|
|
|
return hex;
|
|
|
|
}
|