1
0

app.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. //Relative to index.html not app.js
  3. const SerialScanner = require("../back-end/serial_scanner");
  4. const PortSelect = require("./js/port_select");
  5. function $(id) { return document.getElementById(id) }
  6. const flashButton = $("flash-button");
  7. const appStatus = $("status");
  8. const portsSelect = new PortSelect($("ports"));
  9. const serialScanner = new SerialScanner();
  10. const pollTime = 1000; // One second
  11. var last_notification = "";
  12. flashButton.addEventListener("click", event => {
  13. var notification = new Notification("Flash Finished!");
  14. });
  15. serialScanner.on("ports", (ports) => {
  16. portsSelect.addAll(ports);
  17. readyToFlash();
  18. });
  19. serialScanner.on("deviceAdded", (port) => {
  20. portsSelect.add(port);
  21. new Notification(`Added: ${port}!`);
  22. });
  23. serialScanner.on("deviceRemoved", (port ) => {
  24. portsSelect.remove(port);
  25. new Notification(`Removed: ${port}!`);
  26. });
  27. serialScanner.on("error", onError);
  28. /**
  29. * Updates UI to say it's ready
  30. */
  31. function readyToFlash() {
  32. appStatus.textContent = "Ready";
  33. enableInputs();
  34. }
  35. /**
  36. * Enabled the serial port SELECT and flash BUTTON elements.
  37. */
  38. function enableInputs(){
  39. portsSelect.disabled = false;
  40. flashButton.disabled = false;
  41. }
  42. /**
  43. * Generic catch all error. Shows notification at the moment.
  44. * @param error
  45. */
  46. function onError(error){
  47. if(last_notification !== error.message) {
  48. last_notification = error.message;
  49. new Notification(last_notification);
  50. }
  51. appStatus.textContent = error.message;
  52. }
  53. /**
  54. * Sets up UI
  55. */
  56. function init() {
  57. serialScanner.scan();
  58. setInterval(serialScanner.checkForChanges.bind(serialScanner), pollTime);
  59. }
  60. init();