script.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. fetch("https://169.254.175.168:8888/device").then( function(text) { return text.text() } ).then( function(text2) { element = document.getElementById("deviceheader"); element.innerText = text2} )
  2. function getTime() {
  3. 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") })
  4. }
  5. function addZero(num) {
  6. if (num < 10) {
  7. return "0" + num;
  8. }
  9. return num;
  10. }
  11. function getSystemTime() {
  12. date = new Date();
  13. element = document.getElementById("local");
  14. element.innerText = date.getDate() + "." + (date.getMonth() + 1) + "." + date.getYear() + " " + addZero(date.getHours()) + ":" + addZero(date.getMinutes()) + ":" + addZero(date.getSeconds());
  15. }
  16. function getAlarm() {
  17. 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") })
  18. }
  19. getSystemTime()
  20. getNVRAM()
  21. getAlarm()
  22. getTime()
  23. function getNVRAM() {
  24. 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") })
  25. }
  26. function submitNvRAM() {
  27. nvobj = {};
  28. element = document.getElementById("nvram-text");
  29. nvobj.content = element.value;
  30. 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") })
  31. }
  32. function changeAlarm() {
  33. element = document.getElementById("alarm-time");
  34. if (element.childElementCount === 0) {
  35. fetch("https://169.254.175.168:8888/wkalrm").then(function(text) {return text.text() }).then( function(text2) { element.innerText = ""; textarea = document.createElement("textarea"); textarea.id = "alarmtext"; textarea.rows=1; textarea.cols=20; alarmobj = JSON.parse(text2); textarea.innerText = alarmobj.mday + "." + (alarmobj.month +1) + "." + (alarmobj.year + 1900) + " " + addZero(alarmobj.hour) + ":" + addZero(alarmobj.minutes) + ":" + addZero(alarmobj.seconds); element.appendChild(textarea)})
  36. } else {
  37. textelement = document.getElementById("alarmtext");
  38. alstring = textelement.value.split(" ");
  39. date = alstring[0].split(".");
  40. time = alstring[1].split(":");
  41. alobj = {
  42. "seconds": parseInt(time[2]),
  43. "minutes": parseInt(time[1]),
  44. "hour": parseInt(time[0]),
  45. "mday": parseInt(date[0]),
  46. "month": parseInt(date[1]) -1,
  47. "year": parseInt(date[2]) -1900,
  48. "wday": 0,
  49. "enabled": 1,
  50. "pending": 0
  51. }
  52. 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!")})
  53. }
  54. }
  55. function changeRTC() {
  56. element = document.getElementById("rtc");
  57. if (element.childElementCount === 0) {
  58. fetch("https://169.254.175.168:8888/time").then(function(text) {return text.text() }).then( function(text2) { element.innerText = ""; textarea = document.createElement("textarea"); textarea.id = "rtctext"; textarea.rows=1; textarea.cols=20; timeobj = JSON.parse(text2); textarea.innerText = timeobj.mday + "." + (timeobj.month +1) + "." + (timeobj.year + 1900) + " " + addZero(timeobj.hour) + ":" + addZero(timeobj.minutes) + ":" + addZero(timeobj.seconds); element.appendChild(textarea);})
  59. } else {
  60. textelement = document.getElementById("rtctext");
  61. alstring = textelement.value.split(" ");
  62. date = alstring[0].split(".");
  63. time = alstring[1].split(":");
  64. tobj = {
  65. "seconds": parseInt(time[2]),
  66. "minutes": parseInt(time[1]),
  67. "hour": parseInt(time[0]),
  68. "mday": parseInt(date[0]),
  69. "month": parseInt(date[1]) -1,
  70. "year": parseInt(date[2]) -1900,
  71. "wday": 1
  72. }
  73. 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!")})
  74. }
  75. }
  76. function systoHC() {
  77. date = new Date();
  78. tobj = {
  79. "seconds": date.getSeconds(),
  80. "minutes": date.getMinutes(),
  81. "hour": date.getHours(),
  82. "mday": date.getDate(),
  83. "month": date.getMonth(),
  84. "year": date.getYear(),
  85. "wday": 6
  86. }
  87. console.log(tobj)
  88. 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!"})
  89. }
  90. function updateAll() {
  91. getTime()
  92. getSystemTime()
  93. getAlarm()
  94. getNVRAM()
  95. }
  96. intervallID = 0;
  97. function configureIntervall() {
  98. if(intervallID == 0) {
  99. element = document.getElementById("intervaltext");
  100. intervallID = setInterval(updateAll, parseInt(element.value) * 1000);
  101. } else {
  102. alert("Intervall ist bereits konfiguriert, stoppe die Aktualisierung bitte zuerst!")
  103. }
  104. }
  105. function removeItervall() {
  106. clearInterval(intervallID)
  107. intervallID = 0;
  108. }