2023-05-20 10:57:50 +02:00
|
|
|
function handleRequest(id, url) {
|
|
|
|
var button = document.getElementById(id);
|
2023-05-05 08:21:09 +02:00
|
|
|
var xhr = new XMLHttpRequest();
|
2023-05-20 10:57:50 +02:00
|
|
|
xhr.open('GET', url, true);
|
2023-05-07 16:57:24 +02:00
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("success");
|
2023-05-20 10:57:50 +02:00
|
|
|
button.innerHTML = (id === 'tc_on') ? "Enabled!" : "Disabled!";
|
2023-05-13 23:00:46 +02:00
|
|
|
button.style.backgroundColor = "green";
|
2023-05-07 16:57:24 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("success");
|
|
|
|
button.classList.add("pure-button-primary");
|
2023-05-20 10:57:50 +02:00
|
|
|
button.innerHTML = (id === 'tc_on') ? "ON" : "OFF";
|
2023-05-13 23:00:46 +02:00
|
|
|
button.style.backgroundColor = "";
|
2023-05-07 16:57:24 +02:00
|
|
|
}, 2000);
|
|
|
|
console.log('Data successfully sent to server!');
|
|
|
|
} else {
|
|
|
|
button.classList.remove("pure-button-primary");
|
|
|
|
button.classList.add("error");
|
|
|
|
button.innerHTML = "Error!";
|
2023-05-13 23:00:46 +02:00
|
|
|
button.style.backgroundColor = "";
|
2023-05-07 16:57:24 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
button.classList.remove("error");
|
|
|
|
button.classList.add("pure-button-primary");
|
2023-05-20 10:57:50 +02:00
|
|
|
button.innerHTML = (id === 'tc_on') ? "ON" : "OFF";
|
2023-05-13 23:00:46 +02:00
|
|
|
button.style.backgroundColor = "";
|
2023-05-07 16:57:24 +02:00
|
|
|
}, 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();
|
2023-05-07 16:57:24 +02:00
|
|
|
}
|
2023-05-20 10:57:50 +02:00
|
|
|
function addButtonClickListener(id, url) {
|
|
|
|
var link = document.getElementById(id);
|
|
|
|
link.addEventListener('click', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
handleRequest(id, url);
|
|
|
|
document.getElementById(id).classList.add('pure-button-primary');
|
|
|
|
document.getElementById(id === 'tc_on' ? 'tc_off' : 'tc_on').classList.remove('pure-button-primary');
|
2023-05-05 08:21:09 +02:00
|
|
|
});
|
2023-05-20 10:57:50 +02:00
|
|
|
}
|
|
|
|
addButtonClickListener('tc_on', 'http://{{IP_ADDRESS}}/?tc=true');
|
|
|
|
addButtonClickListener('tc_off', 'http://{{IP_ADDRESS}}/?tc=false');
|
2023-05-05 08:21:09 +02:00
|
|
|
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;
|
|
|
|
}
|