tools.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 series 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. } else if (json[v] == "8") {
  60. document.getElementById("logo").src="images/bluemark.png";
  61. document.getElementById("logo").alt="BlueMark";
  62. document.getElementById("STATUS:BOARD").innerText = "BlueMark db202mav";
  63. document.getElementById("documentation").innerHTML = "<ul><li><a href='https://download.bluemark.io/db200.pdf'>db200 series 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>";
  64. document.body.style.background = "#fafafa";
  65. } else if (json[v] == "9") {
  66. document.getElementById("logo").src="images/bluemark.png";
  67. document.getElementById("logo").alt="BlueMark";
  68. document.getElementById("STATUS:BOARD").innerText = "BlueMark db203can";
  69. document.getElementById("documentation").innerHTML = "<ul><li><a href='https://download.bluemark.io/db200.pdf'>db200 series 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>";
  70. document.body.style.background = "#fafafa";
  71. } else if (json[v] == "11") {
  72. document.getElementById("logo").src="images/holybro.jpg";
  73. document.getElementById("logo").alt="Holybro";
  74. document.getElementById("STATUS:BOARD").innerText = "Holybro RemoteID";
  75. 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>";
  76. } else if (json[v] == "12") {
  77. document.getElementById("logo").src="images/CUAV.jpg";
  78. document.getElementById("logo").alt="CUAV";
  79. document.getElementById("STATUS:BOARD").innerText = "CUAV RemoteID";
  80. 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>";
  81. } else {
  82. document.getElementById("logo").src="images/logo.jpg";
  83. document.getElementById("logo").alt="ArduPilot";
  84. if (json[v] == "1") {
  85. document.getElementById("STATUS:BOARD").innerText = "ESP32S3_DEV";
  86. } else if (json[v] == "2") {
  87. document.getElementById("STATUS:BOARD").innerText = "ESP32C3_DEV";
  88. } else if (json[v] == "5") {
  89. document.getElementById("STATUS:BOARD").innerText = "JW_TBD";
  90. } else if (json[v] == "6") {
  91. document.getElementById("STATUS:BOARD").innerText = "mRo-RID";
  92. }else if(json[v] == '13'){
  93. document.getElementById("STATUS:BOARD").innerText = "VK-RID";
  94. }
  95. else {
  96. document.getElementById("STATUS:BOARD").innerText = "unknown:" + json[v];
  97. }
  98. 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>";
  99. }
  100. }
  101. run_once = 1;
  102. }
  103. }
  104. }
  105. /*
  106. fetch a URL, calling a callback
  107. */
  108. function ajax_get_callback(url, callback) {
  109. var xhr = createCORSRequest("GET", url);
  110. xhr.onload = function() {
  111. callback(xhr.responseText);
  112. }
  113. xhr.send();
  114. }
  115. /*
  116. fetch a URL, calling a callback for binary data
  117. */
  118. function ajax_get_callback_binary(url, callback) {
  119. var xhr = createCORSRequest("GET", url);
  120. xhr.onload = function() {
  121. console.log("got response length " + xhr.response.byteLength);
  122. callback(xhr.response);
  123. }
  124. xhr.responseType = "arraybuffer";
  125. xhr.send();
  126. }
  127. /*
  128. poll a URL, calling a callback
  129. */
  130. function ajax_poll(url, callback, refresh_ms=1000) {
  131. function again() {
  132. setTimeout(function() { ajax_poll(url, callback, refresh_ms); }, refresh_ms);
  133. }
  134. var xhr = createCORSRequest("GET", url);
  135. xhr.onload = function() {
  136. if (callback(xhr.responseText)) {
  137. again();
  138. }
  139. }
  140. xhr.onerror = function() {
  141. again();
  142. }
  143. xhr.timeout = 3000;
  144. xhr.ontimeout = function() {
  145. again();
  146. }
  147. xhr.send();
  148. }
  149. /*
  150. poll a json file and fill document IDs at the given rate
  151. */
  152. function ajax_json_poll(url, callback, refresh_ms=1000) {
  153. function do_callback(responseText) {
  154. try {
  155. var json = JSON.parse(responseText);
  156. return callback(json);
  157. } catch(e) {
  158. return true;
  159. }
  160. /* on bad json keep going */
  161. return true;
  162. }
  163. ajax_poll(url, do_callback, refresh_ms);
  164. }
  165. /*
  166. poll a json file and fill document IDs at the given rate
  167. */
  168. function ajax_json_poll_fill(url, refresh_ms=1000) {
  169. function callback(json) {
  170. page_fill_json_html(json);
  171. return true;
  172. }
  173. ajax_json_poll(url, callback, refresh_ms);
  174. }
  175. /*
  176. set a message in a div by id, with given color
  177. */
  178. function set_message_color(id, color, message) {
  179. var element = document.getElementById(id);
  180. if (element) {
  181. element.innerHTML = '<b style="color:' + color + '">' + message + '</b>';
  182. }
  183. }