113 lines
3.4 KiB
JavaScript
113 lines
3.4 KiB
JavaScript
//
|
|
|
|
// Nach dem Laden des Dokumentes:
|
|
$(document).ready(function() {
|
|
console.log("Es geht los");
|
|
|
|
const URL = "switch/";
|
|
const POLL_INTERVAL_MS = 1000;
|
|
const queryToken = new URLSearchParams(window.location.search).get('token');
|
|
const storedToken = localStorage.getItem('switchApiToken');
|
|
const API_TOKEN = queryToken || storedToken || '';
|
|
let interval = null;
|
|
let status = "";
|
|
|
|
if (queryToken) {
|
|
localStorage.setItem('switchApiToken', queryToken);
|
|
}
|
|
|
|
|
|
$('#versn').html("V " + VERSION + ' ' + VDATE); // Vesion anzeigen
|
|
|
|
|
|
$("#auszeile").hide(); // Meldezeile AUS schalten
|
|
|
|
// sendCommand(URL,"PowerOnState%200"); // OFF bei Power ON
|
|
// sendCommand(URL,"PulseTime%20"+(brenndauer+100)); // Brenndauer einstellen
|
|
|
|
startPolling();
|
|
|
|
document.addEventListener('visibilitychange', function (event) {
|
|
if (!document.hidden) {
|
|
startPolling();
|
|
} else {
|
|
stopPolling();
|
|
}
|
|
});
|
|
|
|
|
|
sendCommand(URL, "get_status")
|
|
|
|
$("#schalter").click(function () {
|
|
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);
|
|
let endpoint = url + cmnd;
|
|
if (API_TOKEN) {
|
|
endpoint += '?token=' + encodeURIComponent(API_TOKEN);
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
$.getJSON(endpoint)
|
|
.done(function (data) {
|
|
console.log("gekommen: ", data);
|
|
resolve(data);
|
|
})
|
|
.fail(function (_jqXHR, textStatus, errorThrown) {
|
|
const msg = errorThrown || textStatus || "Unbekannter Fehler";
|
|
reject(msg);
|
|
});
|
|
});
|
|
}
|
|
|
|
function startPolling() {
|
|
if (interval !== null) {
|
|
clearInterval(interval);
|
|
}
|
|
interval = setInterval(sendTimedCommand, POLL_INTERVAL_MS);
|
|
}
|
|
|
|
function stopPolling() {
|
|
if (interval !== null) {
|
|
clearInterval(interval);
|
|
interval = null;
|
|
}
|
|
}
|
|
|
|
function sendTimedCommand() {
|
|
// console.log("time check");
|
|
sendCommand(URL, 'check')
|
|
.then(data => {
|
|
// console.log("Timed Status= ", st);
|
|
status = data.relais;
|
|
if (status !== 'pending') {
|
|
if (status === 'ON') {
|
|
$('#schalter').html('Laufschrift <b>AUS</b> schalten');
|
|
$('#status').text('EIN');
|
|
$('#laufzeile').addClass('machrot');
|
|
if (data.offtime !== undefined) {
|
|
$('#auszeit').text(data.offtime);
|
|
$('#auszeile').show();
|
|
}
|
|
} else {
|
|
$('#schalter').html('Laufschrift <b>EIN</b> schalten');
|
|
$('#status').text('AUS');
|
|
$('#laufzeile').removeClass('machrot');
|
|
$('#auszeile').hide();
|
|
}
|
|
}
|
|
})
|
|
.catch(err => {
|
|
console.log('Statusabfrage fehlgeschlagen:', err);
|
|
});
|
|
}
|
|
|
|
}); |