all files / lib/ lovesense.js

18.75% Statements 6/32
25% Branches 3/12
11.76% Functions 2/17
18.75% Lines 6/32
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                                                                                                                                                                                                                           
'use strict';
class LovesenseBase {
  constructor() {
  }
 
  vibrate(level) {
  }
 
  rotate(level) {
  }
 
  air(level) {
  }
 
  inflate(level) {
  }
 
  deflate(level) {
  }
 
  startAccelerometer() {
  }
 
  stopAccelerometer() {
  }
 
  changeRotationDirection() {
  }
 
  powerOff() {
  }
 
  batteryLevel() {
  }
 
  deviceType() {
  }
 
  deviceStatus() {
  }
 
  setAirLevel() {
  }
};
 
class LovesenseSerial extends LovesenseBase {
  constructor(port_addr) {
    super();
    if (port_addr === undefined) {
      throw new Error('LovesenseSerial requires a serial port address!');
    }
    Eif (typeof(port_addr) !== 'string') {
      throw new Error('LovesenseSerial requires a string as serial port address!');
    }
    this._port_addr = port_addr;
  }
 
    open() {
    // Requiring the module here means it's not loaded until a Lovesense
    // object is created, so if we just want to use the emulator or websockets,
    // we don't require serial to come along with it.
    const SerialPort = require('serialport');
    const opts = {
      // It's bluetooth, so port settings don't matter.
      autoOpen: false,
      // Everything is strings! Easy!
      parser: SerialPort.parsers.readline('\n')
    };
    return new Promise((resolve, reject) => {
      try {
        this._port = new SerialPort(this._port_addr, opts);
      }
      catch (err) {
        reject(err);
        throw(err);
      }
      this._port.open((err) => {
        if (err) {
          reject(err);
          throw(err);
        }
      });
      this._port.on("open", (err) => {
        if (err) {
          // We'll have already rejected in the port open() callback.
          return;
        }
        resolve();
      });
    });
  }
 
  close() {
    if (!this._port) {
      return new Promise((resolve, reject) => {
        resolve();
      });
    }
    this._port.close();
    return new Promise((resolve, reject) => {
      this._port.on("close", (err) => {
        if (err) {
          reject(err);
          return;
        }
        resolve();
      });
    });
  }
};
 
module.exports = {
  LovesenseSerial: LovesenseSerial
};