All files / src/lifx/packets setColorZones.js

53.03% Statements 35/66
62.79% Branches 27/43
50% Functions 1/2
53.03% Lines 35/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    1x   1x                 1x                                                                                             1x 1x 1x 1x   1x     1x 1x   1x     1x 1x   1x     1x 1x   1x     1x 1x   1x     1x 1x   1x     1x     1x 1x     1x 1x   1x   1x     1x     1x 1x   1x     1x  
'use strict';
 
const {constants} = require('../../lifx');
 
const Packet = {
  size: 15
};
 
/**
 * Converts packet specific data from a buffer to an object
 * @param  {Buffer} buf Buffer containing only packet specific data no header
 * @return {Object}     Information contained in packet
 */
Packet.toObject = function(buf) {
  const obj = {};
  let offset = 0;
 
  if (buf.length !== this.size) {
    throw new Error('Invalid length given for setColorZones LIFX packet');
  }
 
  obj.startIndex = buf.readUInt8(offset);
  offset += 1;
 
  obj.endIndex = buf.readUInt8(offset);
  offset += 1;
 
  obj.color = {};
  obj.color.hue = buf.readUInt16LE(offset);
  offset += 2;
  obj.color.saturation = buf.readUInt16LE(offset);
  offset += 2;
  obj.color.brightness = buf.readUInt16LE(offset);
  offset += 2;
  obj.color.kelvin = buf.readUInt16LE(offset);
  offset += 2;
 
  obj.duration = buf.readUInt32LE(offset);
  offset += 4;
 
  obj.apply = buf.readUInt8(offset);
  offset += 1;
 
  return obj;
};
 
/**
 * Converts the given packet specific object into a packet
 * @param  {Object} obj object with configuration data
 * @param  {Number} obj.startIndex between 0 and 255
 * @param  {Number} obj.endIndex between 0 and 255
 * @param  {Object} obj.color an object with colors to set
 * @param  {Number} obj.color.hue between 0 and 65535
 * @param  {Number} obj.color.saturation between 0 and 65535
 * @param  {Number} obj.color.brightness between 0 and 65535
 * @param  {Number} obj.color.kelvin between 2500 and 9000
 * @param  {Number} [obj.duration] transition time in milliseconds
 * @param  {Number} obj.apply value 0 (NO_APPLY), 1 (APPLY) or 2 (APPLY_ONLY)
 * @return {Buffer} packet
 */
Packet.toBuffer = function(obj) {
  const buf = Buffer.alloc(this.size);
  buf.fill(0);
  let offset = 0;
 
  Iif (typeof obj.startIndex !== 'number' && obj.startIndex < 0 || obj.startIndex > 255) {
    throw new RangeError('Invalid startIndex value given for setColorZones LIFX packet, must be a number between 0 and 255');
  }
  buf.writeUInt8(obj.startIndex, offset);
  offset += 1;
 
  Iif (typeof obj.endIndex !== 'number' && obj.endIndex < 0 || obj.endIndex > 255) {
    throw new RangeError('Invalid endIndex value given for setColorZones LIFX packet, must be a number between 0 and 255');
  }
  buf.writeUInt8(obj.endIndex, offset);
  offset += 1;
 
  Iif (typeof obj.hue !== 'number' || obj.hue < 0 || obj.hue > 65535) {
    throw new RangeError('Invalid color hue given for setColorZones LIFX packet, must be a number between 0 and 65535');
  }
  buf.writeUInt16LE(obj.hue, offset);
  offset += 2;
 
  Iif (typeof obj.saturation !== 'number' || obj.saturation < 0 || obj.saturation > 65535) {
    throw new RangeError('Invalid color saturation given for setColorZones LIFX packet, must be a number between 0 and 65535');
  }
  buf.writeUInt16LE(obj.saturation, offset);
  offset += 2;
 
  Iif (typeof obj.brightness !== 'number' || obj.brightness < 0 || obj.brightness > 65535) {
    throw new RangeError('Invalid color brightness given for setColorZones LIFX packet, must be a number between 0 and 65535');
  }
  buf.writeUInt16LE(obj.brightness, offset);
  offset += 2;
 
  Iif (obj.kelvin === undefined) {
    obj.kelvin = constants.HSBK_DEFAULT_KELVIN;
  }
  Iif (typeof obj.kelvin !== 'number' && obj.kelvin < 2500 || obj.kelvin > 9000) {
    throw new RangeError('Invalid color kelvin given for setColorZones LIFX packet, must be a number between 2500 and 9000');
  }
  buf.writeUInt16LE(obj.kelvin, offset);
  offset += 2;
 
  // Duration is 0 by default
  Eif (obj.duration !== undefined) {
    buf.writeUInt32LE(obj.duration, offset);
  }
  offset += 4;
 
  Iif (obj.apply === undefined) {
    throw new TypeError('obj.apply value must be given for setColorZones LIFX packet');
  }
  Iif (typeof obj.apply !== 'number' && obj.apply < 0 || obj.apply > 2) {
    throw new RangeError('Invalid apply value given for setColorZones LIFX packet, must be a number between 0 and 2');
  }
  buf.writeUInt8(obj.apply, offset);
  offset += 1;
 
  return buf;
};
 
module.exports = Packet;