Browse Source

add automatic updates to the html page

Helmut Pozimski 5 years ago
parent
commit
bce724bd7b
2 changed files with 31 additions and 9 deletions
  1. 1 2
      html/index.html
  2. 30 7
      html/script.js

+ 1 - 2
html/index.html

@@ -4,7 +4,6 @@
 </head>
 <body class="container">
 <div id="deviceheader">
-blub
 </div>
 <div id="time">
 <div id="rtc-headline">
@@ -31,7 +30,7 @@ Alarm-Zeitpunkt:
 <textarea id="nvram-text"></textarea>
 <button id="nvbutton" onclick="submitNvRAM()">Absenden</button>
 </div>
-<div id="buttons"><button onclick="systoHC()">sync</button></div>
+<div id="buttons"><button onclick="systoHC()">sync</button><br><br><textarea rows="1" cols="2" id="intervaltext"></textarea><br><button onclick="configureIntervall()">Aktualisierungsintervall</button><br><button onclick="removeItervall()">Stopp</button></div>
 </body>
 <script src="script.js"></script>
 </html>

+ 30 - 7
html/script.js

@@ -1,7 +1,7 @@
 fetch("https://169.254.175.168:8888/device").then( function(text) { return text.text() } ).then( function(text2) { element = document.getElementById("deviceheader"); element.innerText = text2} )
 
 function getTime() {
-	fetch("https://169.254.175.168:8888/time"). then( function(text) { return text.text() }).then( function(text2) { timeobj = JSON.parse(text2); element = document.getElementById("rtc"); element.innerText = timeobj.mday + "." + (timeobj.month +1) + "." + (timeobj.year + 1900) + " " + addZero(timeobj.hour) + ":" + addZero(timeobj.minutes) + ":" + addZero(timeobj.seconds) })
+	fetch("https://169.254.175.168:8888/time"). then( function(text) { if(!text.ok) { throw text }; return text.text() }).then( function(text2) { timeobj = JSON.parse(text2); element = document.getElementById("rtc"); element.innerText = timeobj.mday + "." + (timeobj.month +1) + "." + (timeobj.year + 1900) + " " + addZero(timeobj.hour) + ":" + addZero(timeobj.minutes) + ":" + addZero(timeobj.seconds) }).catch(function(error) { console.log("failed to fetch the time") })
 }
 
 function addZero(num) {
@@ -18,7 +18,7 @@ function getSystemTime() {
 }
 
 function getAlarm() {
-	fetch("https://169.254.175.168:8888/wkalrm").then(function(text) {return text.text() }).then( function(text2) { alarmdiv = document.getElementById("alarm"); alarmdiv.style.visibility="visible"; alarmtime=document.getElementById("alarm-time"); alarmobj=JSON.parse(text2); alarmtime.innerText = alarmobj.mday + "." + (alarmobj.month +1) + "." + (alarmobj.year + 1900) + " " + addZero(alarmobj.hour) + ":" + addZero(alarmobj.minutes) + ":" + addZero(alarmobj.seconds); alarm_status=document.getElementById("alarm-status"); alarm_status.innerText="Status: "; alarm_status.innerText += alarmobj.enabled ? " aktiviert" : " deaktiviert"; alarm_status.innerText += "\nAusgelöst: "; alarm_status.innerText += alarmobj.pending ? " ja" : " nein"})
+	fetch("https://169.254.175.168:8888/wkalrm").then(function(text) { if(!text.ok) { throw text }; return text.text() }).then( function(text2) { alarmdiv = document.getElementById("alarm"); alarmdiv.style.visibility="visible"; alarmtime=document.getElementById("alarm-time"); alarmobj=JSON.parse(text2); alarmtime.innerText = alarmobj.mday + "." + (alarmobj.month +1) + "." + (alarmobj.year + 1900) + " " + addZero(alarmobj.hour) + ":" + addZero(alarmobj.minutes) + ":" + addZero(alarmobj.seconds); alarm_status=document.getElementById("alarm-status"); alarm_status.innerText="Status: "; alarm_status.innerText += alarmobj.enabled ? " aktiviert" : " deaktiviert"; alarm_status.innerText += "\nAusgelöst: "; alarm_status.innerText += alarmobj.pending ? " ja" : " nein"}).catch( function(error) { console.log("alarm access failed") })
 }
 getSystemTime()
 getNVRAM()
@@ -26,14 +26,14 @@ getAlarm()
 getTime()
 
 function getNVRAM()  {
-	fetch("https://169.254.175.168:8888/nvmem").then(function(text) { return text.text() }).then( function(text2) { nvobj = JSON.parse(text2); element = document.getElementById("nvram-text"); element.innerText = nvobj.content; element2 = document.getElementById("nvram"); element2.style.visibility="visible"; })
+	fetch("https://169.254.175.168:8888/nvmem").then(function(text) { if(!text.ok) { throw text }; return text.text() }).then( function(text2) { nvobj = JSON.parse(text2); element = document.getElementById("nvram-text"); element.innerText = nvobj.content; element2 = document.getElementById("nvram"); element2.style.visibility="visible"; }).catch( function(error) { console.log("error while reading nvram") })
 }
 
 function submitNvRAM() {
 	nvobj = {};
 	element = document.getElementById("nvram-text");
 	nvobj.content = element.value;
-	fetch("https://169.254.175.168:8888/nvmem", {method: "POST", body: JSON.stringify(nvobj)}).then(getNVRAM())
+	fetch("https://169.254.175.168:8888/nvmem", {method: "POST", body: JSON.stringify(nvobj)}).then( function(x) {alert("NVRAM erfolgreich geschrieben"); getNVRAM()}).catch( function(error) { alert("Fehler beim Schreiben in den NVRAM") })
 }
 
 function changeAlarm() {
@@ -56,7 +56,7 @@ function changeAlarm() {
 			"enabled": 1,
 			"pending": 0
 		}
-		fetch("https://169.254.175.168:8888/wkalrm", {method: "POST", body: JSON.stringify(alobj)}).then(function() {textelement.remove(); getAlarm();}) 	
+		fetch("https://169.254.175.168:8888/wkalrm", {method: "POST", body: JSON.stringify(alobj)}).then(function() {textelement.remove(); getAlarm();}).then(function(x) {alert("Alarmzeitpunkt erfolgreich geändert!")}).catch(function(y) {alert("Fehler beim Schreiben des Alarms!")}) 	
 	}
 }
 
