app.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. "use strict";
  2. /**
  3. * Constants for application
  4. *
  5. * manifestList is the url that contains all possible manifests
  6. * pollTime is used to check for changes in the serial ports
  7. *
  8. * @type {{manifestList: string, pollTime: number}}
  9. */
  10. const CONSTANTS = {
  11. manifestList: "http://flasher.thingssdk.com/v1/manifest-list.json",
  12. pollTime: 1000
  13. };
  14. var last_notification = "";
  15. /************************
  16. * Backend dependencies.
  17. * Note: Paths are relative to index.html not app.js
  18. ************************/
  19. const SerialScanner = require("../back-end/serial_scanner");
  20. const PortSelect = require("./js/port_select");
  21. const prepareBinaries = require("../back-end/prepare_binaries");
  22. const log = require("../back-end/logger");
  23. const RomComm = require("../back-end/rom_comm");
  24. const serialScanner = new SerialScanner();
  25. /************************
  26. * UI Elements
  27. ************************/
  28. const flashButton = $("flash-button");
  29. const appStatus = $("status");
  30. const portsSelect = new PortSelect($("ports"));
  31. const manifestsSelect = $("manifests");
  32. const progressBar = $("progress");
  33. /************************
  34. * Utility Functions
  35. ************************/
  36. /**
  37. * Simple helper returns HTML element from an element's id
  38. *
  39. * @param id a string of the id of an HTML element
  40. * @returns {Element}
  41. */
  42. function $(id) { return document.getElementById(id); }
  43. /**
  44. * Processes the response from an HTTP fetch and returns the JSON promise.
  45. *
  46. * @param response from a fetch call
  47. * @returns {Promise}
  48. */
  49. function processJSON(response) {
  50. return response.json();
  51. }
  52. /************************
  53. * Handle UI
  54. ************************/
  55. flashButton.addEventListener("click", (event) => {
  56. disableInputs();
  57. fetch(manifestsSelect.value)
  58. .then(processJSON)
  59. .then(flashWithManifest);
  60. });
  61. /************************
  62. * Manage serial port events
  63. ************************/
  64. serialScanner.on("ports", (ports) => {
  65. portsSelect.addAll(ports);
  66. readyToFlash();
  67. });
  68. serialScanner.on("deviceAdded", (port) => {
  69. portsSelect.add(port);
  70. new Notification(`Added: ${port}!`);
  71. });
  72. serialScanner.on("deviceRemoved", (port) => {
  73. portsSelect.remove(port);
  74. new Notification(`Removed: ${port}!`);
  75. });
  76. serialScanner.on("error", onError);
  77. /**
  78. * Updates UI to say it's ready
  79. */
  80. function readyToFlash() {
  81. appStatus.textContent = "Ready";
  82. enableInputs();
  83. }
  84. /**
  85. * Enabled the serial port SELECT and flash BUTTON elements.
  86. */
  87. function enableInputs() {
  88. portsSelect.disabled = false;
  89. manifestsSelect.disabled = false;
  90. flashButton.disabled = false;
  91. }
  92. function disableInputs() {
  93. portsSelect.disabled = true;
  94. manifestsSelect.disabled = true;
  95. flashButton.disabled = true;
  96. }
  97. /**
  98. * Generic catch all error. Shows notification at the moment.
  99. * @param error
  100. */
  101. function onError(error){
  102. if(last_notification !== error.message) {
  103. last_notification = error.message;
  104. new Notification(last_notification);
  105. }
  106. appStatus.textContent = error.message;
  107. }
  108. function generateManifestList(manifestsJSON) {
  109. manifestsJSON.options.forEach((option) => {
  110. option.versions.forEach((version) => {
  111. const optionElement = document.createElement("option");
  112. optionElement.textContent = `${option.name} - ${version.version}`;
  113. optionElement.value = version.manifest;
  114. manifestsSelect.appendChild(optionElement);
  115. manifestsSelect.disabled = false;
  116. });
  117. });
  118. }
  119. function getManifests() {
  120. appStatus.textContent = "Getting latest manifests.";
  121. fetch(CONSTANTS.manifestList)
  122. .then(processJSON)
  123. .then(generateManifestList).catch(error => {
  124. setTimeout(getManifests, pollTime);
  125. });
  126. }
  127. function flashWithManifest(manifest) {
  128. appStatus.textContent = `Flashing ${portsSelect.value}`;
  129. prepareBinaries(manifest, (err, flashSpec) => {
  130. if(err) throw err;
  131. const esp = new RomComm({
  132. portName: portsSelect.value,
  133. baudRate: 115200,
  134. });
  135. esp.on('progress', (progress) => {
  136. applicationCache.textContent = progress.display;
  137. progressBar.style.width = `${Math.round((progress.details.flashedBytes/progress.details.totalBytes) * 100)}%`;
  138. });
  139. esp.open().then((result) => {
  140. appStatus.textContent = `Flashing ${portsSelect.value}...Opened Port.`;
  141. let promise = Promise.resolve();
  142. flashSpec.forEach(createProgressBars);
  143. return esp.flashSpecifications(flashSpec)
  144. .then(() => esp.close())
  145. .then((result) => {
  146. new Notification("Flash Finished!");
  147. readyToFlash();
  148. log.info("Flashed to latest Espruino build!", result);
  149. });
  150. }).catch((error) => {
  151. log.error("Oh noes!", error);
  152. });
  153. });
  154. }
  155. function createProgressBars(spec) {
  156. }
  157. function progressHandler(spec) {
  158. }
  159. /**
  160. * Get's manifest list for possibilities for flashing,
  161. * scans serial ports and sets up timer for checking for changes.
  162. */
  163. function start() {
  164. getManifests();
  165. serialScanner.scan();
  166. setInterval(serialScanner.checkForChanges.bind(serialScanner), CONSTANTS.pollTime);
  167. }
  168. /**
  169. * Start Application
  170. */
  171. start();