script.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var rawContent;
  2. var data = [];
  3. var loading = false;
  4. var websock;
  5. var settings = {
  6. modOffset: 0,
  7. inputOffset: 0,
  8. updateInterval: 2,
  9. autoupdate: false,
  10. emulateMaster: false
  11. }
  12. var autoRefreshInterval;
  13. function GetData() {
  14. if (loading)
  15. return;
  16. loading = true;
  17. return fetch("/data")
  18. .then(function(result) {
  19. return result.text().then(function(text) {
  20. rawContent = text;
  21. data = ConvertData(rawContent);
  22. loading = false;
  23. });
  24. })
  25. .catch(function() {
  26. loading = false;
  27. });
  28. }
  29. function ConvertData(rawContent) {
  30. let dataResult = [];
  31. var tmpList = rawContent.split('~');
  32. tmpList.forEach(resultLine => {
  33. var tmpResultLine = resultLine.split(":");
  34. var tmpModule = tmpResultLine[0].split(".");
  35. var dataObj = {
  36. module: parseInt(tmpModule[0]),
  37. input: parseInt(tmpModule[1]),
  38. data: (tmpResultLine[1] == 1)
  39. }
  40. if (!isNaN(dataObj.input) && !isNaN(dataObj.module))
  41. dataResult.push(dataObj);
  42. });
  43. return dataResult;
  44. }
  45. function RenderData(data) {
  46. var table = document.getElementById("overview");
  47. table.innerHTML = "";
  48. var lastModule = -1;
  49. var row;
  50. data.forEach(element => {
  51. if (lastModule != element.module) {
  52. lastModule = element.module
  53. row = table.insertRow(element.module);
  54. var title = row.insertCell(0);
  55. title.innerHTML = (element.module + settings.modOffset);
  56. }
  57. var cell = row.insertCell(element.input + 1);
  58. cell.innerHTML = (element.input + settings.inputOffset);
  59. cell.classList.add('input-value')
  60. cell.classList.add('input-value-' + ((element.data) ? "ON" : "OFF"))
  61. });
  62. document.getElementById("lastUpdate").innerHTML = new Date().toLocaleString();
  63. }
  64. function SaveSettings() {
  65. settings.modOffset = parseInt(document.getElementById("modOffset").value);
  66. settings.inputOffset = parseInt(document.getElementById("inputOffset").value);
  67. settings.updateInterval = parseInt(document.getElementById("updateInterval").value);
  68. settings.autoupdate = document.getElementById("checkoptions-0").checked
  69. settings.emulateMaster = document.getElementById("checkoptions-1").checked
  70. window.localStorage.setItem('settings', JSON.stringify(settings));
  71. SetAutoUpdate();
  72. }
  73. function LoadSettings(rawSettings) {
  74. if(rawSettings == null){
  75. var tmpsettings = JSON.parse(window.localStorage.getItem('settings'));
  76. if (tmpsettings == null)
  77. return;
  78. settings = tmpsettings;
  79. }else{
  80. settings = rawSettings;
  81. }
  82. document.getElementById("modOffset").value = settings.modOffset;
  83. document.getElementById("inputOffset").value = settings.inputOffset;
  84. document.getElementById("updateInterval").value = settings.updateInterval;
  85. document.getElementById("checkoptions-0").checked = settings.autoupdate;
  86. document.getElementById("checkoptions-1").checked = settings.emulateMaster;
  87. SetAutoUpdate();
  88. }
  89. function SaveSettingsOnDevice(){
  90. SaveSettings();
  91. var urlEncodedSettings = encodeURI(JSON.stringify(settings));
  92. var url = "/config?key=settings&value="+urlEncodedSettings;
  93. fetch(url)
  94. .then(function(result) {
  95. result.text().then(function(text) {
  96. alert("Saved")
  97. });
  98. })
  99. .catch(function() {
  100. });
  101. }
  102. function LoadSettingsFromDevice(){
  103. fetch("/config?key=settings")
  104. .then(function(result) {
  105. return result.text().then(function(text) {
  106. var settingsraw = JSON.parse(decodeURI(text));
  107. LoadSettings(settingsraw);
  108. });
  109. })
  110. .catch(function() {
  111. loading = false;
  112. });
  113. }
  114. function SetAutoUpdate() {
  115. if (autoRefreshInterval != null)
  116. clearInterval(autoRefreshInterval)
  117. autoRefreshInterval = setInterval(() => {
  118. if (settings.autoupdate) {
  119. if (loading)
  120. return;
  121. DoUpdate();
  122. }
  123. }, settings.updateInterval*1000);
  124. }
  125. function DoUpdate() {
  126. if (loading)
  127. return;
  128. GetData().then(() => {
  129. RenderData(data)
  130. });
  131. }
  132. function StartSocket() {
  133. websock = new WebSocket('ws://' + window.location.hostname + ':81/');
  134. websock.onopen = function(evt) { console.log('websock open'); };
  135. websock.onclose = function(evt) { console.log('websock close'); };
  136. websock.onerror = function(evt) { console.log(evt); };
  137. websock.onmessage = function(evt) {
  138. console.log(evt);
  139. switch(evt.data){
  140. case "update":
  141. DoUpdate();
  142. break;
  143. }
  144. };
  145. }
  146. LoadSettings();
  147. DoUpdate();
  148. StartSocket();
  149. document.getElementById("button-save").addEventListener("click", SaveSettings);
  150. document.getElementById("button-save-device").addEventListener("click", SaveSettingsOnDevice);
  151. document.getElementById("button-load-device").addEventListener("click", LoadSettingsFromDevice);
  152. document.getElementById("button-update").addEventListener("click", DoUpdate);