@@ -78,7 +78,7 @@ function changeRTC() {
                         "year": parseInt(date[2]) -1900,
                         "wday": 1
                 }
-		fetch("https://169.254.175.168:8888/time", {method: "POST", body: JSON.stringify(tobj)}).then(function() {textelement.remove(); getTime();})
+		fetch("https://169.254.175.168:8888/time", {method: "POST", body: JSON.stringify(tobj)}).then(function() {textelement.remove(); getTime();}).then(function(x) {alert("Zeit/Datum erfolgreich geschrieben!")}).catch(function(x) {alert("Fehler beim Schreiben von Uhrzeit bzw. Datum!")})
 	}
 }
 
@@ -94,5 +94,28 @@ function systoHC() {
 		"wday": 6
 	}
 	console.log(tobj)
-	fetch("https://169.254.175.168:8888/time", {method: "POST", body: JSON.stringify(tobj)}).then(function() {getTime();})
+	fetch("https://169.254.175.168:8888/time", {method: "POST", body: JSON.stringify(tobj)}).then(function() {getTime();alert("Zeit erfolgreich mit RTC synchronisiert")}).catch(function(error) {"Fehler beim Synchronisieren mit der RTC!"})
+}
+
+function updateAll() {
+	getTime()
+	getSystemTime()
+	getAlarm()
+	getNVRAM()
+}
+
+intervallID = 0;
+
+function configureIntervall() {
+	if(intervallID == 0) {
+		element = document.getElementById("intervaltext");
+		intervallID = setInterval(updateAll, parseInt(element.value) * 1000);
+	} else {
+		alert("Intervall ist bereits konfiguriert, stoppe die Aktualisierung bitte zuerst!")
+	}
+}
+
+function removeItervall() {
+	clearInterval(intervallID)
+	intervallID = 0;
 }