tools.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. helper functions for RemoteID javascript
  3. */
  4. // helper function for cross-origin requests
  5. function createCORSRequest(method, url) {
  6. var xhr = new XMLHttpRequest();
  7. if ("withCredentials" in xhr) {
  8. // XHR for Chrome/Firefox/Opera/Safari.
  9. xhr.open(method, url, true);
  10. } else if (typeof XDomainRequest != "undefined") {
  11. // XDomainRequest for IE.
  12. xhr = new XDomainRequest();
  13. xhr.open(method, url);
  14. } else {
  15. // CORS not supported.
  16. xhr = null;
  17. }
  18. return xhr;
  19. }
  20. /*
  21. fill variables in a page from json
  22. */
  23. function page_fill_json_value(json) {
  24. for (var v in json) {
  25. var element = document.getElementById(v);
  26. if (element) {
  27. element.value = json[v];
  28. }
  29. }
  30. }
  31. /*
  32. fill html in a page from json
  33. */
  34. function page_fill_json_html(json) {
  35. for (var v in json) {
  36. var element = document.getElementById(v);
  37. if (element) {
  38. element.innerHTML = json[v];
  39. } else if (v == "STATUS:BOARD_ID") {
  40. if(typeof page_fill_json_html.run_once == 'undefined' ) {
  41. //run this code only once to avoid updating these fields
  42. if (json[v] == "3") {
  43. document.getElementById("logo").src="images/bluemark.png";
  44. document.getElementById("logo").alt="BlueMark";
  45. document.getElementById("STATUS:BOARD").innerText = "BlueMark db200";
  46. document.getElementById("documentation").innerHTML = "<ul><li><a href='https://download.bluemark.io/db200.pdf'>db200 manual</a></li><li><a href='https://ardupilot.org/ardupilot/index.html'>ArduPilot Project</a></li><li><a href='https://github.com/ArduPilot/ArduRemoteID'>ArduRemoteID Project</a></li><li><a href='https://ardupilot.org/plane/docs/common-remoteid.html'>ArduPilot RemoteID Documentation</a></li><li><a href='https://www.opendroneid.org/'>OpenDroneID Website</a></li></ul>";
  47. document.body.style.background = "#fafafa";
  48. } else if (json[v] == "4") {
  49. document.getElementById("logo").src="images/bluemark.png";
  50. document.getElementById("logo").alt="BlueMark";
  51. document.getElementById("STATUS:BOARD").innerText = "BlueMark db110";
  52. document.getElementById("documentation").innerHTML = "<ul><li><a href='https://download.bluemark.io/db110.pdf'>db110 manual</a></li><li><a href='https://ardupilot.org/ardupilot/index.html'>ArduPilot Project</a></li><li><a href='https://github.com/ArduPilot/ArduRemoteID'>ArduRemoteID Project</a></li><li><a href='https://ardupilot.org/plane/docs/common-remoteid.html'>ArduPilot RemoteID Documentation</a></li><li><a href='https://www.opendroneid.org/'>OpenDroneID Website</a></li></ul>";
  53. document.body.style.background = "#fafafa";
  54. } else if (json[v] == "10") {
  55. document.getElementById("logo").src="images/bluemark.png";
  56. document.getElementById("logo").alt="BlueMark";
  57. document.getElementById("STATUS:BOARD").innerText = "BlueMark db210pro";
  58. document.getElementById("documentation").innerHTML = "<ul><li><a href='https://download.bluemark.io/db200.pdf'>db210pro manual</a></li><li><a href='https://ardupilot.org/ardupilot/index.html'>ArduPilot Project</a></li><li><a href='https://github.com/ArduPilot/ArduRemoteID'>ArduRemoteID Project</a></li><li><a href='https://ardupilot.org/plane/docs/common-remoteid.html'>ArduPilot RemoteID Documentation</a></li><li><a href='https://www.opendroneid.org/'>OpenDroneID Website</a></li></ul>";
  59. document.body.style.background = "#fafafa";
  60. } else {
  61. document.getElementById("logo").src="images/logo.jpg";
  62. document.getElementById("logo").alt="ArduPilot";
  63. if (json[v] == "1") {
  64. document.getElementById("STATUS:BOARD").innerText = "ESP32S3_DEV";
  65. } else if (json[v] == "2") {
  66. document.getElementById("STATUS:BOARD").innerText = "ESP32C3_DEV";
  67. } else if (json[v] == "5") {
  68. document.getElementById("STATUS:BOARD").innerText = "JW_TBD";
  69. } else if (json[v] == "6") {
  70. document.getElementById("STATUS:BOARD").innerText = "mRo-RID";
  71. } else {
  72. document.getElementById("STATUS:BOARD").innerText = "unknown:" + json[v];
  73. }
  74. document.getElementById("documentation").innerHTML = "<ul><li><a href='https://ardupilot.org/ardupilot/index.html'>ArduPilot Project</a></li><li><a href='https://github.com/ArduPilot/ArduRemoteID'>ArduRemoteID Project</a></li><li><a href='https://ardupilot.org/plane/docs/common-remoteid.html'>ArduPilot RemoteID Documentation</a></li><li><a href='https://www.opendroneid.org/'>OpenDroneID Website</a></li></ul>";
  75. }
  76. }
  77. run_once = 1;
  78. }
  79. }
  80. }
  81. /*
  82. fetch a URL, calling a callback
  83. */
  84. function ajax_get_callback(url, callback) {
  85. var xhr = createCORSRequest("GET", url);
  86. xhr.onload = function() {
  87. callback(xhr.responseText);
  88. }
  89. xhr.send();
  90. }
  91. /*
  92. fetch a URL, calling a callback for binary data
  93. */
  94. function ajax_get_callback_binary(url, callback) {
  95. var xhr = createCORSRequest("GET", url);
  96. xhr.onload = function() {
  97. console.log("got response length " + xhr.response.byteLength);
  98. callback(xhr.response);
  99. }
  100. xhr.responseType = "arraybuffer";
  101. xhr.send();
  102. }
  103. /*
  104. poll a URL, calling a callback
  105. */
  106. function ajax_poll(url, callback, refresh_ms=1000) {
  107. function again() {
  108. setTimeout(function() { ajax_poll(url, callback, refresh_ms); }, refresh_ms);
  109. }
  110. var xhr = createCORSRequest("GET", url);
  111. xhr.onload = function() {
  112. if (callback(xhr.responseText)) {
  113. again();
  114. }
  115. }
  116. xhr.onerror = function() {
  117. again();
  118. }
  119. xhr.timeout = 3000;
  120. xhr.ontimeout = function() {
  121. again();
  122. }
  123. xhr.send();
  124. }
  125. /*
  126. poll a json file and fill document IDs at the given rate
  127. */
  128. function ajax_json_poll(url, callback, refresh_ms=1000) {
  129. function do_callback(responseText) {
  130. try {
  131. var json = JSON.parse(responseText);
  132. return callback(json);
  133. } catch(e) {
  134. return true;
  135. }
  136. /* on bad json keep going */
  137. return true;
  138. }
  139. ajax_poll(url, do_callback, refresh_ms);
  140. }
  141. /*
  142. poll a json file and fill document IDs at the given rate
  143. */
  144. function ajax_json_poll_fill(url, refresh_ms=1000) {
  145. function callback(json) {
  146. page_fill_json_html(json);
  147. return true;
  148. }
  149. ajax_json_poll(url, callback, refresh_ms);
  150. }
  151. /*
  152. set a message in a div by id, with given color
  153. */
  154. function set_message_color(id, color, message) {
  155. var element = document.getElementById(id);
  156. if (element) {
  157. element.innerHTML = '<b style="color:' + color + '">' + message + '</b>';
  158. }
  159. }