all files / Insteon/ GarageDoor.js

100% Statements 50/50
100% Branches 16/16
100% Functions 13/13
100% Lines 50/50
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                                                                                       
//var utils = require('./utils');
var q = require('q');
var debug = require('debug')('home-controller:insteon:garage');
var util = require('util');
var events = require('events');
 
 
function GarageDoor(id, insteon) {
  this.id = id;
  this.insteon = insteon;
  this.isLockedOut = false;
  this.LOCKOUT_TIME = 15000; // 15 seconds
}
 
util.inherits(GarageDoor, events.EventEmitter);
 
GarageDoor.prototype.open = function () {
  return toggle(this, 'open');
};
 
GarageDoor.prototype.close = function () {
  return toggle(this, 'closed');
};
 
function toggle(self, state) {
  if (self.isLockedOut) {
    return q.fcall(function () {
      return false;
    });
  }
 
  self.isLockedOut = true;
 
  return self.status()
    .then(function (status) {
      if (status !== state) {
        self.emit(state === 'open' ? 'opening' : 'closing');
 
        setTimeout(function () {
          self.isLockedOut = false;
          self.emit(state);
        }, self.LOCKOUT_TIME);
 
        return self.insteon.directCommand(self.id, '11', 'ff')
          .then(function () {
            return true;
          });
      } else {
        self.isLockedOut = false;
        return q.fcall(function () {
          return false;
        });
      }
    });
}
 
GarageDoor.prototype.status = function () {
  return this.insteon.directCommand(this.id, '19', '01')
    .then(function (status) {
      if (!status || !status.response || !status.response.standard) {
        debug('No response for garage door status for device %s', this.id);
        return null;
      }
 
      if (parseInt(status.response.standard.command2)) {
        return 'closed';
      } else {
        return 'open';
      }
    });
};
 
GarageDoor.prototype.cancelPending = function () {
  this.insteon.cancelPending(this.id);
};
 
GarageDoor.prototype.handleAllLinkBroadcast = function (group, cmd1, cmd2) {
 
  debug('Emitting BC command for device (%s) - group: %s, cmd1: %s, cmd2: %s', this.id, group, cmd1, cmd2);
  this.emit('command', group, cmd1, cmd2);
 
  switch (cmd1) {
    case '11':
      this.emit('open', group);
      break;
    case '13':
      this.emit('close', group);
      break;
    default:
      debug('No event for command - %s', cmd1);
  }
};
 
module.exports = GarageDoor;