all files / lib/lifx/packets/ stateTileState64.js

96.88% Statements 31/32
50% Branches 1/2
100% Functions 3/3
96.88% Lines 31/32
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          64× 64× 64× 64× 64× 64× 64× 64× 64× 64×                       64× 64× 64×      
'use strict';
 
var Packet = {
  size: 1 + 1 + 1 + 1 + 1 + 64 * (2 + 2 + 2 + 2),
  HSBK: {
    toObject: function toObject(buf, offset) {
      var hsbk = {};
      hsbk.hue = buf.readUInt16LE(offset);
      offset += 2;
      hsbk.saturation = buf.readUInt16LE(offset);
      offset += 2;
      hsbk.brightness = buf.readUInt16LE(offset);
      offset += 2;
      hsbk.kelvin = buf.readUInt16LE(offset);
      offset += 2;
      return { offset, hsbk };
    }
  }
};
 
/**
 * 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) {
  Iif (buf.length !== this.size) {
    throw new Error(`Invalid length given for stateTileState64 LIFX packet:${buf.length}:${this.size}`);
  }
  var offset = 0;
  var obj = {};
  obj.tileIndex = buf.readUInt8(offset);
  offset += 1;
  obj.reserved = buf.readUInt8(offset);
  offset += 1;
  obj.x = buf.readUInt8(offset);
  offset += 1;
  obj.y = buf.readUInt8(offset);
  offset += 1;
  obj.width = buf.readUInt8(offset);
  offset += 1;
  obj.colors = new Array(64).fill(undefined).map(function () {
    var ret = Packet.HSBK.toObject(buf, offset);
    offset = ret.offset;
    return ret.hsbk;
  });
  return obj;
};
 
module.exports = Packet;