lib/device.js
function Device(options) {

  EventEmitter.call(this);

  // Check required fields.
  if(!options || !options.id) {
    throw new Error('ID is required.');
  }

  this.options = options;
  this.id = options.id;

  // Default the options and remove whitespace.
  options.id = (options.id || '').replace(/ /g, '');
  options.key = (options.key || '').replace(/ /g, '');
  options.secret = (options.secret || '').replace(/ /g, '');

  // The MQTT topics that this device will use.
  this.commandTopic = util.format(config.topicFormatCommand, options.id);
  this.stateTopic = util.format(config.topicFormatState, options.id);

  this.mqtt = options.mqtt || new MQTT();
  this.mqtt.on('message', this.handleMessage.bind(this));

  // Only want 'connect' to be emitted the first time. Every time after
  // will only emit the reconnected event.
  var firstConnect = true;
  this.mqtt.on('connect', (function() {
    this.handleConnect();
    if(firstConnect) {
      firstConnect = false;
      this.emit('connect');
    }
    else {
      this.emit('reconnected');
    }
  }).bind(this));

  this.mqtt.on('reconnect', (function() {
    this.emit('reconnect');
  }).bind(this));

  this.mqtt.on('close', (function() {
    this.emit('close');
  }).bind(this));

  this.mqtt.on('offline', (function() {
    this.emit('offline');
  }).bind(this));

  this.mqtt.on('error', (function(err) {
    this.emit('error', err);
  }).bind(this));
}
function Device(options) {

  EventEmitter.call(this);

  // Check required fields.
  if(!options || !options.id) {
    throw new Error('ID is required.');
  }

  this.options = options;
  this.id = options.id;

  // Default the options and remove whitespace.
  options.id = (options.id || '').replace(/ /g, '');
  options.key = (options.key || '').replace(/ /g, '');
  options.secret = (options.secret || '').replace(/ /g, '');

  // The MQTT topics that this device will use.
  this.commandTopic = util.format(config.topicFormatCommand, options.id);
  this.stateTopic = util.format(config.topicFormatState, options.id);

  this.mqtt = options.mqtt || new MQTT();
  this.mqtt.on('message', this.handleMessage.bind(this));

  // Only want 'connect' to be emitted the first time. Every time after
  // will only emit the reconnected event.
  var firstConnect = true;
  this.mqtt.on('connect', (function() {
    this.handleConnect();
    if(firstConnect) {
      firstConnect = false;
      this.emit('connect');
    }
    else {
      this.emit('reconnected');
    }
  }).bind(this));

  this.mqtt.on('reconnect', (function() {
    this.emit('reconnect');
  }).bind(this));

  this.mqtt.on('close', (function() {
    this.emit('close');
  }).bind(this));

  this.mqtt.on('offline', (function() {
    this.emit('offline');
  }).bind(this));

  this.mqtt.on('error', (function(err) {
    this.emit('error', err);
  }).bind(this));
}
function Device ( options ) { EventEmitter . call ( this ) ; // Check required fields. if ( ! options || ! options . id ) { throw new Error ( 'ID is required.' ) ; } this . options = options ; this . id = options . id ; // Default the options and remove whitespace. options . id = ( options . id || '' ) . replace ( / / g , '' ) ; options . key = ( options . key || '' ) . replace ( / / g , '' ) ; options . secret = ( options . secret || '' ) . replace ( / / g , '' ) ; // The MQTT topics that this device will use. this . commandTopic = util . format ( config . topicFormatCommand , options . id ) ; this . stateTopic = util . format ( config . topicFormatState , options . id ) ; this . mqtt = options . mqtt || new MQTT ( ) ; this . mqtt . on ( 'message' , this . handleMessage . bind ( this ) ) ; // Only want 'connect' to be emitted the first time. Every time after // will only emit the reconnected event. var firstConnect = true ; this . mqtt . on ( 'connect' , ( function ( ) { this . handleConnect ( ) ; if ( firstConnect ) { firstConnect = false ; this . emit ( 'connect' ) ; } else { this . emit ( 'reconnected' ) ; } } ) . bind ( this ) ) ; this . mqtt . on ( 'reconnect' , ( function ( ) { this . emit ( 'reconnect' ) ; } ) . bind ( this ) ) ; this . mqtt . on ( 'close' , ( function ( ) { this . emit ( 'close' ) ; } ) . bind ( this ) ) ; this . mqtt . on ( 'offline' , ( function ( ) { this . emit ( 'offline' ) ; } ) . bind ( this ) ) ; this . mqtt . on ( 'error' , ( function ( err ) { this . emit ( 'error' , err ) ; } ) . bind ( this ) ) ; }
Device constructor.
Device constructor .