all files / Insteon/ Motion.js

77.42% Statements 96/124
52.94% Branches 36/68
90.91% Functions 10/11
77.42% Lines 96/124
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259                                                                                                                                                                                                                                                                                                                                      
var util = require('util');
var events = require('events');
var Q = require('q');
var debug = require('debug')('home-controller:insteon:motion');
 
var utils = require('./utils');
var toByte = utils.toByte;
var assignDefaults = utils.assignDefaults;
 
function Motion(id, insteon) {
  this.id = id;
  this.insteon = insteon;
 
  this.emitOnAck = true;
}
 
util.inherits(Motion, events.EventEmitter);
 
Motion.prototype.status = function(wait, next) {
  Iif(typeof wait === 'function') {
    next = wait;
    wait = true;
  }
 
  Eif(wait === null || wait === undefined) {
    wait = true;
  }
 
  var cmd = {
    cmd1: '2E',
    cmd2: '00',
    extended: true
  };
 
  var insteon = this.insteon;
  var id = this.id;
 
 
  var details = {};
  var data;
 
  var deferred = Q.defer();
 
  function status() {
    deferred.resolve(
      insteon.directCommand(id, cmd)
      .then(function (status) {
        data = status.response.extended.userData;
 
        details.ledLevel = parseInt(data[2], 16);
        details.clearTimer = (parseInt(data[3], 16) + 1) * 30; // secondes
        details.duskThreshold = Math.floor(parseInt(data[4], 16) * 100 / 255);
 
        var optionFlags = parseInt(data[5], 16);
        details.options = {
          occupancyMode: !!(optionFlags & 0x10),
          ledOn: !!(optionFlags & 0x08),
          nightMode: !(optionFlags & 0x04),
          onOnlyMode: !(optionFlags & 0x02)
        };
 
        var jumperFlags = parseInt(data[8], 16);
        details.jumpers = {
          j2: !(jumperFlags & 0x08),
          j3: !(jumperFlags & 0x04),
          j4: !(jumperFlags & 0x02),
          j5: !(jumperFlags & 0x01)
        };
        details.lightLevel = Math.floor(parseInt(data[10], 16) * 100 / 255);
        details.batteryLevel = parseInt(data[11], 16) / 10;
 
        return details;
      })
    );
  }
 
  Eif(wait) {
    this.once('command', status);
  } else {
    status();
  }
 
  return deferred.promise.nodeify(next);
};
 
Motion.prototype.setWaitCmd = function (wait, userData, next) {
 
  var insteon = this.insteon;
 
  var deferred = Q.defer();
 
  var id = this.id;
 
 
  var cmd = {
    cmd1: '2E',
    cmd2: '00',
    extended: true,
    isStandardResponse: true,
    userData: userData
  };
 
 
  function setOptions() {
    deferred.resolve(insteon.directCommand(id, cmd));
  }
 
  Eif(wait) {
    this.once('command', setOptions);
  } else {
    setOptions();
  }
 
  return deferred.promise.nodeify(next);
};
 
Motion.prototype.options = function (options, wait, next) {
  Eif(typeof options !== 'object') {
    next = wait;
    wait = options;
    options = null;
  }
 
  Iif(typeof wait === 'function') {
    next = wait;
    wait = true;
  }
 
  Eif(wait === null || wait === undefined) {
    wait = true;
  }
 
  var defaults = {
    occupancyMode: false,
    ledOn: true,
    nightMode: false,
    onOnlyMode: false
  };
 
  options = assignDefaults(defaults, options);
 
  var optionBytes = toByte(
    (options.occupancyMode ? 0x10 : 0) |
    (options.ledOn ? 0x08 : 0) |
    (options.nightMode ? 0 : 0x04) |
    (options.onOnlyMode ? 0 : 0x02)
  );
 
 
 
  var userData = ['00', '05', optionBytes];
 
  return this.setWaitCmd(wait, userData, next);
 
};
 
 
 
Motion.prototype.clearTimer = function(timeout, wait, next) {
  Iif(typeof timeout !== 'number') {
    next = wait;
    wait = timeout;
    timeout = 30; // default timeout
  }
 
  Iif(typeof wait === 'function') {
    next = wait;
    wait = true;
  }
 
  Eif(wait === null || wait === undefined) {
    wait = true;
  }
 
  timeout = Math.ceil((timeout / 30)) - 1;
  timeout = timeout < 0 ? 0 : timeout;
 
  timeout = toByte(timeout);
 
  var userData = ['00', '03', timeout];
 
  return this.setWaitCmd(wait, userData, next);
};
 
Motion.prototype.duskThreshold = function(threshold, wait, next) {
  Iif(typeof threshold !== 'number' && typeof threshold !== 'string') {
    next = wait;
    wait = threshold;
    threshold = 50; // default threshold
  }
 
  Iif(typeof wait === 'function') {
    next = wait;
    wait = true;
  }
 
  Eif(wait === null || wait === undefined) {
    wait = true;
  }
 
  Iif(threshold === 'alwaysDay') {
    threshold = 0;
  }
 
  Iif(threshold === 'alwaysNight') {
    threshold = 100;
  }
 
  Iif(typeof threshold === 'string') {
    threshold = 50; // default threshold
  }
 
  threshold = Math.ceil((threshold / 100 * 255));
  threshold = threshold < 0 ? 0 : threshold;
  threshold = threshold > 255 ? 255 : threshold;
 
  threshold = toByte(threshold);
 
  var userData = ['00', '04', threshold];
 
  return this.setWaitCmd(wait, userData, next);
};
 
 
Motion.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 (group) {
  case 1:
    if(cmd1 === '11') {
      this.emit('motion');
    } else Eif(cmd1 === '13') {
      this.emit('clear');
    }
    break;
  case 2:
    if(cmd1 === '11') {
      this.emit('dusk');
    } else if(cmd1 === '13') {
      this.emit('dawn');
    }
    break;
  case 3:
    this.emit('battery');
    break;
  default:
    debug('No event for command - %s, %s', cmd1, group);
  }
};
 
 
Motion.prototype.cancelPending = function() {
  this.insteon.cancelPending(this.id);
};
 
 
module.exports = Motion;