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

98.08% Statements 51/52
50% Branches 1/2
100% Functions 3/3
98.08% Lines 51/52
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          16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16×       16× 16×       16× 16× 16× 16× 16× 16× 16× 16×                       16× 16× 16×      
'use strict';
 
var Packet = {
  size: 1 + 16 * (2 + 2 + 2 + 2 + 4 + 4 + 1 + 1 + 1 + 4 + 4 + 4 + 8 + 8 + 2 + 2 + 4) + 1,
  Tile: {
    toObject: function toObject(buf, offset) {
      var tile = {};
      tile.accelMeasX = buf.readUInt16LE(offset);
      offset += 2;
      tile.accelMeasY = buf.readUInt16LE(offset);
      offset += 2;
      tile.accelMeasZ = buf.readUInt16LE(offset);
      offset += 2;
      tile.reserved0 = buf.readUInt16LE(offset);
      offset += 2;
      tile.userX = buf.readUInt32LE(offset);
      offset += 4;
      tile.userY = buf.readUInt32LE(offset);
      offset += 4;
      tile.width = buf.readUInt8(offset);
      offset += 1;
      tile.height = buf.readUInt8(offset);
      offset += 1;
      tile.reserved1 = buf.readUInt8(offset);
      offset += 1;
      tile.deviceVersionVendor = buf.readUInt32LE(offset);
      offset += 4;
      tile.deviceVersionProduct = buf.readUInt32LE(offset);
      offset += 4;
      tile.deviceVersionVersion = buf.readUInt32LE(offset);
      offset += 4;
      tile.firmwareBuild = {
        low: buf.readUInt32LE(offset),
        high: buf.readUInt32LE(offset + 4)
      };
      offset += 8;
      tile.reserved2 = {
        low: buf.readUInt32LE(offset),
        high: buf.readUInt32LE(offset + 4)
      };
      offset += 8;
      tile.firmwareVersionMinor = buf.readUInt16LE(offset);
      offset += 2;
      tile.firmwareVersionMajor = buf.readUInt16LE(offset);
      offset += 2;
      tile.reserved3 = buf.readUInt32LE(offset);
      offset += 4;
      return { offset, tile };
    }
  }
};
 
/**
 * 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 stateDeviceChain LIFX packet:${buf.length}:${this.size}`);
  }
  var offset = 0;
  var obj = {};
  obj.startIndex = buf.readUInt8(offset);
  offset += 1;
  obj.tileDevices = new Array(16).fill(undefined).map(function () {
    var ret = Packet.Tile.toObject(buf, offset);
    offset = ret.offset;
    return ret.tile;
  });
  obj.totalCount = buf.readUInt8(offset);
  offset += 1;
  return obj;
};
 
module.exports = Packet;