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();
|
|
|
|
//console.log('tc=true call');
|
|
|
|
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();
|
|
|
|
//console.log('tc=false call');
|
|
|
|
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;
|
|
|
|
var url = `http://{{IP_ADDRESS}}/?bri${x}=${value}`;
|
|
|
|
fetch(url).then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
}
|
|
|
|
//console.log(`Sent slider value ${value} to ${url}`);
|
|
|
|
}).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);
|
|
|
|
}
|