Преглед изворни кода

Brings specification knowledge into RomComm. Adds Progress reporting. Listen for progress

craigsdennis пре 9 година
родитељ
комит
487ef61118
2 измењених фајлова са 50 додато и 11 уклоњено
  1. 41 1
      back-end/rom_comm.js
  2. 9 10
      front-end/js/app.js

+ 41 - 1
back-end/rom_comm.js

@@ -68,6 +68,7 @@ class RomComm extends EventEmitter {
             dsrdtr: false
         }, false);
         this.bindPort();
+        this.resetState();
         var boardName = config.boardName ? config.boardName : "Esp12";
         var BoardFactory = boards[boardName];
         if (BoardFactory === undefined) {
@@ -78,6 +79,10 @@ class RomComm extends EventEmitter {
         this.config = config;
     }
 
+    resetState() {
+        this.setState("Unknown", {});
+    }
+
     bindPort() {
         this._port.on('error', error => log.error("PORT ERROR", error));
         this.in = new slip.SlipDecoder();
@@ -87,6 +92,13 @@ class RomComm extends EventEmitter {
         this.in.on("data", (data) => this.handleResponse(data));
     }
 
+    setState(status, details) {
+        this.state = {
+            display: status,
+            details: details
+        };
+    }
+
     /**
      * Response from the device are eventually sensical.  Hold tight.
      */
@@ -298,6 +310,24 @@ class RomComm extends EventEmitter {
         });
     }
 
+    flashSpecifications(specs) {
+        let totalBytes = specs.reduce(() => specs.buffer.length, 0);
+        let details = {
+            totalFiles: specs.length,
+            totalBytes: totalBytes,
+            flashedBytes: 0
+        };
+        this.setState("Flashing", details);
+        let promiseFunctions = specs.map((spec, index) => () => {
+            details.currentIndex = index;
+            details.currentAddress = spec.address;
+            details.currentSize = spec.buffer.length;
+            return this.flashAddress(Number.parseInt(spec.address), spec.buffer)
+                .then(() => details.flashedBytes += details.currentSize);
+        });
+        return promiseChain(promiseFunctions);
+    }
+
     flashAddress(address, data) {
         return new Promise((resolve, reject) => {
             this.prepareFlashAddress(address, data.length)
@@ -329,12 +359,22 @@ class RomComm extends EventEmitter {
                         dv.setUint32(12, 0, true);  // Uhhh
                         requests.push(Buffer.concat([new Buffer(buffer), block]));
                     }
-                    let promiseFunctions = requests.map((req) => () => this.sendCommand(commands.FLASH_DOWNLOAD_DATA, req));
+                    let promiseFunctions = requests.map((req, index) => () => {
+                        this.reportBlockProgress(req.length, index, requests.length);
+                        return this.sendCommand(commands.FLASH_DOWNLOAD_DATA, req);
+                    });
                     return promiseChain(promiseFunctions);
                 }).then((result) => resolve(result));
         });
     }
 
+    reportBlockProgress(length, index, total) {
+        this.state.details.blockSize = length;
+        this.state.details.blockIndex = index;
+        this.state.details.totalBlocks = total;
+        this.emit('progress', this.state);
+    }
+
     /**
      * Must be called after flashing has occurred to switch modes
      */

+ 9 - 10
front-end/js/app.js

@@ -161,17 +161,16 @@ function flashWithManifest(manifest) {
             progress: progressHandler
         });
 
+        esp.on('progress', (progress) => {
+           log.info("Current progress:", progress);
+        });
+
         esp.open().then((result) => {
-            appStatus.textContent = `Flashing ${portsSelect.value}...Openned Port.`;            let promise = Promise.resolve();
+            appStatus.textContent = `Flashing ${portsSelect.value}...Opened Port.`;
+            let promise = Promise.resolve();
             flashSpec.forEach(createProgressBars);
-            flashSpec.forEach((spec, index) => {
-               promise = promise.then(()=> {
-                   appStatus.textContent = `Flashing ${index+1}/${flashSpec.length} binaries.`;
-                   return esp.flashAddress(Number.parseInt(spec.address), spec.buffer)
-               });
-            });
-
-            return promise.then(() => esp.close())
+            return esp.flashSpecifications(flashSpec)
+                .then(() => esp.close())
                 .then((result) => {
                     new Notification("Flash Finished!");
                     readyToFlash();
@@ -201,7 +200,7 @@ function start() {
     setInterval(serialScanner.checkForChanges.bind(serialScanner), CONSTANTS.pollTime);
 }
 
-/** 
+/**
  * Start Application
  */
 start();