All files interchange_client.js

32.88% Statements 24/73
0% Branches 0/12
5.26% Functions 1/19
32.88% Lines 24/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 1491x 1x 1x 1x 1x 1x 1x   1x       1x 1x 1x   1x 1x   1x               1x                 1x                                 1x   1x   1x           1x                                                                       1x       1x                                                                                     1x 1x  
const async = require('async');
const colors = require('colors');
const events = require('events');
const parsers = require('serialport').parsers; // used for parser
const SerialPort = require('serialport');
const ReadLine = require('@serialport/parser-readline');
const util = require('util');
 
const NL = 0x0a;
 
function Client() {
  // events.EventEmitter.call(this);
  let port_address = null;
  const sp_open = false;
  let sp_timeout = null;
 
  let current_state = null;
  let current_callback = null;
 
  const state_write = function(data) {
    // just passes the data up to anything that wants to listen for it.
    console.log(data);
    this.emit('data', data);
    // clear any callbacks
    current_callback = null;
  }.bind(this);
 
  const state_init = function(data) {
    // initialising the connection, waits for the magic chars to come out
    if (data.indexOf('>>') >=0) {
      // now we move to just writing anything from the serial line out.
      current_state = state_write;
      this.emit('ready');
    }
  }.bind(this);
 
  const state_dump = function(data) {
    // waits for the state dump to come back and then parses the json and
    //
    current_state = state_write;
    try {
      const dump = JSON.parse(data);
    } catch (err) {
      if (current_callback != null) {
        current_callback(e);
      }
    }
 
    if (current_callback != null) {
      current_callback(null, dump);
    }
  };
 
  current_state = state_init;
 
  this.serialport = null;
 
  this.get_info = function(cb) {
    this.serialport.write('DUMP\n');
    current_state = state_dump;
    current_callback = cb;
  };
 
  this.set_details = function(data, cb) {
    console.info('Setting firmware details'.magenta);
 
    current_state = state_write;
 
    // TODO refactor this into a promise chain instead.
    async.series([
      function(callback) {
        this.serialport.write('CLR\n');
        callback(null, 'erase');
      }.bind(this),
      function(callback) {
        const s = 'FID ' + data.firmwareID + '\n';
        this.serialport.write(s);
        callback(null, 'FID');
      }.bind(this),
      function(callback) {
        this.serialport.write('CID ' + data.creatorID + '\n');
        callback(null, 'CID');
      }.bind(this),
      function(callback) {
        this.serialport.write('I2C ' + data.i2c_address + ' ' + data.use_custom_address + '\n');
        // this is a hack - blame @jacobrosenthal as I pinched it
        // for how uploads are managed in STK500 loader.
        // Basically delay return on this just to give it time to
        // finish executing or the serial port will get closed.
        setTimeout(function() {
          callback(null, 'I2C')
        }, 20);
      }.bind(this)
    ],
    function(err, results) {
      cb();
    });
  };
 
  this.close = function() {
    this.serialport.close();
  };
 
  Object.defineProperties(this, {
    'port': {
      set(p) {
        if (p != undefined) {
          // set the port here.
          this.serialport = new SerialPort(p, {
            baudRate: 9600
          });
          const parser = new ReadLine();
          this.serialport.pipe(parser);
 
          sp_timeout = setTimeout(function() {
            if (current_state == state_init) {
              this.emit('error', new Error('Serialport timeout'));
            }
          }.bind(this), 4000);
 
          // create handlers for the various serial port actions.
          this.serialport.on('open', function(error) {
            if (error) {
              console.error(error);
            }
            port_address = p;
            this.emit('connected');
          }.bind(this));
 
          this.serialport.on('error', function(err) {
            console.error('error');
            this.emit('error', err);
          }.bind(this));
 
          this.serialport.on('data', function(data) {
            current_state(data);
          }.bind(this));
        }
      },
      get() {
        return port_address;
      }
    }
  });
}
 
util.inherits(Client, events.EventEmitter);
module.exports.Client = Client;