So siehts ganz gut aus !
This commit is contained in:
@@ -5,12 +5,11 @@ $(document).ready(function() {
|
||||
console.log("Es geht los");
|
||||
|
||||
const URL = "switch/";
|
||||
const brenndauer = 300; // Dauer der Anzeige in Sekunden
|
||||
const checktime = 5000000; // alle 30sec Zustand prüfen
|
||||
|
||||
let status = "";
|
||||
let tr;
|
||||
let interval;
|
||||
|
||||
|
||||
$('#versn').html("V " + VERSION + ' ' + VDATE); // Vesion anzeigen
|
||||
|
||||
@@ -20,37 +19,32 @@ $(document).ready(function() {
|
||||
// sendCommand(URL,"PowerOnState%200"); // OFF bei Power ON
|
||||
// sendCommand(URL,"PulseTime%20"+(brenndauer+100)); // Brenndauer einstellen
|
||||
|
||||
let interval = setInterval(sendTimedCommand, 1000); // alle Sekunde pollen
|
||||
|
||||
sendCommand(URL,"get_status")
|
||||
.then(st => {
|
||||
console.log("Status = ",st);
|
||||
checkStatus(st);
|
||||
});
|
||||
|
||||
$("#schalter").click(function() {
|
||||
let message;
|
||||
clearTimeout(interval);
|
||||
if (status == 'ON') {
|
||||
console.log("Sende OFF")
|
||||
sendCommand(URL,"switch_off")
|
||||
.then(st => {
|
||||
console.log("Status = ", st);
|
||||
checkStatus(st);
|
||||
interval = setInterval(sendTimedCommand,5000);
|
||||
|
||||
});
|
||||
document.addEventListener('visibilitychange', function (event) {
|
||||
if (!document.hidden) {
|
||||
interval = setInterval(sendTimedCommand, 1000);
|
||||
} else {
|
||||
console.log("Sende ON")
|
||||
sendCommand(URL,"switch_on")
|
||||
.then(st => {
|
||||
console.log("Status = ",st);
|
||||
checkStatus(st);
|
||||
interval = setInterval(sendTimedCommand,5000);
|
||||
});
|
||||
clearTimeout(interval); // The page is hidden.
|
||||
}
|
||||
});
|
||||
|
||||
function sendCommand(url,cmnd) {
|
||||
|
||||
sendCommand(URL, "get_status")
|
||||
|
||||
$("#schalter").click(function () {
|
||||
let message;
|
||||
if (status == 'ON') {
|
||||
console.log("Sende OFF")
|
||||
sendCommand(URL, "switch_off")
|
||||
} else {
|
||||
console.log("Sende ON")
|
||||
sendCommand(URL, "switch_on")
|
||||
}
|
||||
});
|
||||
|
||||
function sendCommand(url, cmnd) {
|
||||
console.log("sendCommand", cmnd);
|
||||
const p = new Promise((resolve, reject) => {
|
||||
$.getJSON(url + cmnd, function (data, err) { // AJAX Call
|
||||
if (err != 'success') {
|
||||
@@ -58,7 +52,7 @@ $(document).ready(function() {
|
||||
alert("Fehler <br />" + err); // if error, show it
|
||||
} else {
|
||||
console.log("gekommen: ", data);
|
||||
resolve(data.status);
|
||||
resolve(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -66,32 +60,27 @@ $(document).ready(function() {
|
||||
}
|
||||
|
||||
function sendTimedCommand() {
|
||||
console.log("time check");
|
||||
sendCommand(URL,'check')
|
||||
.then(st => {
|
||||
console.log("Timed Status= ", st);
|
||||
status = st;
|
||||
if (st != 'pending') {
|
||||
// console.log("time check");
|
||||
sendCommand(URL, 'check')
|
||||
.then(data => {
|
||||
// console.log("Timed Status= ", st);
|
||||
status = data.relais;
|
||||
if (status != 'pending') {
|
||||
clearTimeout(tr);
|
||||
if (st == 'ON') {
|
||||
if (status == 'ON') {
|
||||
$('#schalter').html('Laufschrift <b>AUS</b> schalten');
|
||||
$('#status').text('EIN');
|
||||
$('#statuszeile').addClass('machrot');
|
||||
$('#laufzeile').addClass('machrot');
|
||||
$('#auszeit').text(data.offtime);
|
||||
$('#auszeile').show();
|
||||
} else {
|
||||
$('#schalter').html('Laufschrift <b>EIN</b> schalten');
|
||||
$('#status').text('AUS');
|
||||
$('#statuszeile').removeClass('machrot');
|
||||
$('#laufzeile').removeClass('machrot');
|
||||
$('#auszeile').hide();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function checkStatus(st) {
|
||||
if (st == 'pending') {
|
||||
tr = setTimeout(sendTimedCommand,500);
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user