app.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. const URLS = {
  3. manifestList: "http://flasher.thingssdk.com/v1/manifest-list.json"
  4. };
  5. //Relative to index.html not app.js
  6. const SerialScanner = require("../back-end/serial_scanner");
  7. const PortSelect = require("./js/port_select");
  8. const prepareBinaries = require("../back-end/prepare_binaries");
  9. const log = require("../back-end/logger");
  10. const RomComm = require("../back-end/rom_comm");
  11. function $(id) { return document.getElementById(id); }
  12. const flashButton = $("flash-button");
  13. const appStatus = $("status");
  14. const portsSelect = new PortSelect($("ports"));
  15. const manifestsSelect = $("manifests");
  16. const serialScanner = new SerialScanner();
  17. const pollTime = 1000; // One second
  18. var last_notification = "";
  19. flashButton.addEventListener("click", event => {
  20. fetch(manifestsSelect.value)
  21. .then(processJSON)
  22. .then(flashWithManifest);
  23. });
  24. serialScanner.on("ports", (ports) => {
  25. portsSelect.addAll(ports);
  26. readyToFlash();
  27. });
  28. serialScanner.on("deviceAdded", (port) => {
  29. portsSelect.add(port);
  30. new Notification(`Added: ${port}!`);
  31. });
  32. serialScanner.on("deviceRemoved", (port ) => {
  33. portsSelect.remove(port);
  34. new Notification(`Removed: ${port}!`);
  35. });
  36. serialScanner.on("error", onError);
  37. /**
  38. * Updates UI to say it's ready
  39. */
  40. function readyToFlash() {
  41. appStatus.textContent = "Ready";
  42. enableInputs();
  43. }
  44. /**
  45. * Enabled the serial port SELECT and flash BUTTON elements.
  46. */
  47. function enableInputs(){
  48. portsSelect.disabled = false;
  49. flashButton.disabled = false;
  50. }
  51. /**
  52. * Generic catch all error. Shows notification at the moment.
  53. * @param error
  54. */
  55. function onError(error){
  56. if(last_notification !== error.message) {
  57. last_notification = error.message;
  58. new Notification(last_notification);
  59. }
  60. appStatus.textContent = error.message;
  61. }
  62. function processJSON(response) {
  63. return response.json();
  64. }
  65. function generateManifestList(manifestsJSON) {
  66. console.log(manifestsJSON);
  67. manifestsJSON.options.forEach((option) => {
  68. option.versions.forEach((version) => {
  69. const optionElement = document.createElement("option");
  70. optionElement.textContent = `${option.name} - ${version.version}`;
  71. optionElement.value = version.manifest;
  72. manifestsSelect.appendChild(optionElement);
  73. manifestsSelect.disabled = false;
  74. });
  75. });
  76. }
  77. function getManifests() {
  78. fetch(URLS.manifestList)
  79. .then(processJSON)
  80. .then(generateManifestList).catch((error) => {
  81. console.log(error);
  82. setTimeout(getManifests, pollTime);
  83. });
  84. }
  85. /**
  86. * Sets up UI
  87. */
  88. function init() {
  89. getManifests();
  90. serialScanner.scan();
  91. setInterval(serialScanner.checkForChanges.bind(serialScanner), pollTime);
  92. }
  93. init();
  94. function flashWithManifest(manifest) {
  95. console.log(portsSelect.value);
  96. prepareBinaries(manifest, (err, flashSpec) => {
  97. if(err) throw err;
  98. const esp = new RomComm({
  99. portName: portsSelect.value,
  100. baudRate: 115200
  101. });
  102. esp.open().then((result) => {
  103. log.info("ESP is open", result);
  104. let promise = Promise.resolve();
  105. flashSpec.forEach((spec) => {
  106. promise = promise.then(()=> {
  107. return esp.flashAddress(Number.parseInt(spec.address), spec.buffer)
  108. });
  109. });
  110. return promise.then(() => esp.close())
  111. .then((result) => {
  112. var notification = new Notification("Flash Finished!");
  113. log.info("Flashed to latest Espruino build!", result);
  114. });
  115. }).catch((error) => {
  116. log.error("Oh noes!", error);
  117. });
  118. });
  119. }