Преглед на файлове

Creates utilities and introduces better logging

craigsdennis преди 9 години
родител
ревизия
94c08cf2c6
променени са 5 файла, в които са добавени 52 реда и са изтрити 44 реда
  1. 5 0
      back-end/logger.js
  2. 18 36
      back-end/rom_comm.js
  3. 5 8
      back-end/streams/slip.js
  4. 23 0
      back-end/utilities.js
  5. 1 0
      package.json

+ 5 - 0
back-end/logger.js

@@ -0,0 +1,5 @@
+"use strict";
+
+const bunyan = require("bunyan");
+
+module.exports = bunyan.createLogger({name: "especially-flasher"});

+ 18 - 36
back-end/rom_comm.js

@@ -2,7 +2,10 @@
 
 const fs = require("fs");
 const SerialPort = require("serialport").SerialPort;
+const log = require("./logger");
 const slip = require("./streams/slip");
+const delay = require("./utilities").delay;
+const repeatPromise = require("./utilities").repeatPromise;
 
 
 // ../esptool.py --port /dev/cu.SLAB_USBtoUART --baud 115200 \
@@ -40,24 +43,6 @@ const SYNC_FRAME = new Buffer([0x07, 0x07, 0x12, 0x20,
 const FLASH_BLOCK_SIZE = 0x400;
 const SUCCESS = [0x01, 0x01];
 
-var debug = function() {};
-
-function delay(time) {
-    return new Promise((resolve) => {
-        debug("Delaying for", time);
-        setTimeout(resolve, time);
-    });
-}
-
-function repeatPromise(times, callback) {
-    let chain = Promise.resolve();
-    for (let i = 0; i < times; i++) {
-        chain = chain.then(() => callback());
-    }
-    return chain;
-}
-
-
 class EspBoard {
     constructor(port) {
         this.port = port;
@@ -65,7 +50,7 @@ class EspBoard {
 
     portSet(options) {
         return new Promise((resolve, reject) => {
-            debug("Setting port", options);
+            log.info("Setting port", options);
             this.port.set(options, (err, result) => {
                 if (err) {
                     reject(err);
@@ -78,7 +63,7 @@ class EspBoard {
     resetIntoBootLoader() {
         // RTS - Request To Send
         // DTR - Data Terminal Ready
-        debug("Resetting board");
+        log.info("Resetting board");
         return this.portSet({rts: true, dtr:false})
             .then(() => delay(5))
             .then(() => this.portSet({rts: false, dtr: true}))
@@ -90,9 +75,6 @@ class EspBoard {
 
 class RomComm {
     constructor(config) {
-        if (config.debug) {
-            debug = config.debug;
-        }
         this._port = new SerialPort(config.portName, {
             baudRate: config.baudRate,
             parity: 'none',
@@ -110,26 +92,26 @@ class RomComm {
     }
 
     bindPort() {
-        this._port.on('error', (error) => debug("PORT ERROR", error));
-        this.in = new slip.SlipDecoder({debug: debug});
-        this.out = new slip.SlipEncoder({debug: debug});
+        this._port.on('error', error => log.error("PORT ERROR", error));
+        this.in = new slip.SlipDecoder();
+        this.out = new slip.SlipEncoder();
         this._port.pipe(this.in);
         this.out.pipe(this._port);
         this.in.on("data", (data) => {
             if (data.length < 8) {
-                debug("Missing header");
+                log.error("Missing header");
                 return;
             }
             let headerBytes = data.slice(0, 8);
             let header = this.headerPacketFrom(headerBytes);
             if (header.direction != 0x01) {
-                debug("Invaid direction", header.direction);
+                log.error("Invaid direction", header.direction);
                 return;
             }
             let commandName = commandToKey(header.command);
             let body = data.slice(8, header.size);
 
-            debug("Emitting", commandName, body);
+            log.info("Emitting", commandName, body);
             this.in.emit(commandName, body);
         });
     }
@@ -137,7 +119,7 @@ class RomComm {
     open() {
         return new Promise((resolve, reject) => {
             this._port.open((error) => {
-                debug("Opening port...", this._port);
+                log.info("Opening port...", this._port);
                 if (error) {
                     reject(error);
                 } else {
@@ -163,11 +145,11 @@ class RomComm {
 
 
     sync(ignoreResponse) {
-        debug("Syncing");
+        log.info("Syncing");
         return this.sendCommand(commands.SYNC_FRAME, SYNC_FRAME, ignoreResponse)
             .then((response) => {
                 if (!ignoreResponse) {
-                    debug("Sync response completed!", response);
+                    log.info("Sync response completed!", response);
                     this.isInBootLoader = true;
                 }
             });
@@ -192,7 +174,7 @@ class RomComm {
                         if (error) {
                             reject(error);
                         }
-                        debug("Port flushed");
+                        log.info("Port flushed");
 
                         resolve();
                     });
@@ -313,7 +295,7 @@ class RomComm {
                 this.out.write(message, 'buffer', (err, res) => {
                     delay(5).then(() => {
                         this._port.drain((drainErr, results) => {
-                            debug("Draining", drainErr, results);
+                            log.info("Draining", drainErr, results);
                             if (ignoreResponse) {
                                 resolve("Response was ignored");
                             }
@@ -324,12 +306,12 @@ class RomComm {
             if (!ignoreResponse) {
                 let commandName = commandToKey(command);
                 if (this.in.listeners(commandName).length === 0) {
-                    debug("Listening once", commandName);
+                    log.info("Listening once", commandName);
                     this.in.once(commandName, (response) => {
                         resolve(response);
                     });
                 } else {
-                    debug("Someone is already awaiting", commandName);
+                    log.info("Someone is already awaiting", commandName);
                 }
             }
         });

+ 5 - 8
back-end/streams/slip.js

@@ -1,6 +1,7 @@
 "use strict";
 
 const stream = require("stream");
+const log = require("../logger");
 const Transform = stream.Transform;
 const Readable = stream.Readable;
 
@@ -11,16 +12,12 @@ const CODES = {
     transposedFrameEnd: 0xDC,
     transposedFrameEscape: 0xDD
  };
- let debug = () => {};
 
 
 
 class SlipDecoder extends Transform {
     constructor(options) {
         super(options);
-        if (options.debug) {
-            debug = options.debug;
-        }
         this._slipping = false;
         this.resetDecoded();
     }
@@ -33,16 +30,16 @@ class SlipDecoder extends Transform {
     // TODO:csd - Write flush
 
     _transform(chunk, encoding, done) {
-        debug("SlipDecoder._transform", encoding, chunk.length);
+        log.info("SlipDecoder._transform", encoding, chunk.length);
         for (let index = 0; index < chunk.length; index++) {
             let val = chunk[index];
             if (val === CODES.frameEnd) {
-                debug("frameEnd detected");
+                log.debug("frameEnd detected");
                 if (this._slipping) {
                     this._slipping = false;
                     // Return all of decoded
                     this.push(this.decoded.slice(0, this.decodedIndex));
-                    debug("Resetting buffer");
+                    log.debug("Resetting buffer");
                     this.resetDecoded();
                 } else {
                     this._slipping = true;
@@ -70,7 +67,7 @@ class SlipDecoder extends Transform {
 class SlipEncoder extends Transform {
 
     _transform(chunk, encoding, done) {
-        debug("SlipEncoder._transform", encoding);
+        log.info("SlipEncoder._transform", encoding);
         let encoded = new Buffer(chunk.length + 100);
         let encodedIndex = 0;
         encoded[encodedIndex++] = CODES.frameEnd;

+ 23 - 0
back-end/utilities.js

@@ -0,0 +1,23 @@
+"use strict";
+
+const log = require("./logger");
+
+function delay(time) {
+    return new Promise((resolve) => {
+        log.info("Delaying for %d ms", time);
+        setTimeout(resolve, time);
+    });
+}
+
+function repeatPromise(times, callback) {
+    let chain = Promise.resolve();
+    for (let i = 0; i < times; i++) {
+        chain = chain.then(() => callback());
+    }
+    return chain;
+}
+
+module.exports = {
+    delay: delay,
+    repeatPromise: repeatPromise
+};

+ 1 - 0
package.json

@@ -24,6 +24,7 @@
   },
   "homepage": "https://github.com/thingsSDK/especially-flasher#readme",
   "dependencies": {
+    "bunyan": "^1.8.0",
     "node-binary": "^1.1.0",
     "serialport": "^2.0.7-beta1"
   },