var rawContent; var data = []; var update = []; var loading = false; var websock; var settings = { modOffset: 0, inputOffset: 0, updateInterval: 2, autoupdate: false, emulateMaster: false } var autoRefreshInterval; function GetData() { if (loading) return; loading = true; return fetch("/data") .then(function (result) { return result.text().then(function (text) { rawContent = text; data = ConvertData(rawContent); loading = false; }); }) .catch(function () { loading = false; }); } function ConvertData(rawContent) { let dataResult = []; var tmpList = rawContent.split('~'); tmpList.forEach(resultLine => { var tmpResultLine = resultLine.split(":"); var tmpModule = tmpResultLine[0].split("."); var dataObj = { module: parseInt(tmpModule[0]), input: parseInt(tmpModule[1]), data: (tmpResultLine[1] == 1) } if (!isNaN(dataObj.input) && !isNaN(dataObj.module)) dataResult.push(dataObj); }); return dataResult; } function RenderData(data) { var table = document.getElementById("overview"); table.innerHTML = ""; var lastModule = -1; var row; data.forEach(element => { if (lastModule != element.module) { lastModule = element.module row = table.insertRow(element.module); var title = row.insertCell(0); title.innerHTML = (element.module + settings.modOffset); } var cell = row.insertCell(element.input + 1); cell.innerHTML = (element.input + settings.inputOffset); cell.classList.add('input-value') cell.classList.add('input-value-' + ((element.data) ? "ON" : "OFF")) }); document.getElementById("lastUpdate").innerHTML = new Date().toLocaleString(); } function SaveSettings() { settings.modOffset = parseInt(document.getElementById("modOffset").value); settings.inputOffset = parseInt(document.getElementById("inputOffset").value); settings.updateInterval = parseInt(document.getElementById("updateInterval").value); settings.autoupdate = document.getElementById("checkoptions-0").checked settings.emulateMaster = document.getElementById("checkoptions-1").checked window.localStorage.setItem('settings', JSON.stringify(settings)); SetAutoUpdate(); } function LoadSettings(rawSettings) { if (rawSettings == null) { var tmpsettings = JSON.parse(window.localStorage.getItem('settings')); if (tmpsettings == null) return; settings = tmpsettings; } else { settings = rawSettings; } document.getElementById("modOffset").value = settings.modOffset; document.getElementById("inputOffset").value = settings.inputOffset; document.getElementById("updateInterval").value = settings.updateInterval; document.getElementById("checkoptions-0").checked = settings.autoupdate; document.getElementById("checkoptions-1").checked = settings.emulateMaster; SetAutoUpdate(); } function SaveSettingsOnDevice() { SaveSettings(); var urlEncodedSettings = encodeURI(JSON.stringify(settings)); var url = "/config?key=settings&value=" + urlEncodedSettings; fetch(url) .then(function (result) { result.text().then(function (text) { alert("Saved") }); }) .catch(function () { }); } function LoadSettingsFromDevice() { fetch("/config?key=settings") .then(function (result) { return result.text().then(function (text) { var settingsraw = JSON.parse(decodeURI(text)); LoadSettings(settingsraw); }); }) .catch(function () { loading = false; }); } function SetAutoUpdate() { if (autoRefreshInterval != null) clearInterval(autoRefreshInterval) autoRefreshInterval = setInterval(() => { if (settings.autoupdate) { if (loading) return; DoUpdate(); } }, settings.updateInterval * 1000); } function DoUpdate() { if (loading) return; GetData().then(() => { RenderData(data) }); } function StartSocket() { websock = new WebSocket('ws://' + window.location.hostname + ':81/'); websock.onopen = function (evt) { console.log('websock open'); }; websock.onclose = function (evt) { console.log('websock close'); }; websock.onerror = function (evt) { console.log(evt); }; websock.onmessage = function (evt) { console.log(evt); switch (evt.data) { case "update": DoUpdate(); break; default: var data = evt.data; update = data.split(";"); UpdateData(update); break; } }; } function UpdateData(update) { update.forEach(u => { if (u != '') { var tmp = u.split('|'); var module = tmp[0]; if (tmp[1] != null) { tmp = tmp[1].split(':'); var input = tmp[0]; if (tmp[1] != null) { var value = tmp[1]; data.find(d => d.module == module && d.input == input).data = (value == "1"); } } } }); RenderData(data); } LoadSettings(); DoUpdate(); StartSocket(); document.getElementById("button-save").addEventListener("click", SaveSettings); document.getElementById("button-save-device").addEventListener("click", SaveSettingsOnDevice); document.getElementById("button-load-device").addEventListener("click", LoadSettingsFromDevice); document.getElementById("button-update").addEventListener("click", DoUpdate);