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

Updated up and added an arbitrary progress for extracting files

Andrew Chalkley пре 9 година
родитељ
комит
9a84f89859
2 измењених фајлова са 25 додато и 1 уклоњено
  1. 12 0
      back-end/prepare_binaries.js
  2. 13 1
      front-end/js/app.js

+ 12 - 0
back-end/prepare_binaries.js

@@ -2,6 +2,7 @@
 const http = require("http");
 const unzip = require("unzip");
 const fs = require("fs");
+const EventEmitter = require("events");
 
 function isBinaryFileRequired(flashSpecification, fileName) {
     return flashSpecification.map(binary => binary.path).indexOf(fileName) !== -1;
@@ -16,11 +17,17 @@ function addBufferToBinary(flashSpecification, fileName, buffer) {
 }
 
 function prepareBinaries(manifest, callback) {
+    const eventEmitter = new EventEmitter();
     const flashContents = manifest.flash;
     const downloadRequest = http.get(manifest.download, (response) => {
         response.pipe(unzip.Parse()).on('entry', (entry) => {
             const fileName = entry.path;
             if (isBinaryFileRequired(flashContents, fileName)) {
+                eventEmitter.emit("entry", {
+                    display: `Extracting ${fileName}`,
+                    stage: "start"
+                });
+            
                 let body;
                 entry.on("data", function(data){
                     if(body) {
@@ -29,6 +36,10 @@ function prepareBinaries(manifest, callback) {
                         body = data;
                     }
                 }).on("end", () => {
+                    eventEmitter.emit("entry", {
+                        display: `Extracted ${fileName}`,
+                        stage: "end"
+                    });
                     addBufferToBinary(flashContents, fileName, body);
                 }).on("error", callback);
 
@@ -41,6 +52,7 @@ function prepareBinaries(manifest, callback) {
         });
         response.on("error", callback);
     });
+    return eventEmitter;
 }
 
 module.exports = prepareBinaries;

+ 13 - 1
front-end/js/app.js

@@ -152,6 +152,8 @@ function getManifests() {
 
 function flashWithManifest(manifest) {
     appStatus.textContent = `Flashing ${portsSelect.value}`;
+    const numberOfSteps = manifest.flash.length * 2;
+    let currectStepNumber = 0;
     prepareBinaries(manifest, (err, flashSpec) => {
         if(err) throw err;
 
@@ -162,11 +164,13 @@ function flashWithManifest(manifest) {
 
         esp.on('progress', (progress) => {
             applicationCache.textContent = progress.display;
-            progressBar.style.width = `${Math.round((progress.details.flashedBytes/progress.details.totalBytes) * 100)}%`;
+            const progressPercent = Math.round((progress.details.flashedBytes/progress.details.totalBytes) * 100);
+            updateProgressBar(progressPercent);
         });
 
         esp.open().then((result) => {
             appStatus.textContent = `Flashing ${portsSelect.value}...Opened Port.`;
+            updateProgressBar(0);
             let promise = Promise.resolve();
             flashSpec.forEach(createProgressBars);
             return esp.flashSpecifications(flashSpec)
@@ -179,9 +183,17 @@ function flashWithManifest(manifest) {
         }).catch((error) => {
             log.error("Oh noes!", error);
         });
+    }).on("entry", (progress) => {
+        const percent = Math.round((currectStepNumber++/numberOfSteps) * 100);
+        updateProgressBar(percent);
+        appStatus.textContent = progress.display;
     });
 }
 
+function updateProgressBar(percent) {
+    progressBar.style.width = `${percent}%`;
+}
+
 /**
  * Get's manifest list for possibilities for flashing,
  * scans serial ports and sets up timer for checking for changes.