common.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* The UI can also be run locally, but you have to set the IP of your device accordingly.
  2. * And you also might have to disable CORS in your webbrowser!
  3. * Eg using https://chromewebstore.google.com/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?utm_source=ext_app_menu on chrome
  4. * Keep empty to disable using it. Enabling it will break access through a forwared port, see
  5. * https://github.com/jomjol/AI-on-the-edge-device/issues/2681 */
  6. var domainname_for_testing = "";
  7. //var domainname_for_testing = "192.168.1.151";
  8. /* Returns the domainname with prepended protocol.
  9. Eg. http://watermeter.fritz.box or http://192.168.1.5 */
  10. function getDomainname(){
  11. var host = window.location.hostname;
  12. if (domainname_for_testing != "") {
  13. console.log("Using pre-defined domainname for testing: " + domainname_for_testing);
  14. domainname = "http://" + domainname_for_testing
  15. }
  16. else
  17. {
  18. domainname = window.location.protocol + "//" + host;
  19. if (window.location.port != "") {
  20. domainname = domainname + ":" + window.location.port;
  21. }
  22. }
  23. return domainname;
  24. }
  25. function UpdatePage(_dosession = true){
  26. var zw = location.href;
  27. zw = zw.substr(0, zw.indexOf("?"));
  28. if (_dosession) {
  29. window.location = zw + '?session=' + Math.floor((Math.random() * 1000000) + 1);
  30. }
  31. else {
  32. window.location = zw;
  33. }
  34. }
  35. function LoadHostname() {
  36. _domainname = getDomainname();
  37. var xhttp = new XMLHttpRequest();
  38. xhttp.addEventListener('load', function(event) {
  39. if (xhttp.status >= 200 && xhttp.status < 300) {
  40. hostname = xhttp.responseText;
  41. document.title = hostname + " - AI on the edge";
  42. document.getElementById("id_title").innerHTML = "Digitizer - AI on the edge - " + hostname;
  43. }
  44. else {
  45. console.warn(request.statusText, request.responseText);
  46. }
  47. });
  48. // var xhttp = new XMLHttpRequest();
  49. try {
  50. url = _domainname + '/info?type=Hostname';
  51. xhttp.open("GET", url, true);
  52. xhttp.send();
  53. }
  54. catch (error)
  55. {
  56. // alert("Loading Hostname failed");
  57. }
  58. }
  59. var fwVersion = "";
  60. var webUiVersion = "";
  61. // Load all startup metadata from one sysinfo request instead of three /info requests.
  62. function LoadStartupInfo() {
  63. var xhttp = new XMLHttpRequest();
  64. xhttp.onreadystatechange = function() {
  65. if (xhttp.readyState !== 4) {
  66. return;
  67. }
  68. if (xhttp.status < 200 || xhttp.status >= 300) {
  69. console.warn("Could not load startup information:", xhttp.status, xhttp.responseText);
  70. fwVersion = "NaN";
  71. webUiVersion = "NaN";
  72. return;
  73. }
  74. try {
  75. var response = JSON.parse(xhttp.responseText);
  76. var info = Array.isArray(response) ? response[0] : response;
  77. if (!info || typeof info !== "object") {
  78. throw new Error("Invalid system information response");
  79. }
  80. var hostname = info.hostname || "";
  81. if (hostname) {
  82. document.title = hostname + " - AI on the edge";
  83. var title = document.getElementById("id_title");
  84. if (title) {
  85. title.innerHTML = "Digitizer - AI on the edge - " + hostname;
  86. }
  87. }
  88. fwVersion = info.firmwareVersion || "NaN";
  89. webUiVersion = info.htmlVersion || "NaN";
  90. var version = document.getElementById("Version");
  91. if (version) {
  92. version.innerHTML = fwVersion;
  93. }
  94. console.log(fwVersion);
  95. console.log("Web UI Version: " + webUiVersion);
  96. compareVersions();
  97. }
  98. catch (error) {
  99. console.warn("Could not parse startup information:", error);
  100. fwVersion = "NaN";
  101. webUiVersion = "NaN";
  102. }
  103. };
  104. try {
  105. xhttp.open("GET", getDomainname() + "/sysinfo", true);
  106. xhttp.send();
  107. }
  108. catch (error) {
  109. console.warn("Could not request startup information:", error);
  110. fwVersion = "NaN";
  111. webUiVersion = "NaN";
  112. }
  113. }
  114. function LoadFwVersion() {
  115. _domainname = getDomainname();
  116. var xhttp = new XMLHttpRequest();
  117. xhttp.addEventListener('load', function(event) {
  118. if (xhttp.status >= 200 && xhttp.status < 300) {
  119. fwVersion = xhttp.responseText;
  120. document.getElementById("Version").innerHTML = fwVersion;
  121. console.log(fwVersion);
  122. compareVersions();
  123. }
  124. else {
  125. console.warn(request.statusText, request.responseText);
  126. fwVersion = "NaN";
  127. }
  128. });
  129. try {
  130. url = _domainname + '/info?type=FirmwareVersion';
  131. xhttp.open("GET", url, true);
  132. xhttp.send();
  133. }
  134. catch (error) {
  135. fwVersion = "NaN";
  136. }
  137. }
  138. function LoadWebUiVersion() {
  139. _domainname = getDomainname();
  140. var xhttp = new XMLHttpRequest();
  141. xhttp.addEventListener('load', function(event) {
  142. if (xhttp.status >= 200 && xhttp.status < 300) {
  143. webUiVersion = xhttp.responseText;
  144. console.log("Web UI Version: " + webUiVersion);
  145. compareVersions();
  146. }
  147. else {
  148. console.warn(request.statusText, request.responseText);
  149. webUiVersion = "NaN";
  150. }
  151. });
  152. try {
  153. url = _domainname + '/info?type=HTMLVersion';
  154. console.log("url");
  155. xhttp.open("GET", url, true);
  156. xhttp.send();
  157. }
  158. catch (error) {
  159. webUiVersion = "NaN";
  160. }
  161. }
  162. function compareVersions() {
  163. if (fwVersion == "" || webUiVersion == "") {
  164. return;
  165. }
  166. arr = fwVersion.split(" ");
  167. fWGitHash = arr[arr.length - 1].substring(0, 7);
  168. arr = webUiVersion.split(" ");
  169. webUiHash = arr[arr.length - 1].substring(0, 7);
  170. console.log("FW Hash: " + fWGitHash + ", Web UI Hash: " + webUiHash);
  171. if (fWGitHash != webUiHash) {
  172. firework.launch("The version of the web interface (" + webUiHash +
  173. ") does not match the firmware version (" +
  174. fWGitHash + ")! It is suggested to keep them on the same version!", 'warning', 30000);
  175. }
  176. }