all files / Insteon/ IOLinc.js

86.44% Statements 51/59
75% Branches 18/24
88.89% Functions 8/9
86.44% Lines 51/59
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                                                                                                                      
var Q = require('q');
var events = require('events');
var util = require('util');
var debug = require('debug')('home-controller:insteon:iolinc');
 
function IOLinc(id, insteon) {
  this.id = id;
  this.insteon = insteon;
 
  this.emitOnAck = true;
}
 
util.inherits(IOLinc, events.EventEmitter);
 
IOLinc.prototype.relayOn = function (next) {
  var id = this.id;
  return this.insteon.directCommand(id, '11', 'FF', next);
};
 
IOLinc.prototype.relayOff = function (next) {
  var id = this.id;
  return this.insteon.directCommand(id, '13', next);
};
 
 
IOLinc.prototype.status = function(next) {
 
  var insteon = this.insteon;
  var id = this.id;
 
  var deferred = Q.defer();
  var stat = {};
  deferred.resolve(
    insteon.directCommand(id, '19', '00') // Relay status
    .then(function (status) {
      Iif(!status || !status.response || !status.response.standard) {
        debug('No response for IO Linc Relay request for device %s', id);
      } else {
        var relayStatus =  parseInt(status.response.standard.command2, 16);
        stat.relay = relayStatus === 0 ? 'off' : 'on';
      }
      return insteon.directCommand(id, '19', '01'); // Sensor status
    })
    .then(function (status) {
      Iif(!status || !status.response || !status.response.standard) {
        debug('No response for IO Linc Sensor request for device %s', id);
      } else {
        var sensorStatus =  parseInt(status.response.standard.command2, 16);
        stat.sensor = sensorStatus === 0 ? 'off' : 'on';
      }
      return stat;
    })
  );
  return deferred.promise.nodeify(next);
};
 
 
IOLinc.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 + group) {
  case '110':
    this.emit('relayOn', group);
    break;
  case '111':
    this.emit('sensorOn', group);
    break;
  case '130':
    this.emit('relayOff', group);
    break;
  case '131':
    this.emit('sensorOff', group);
    break;
  default:
    debug('No event for command - %s', cmd1);
  }
};
 
IOLinc.prototype.handleAck = function (cmd1, cmd2) {
 
  Iif(!this.emitOnAck) {
    return;
  }
 
  debug('Emitting ACK command for device (%s) - cmd1: %s, cmd2: %s', this.id, cmd1, cmd2);
 
  this.emit('command', null, cmd1, cmd2);
  switch (cmd1) {
  case '11': // turnOn
    this.emit('relayOn');
    break;
  case '13': // turnOff
    this.emit('relayOff');
    break;
  default:
    debug('No event for command - %s', cmd1);
  }
 
};
 
 
 
IOLinc.prototype.cancelPending = function() {
  this.insteon.cancelPending(this.id);
};
 
 
module.exports = IOLinc;