All files / src/lifx/packets stateVersion.js

20% Statements 6/30
0% Branches 0/4
0% Functions 0/2
20% Lines 6/30

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    1x 1x   1x                 1x                                                           1x                                 1x  
'use strict';
 
const {find} = require('lodash');
const {constants} = require('../../lifx');
 
const Packet = {
  size: 12
};
 
/**
 * 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;
 
  // Check length
  if (buf.length !== this.size) {
    throw new Error('Invalid length given for stateHostFirmware LIFX packet');
  }
 
  obj.vendorId = buf.readUInt32LE(offset);
  const vendor = find(constants.LIFX_VENDOR_IDS, {id: obj.vendorId});
  if (vendor !== undefined) {
    obj.vendorName = vendor.name;
  }
  offset += 4;
 
  obj.productId = buf.readUInt32LE(offset);
  offset += 4;
 
  obj.version = buf.readUInt32LE(offset);
  offset += 4;
 
  return obj;
};
 
/**
 * Converts the given packet specific object into a packet
 * @param  {Object} obj object with configuration data
 * @return {Buffer}     packet
 */
Packet.toBuffer = function(obj) {
  const buf = Buffer.alloc(this.size);
  buf.fill(0);
  let offset = 0;
 
  buf.writeUInt32LE(obj.vendor, offset);
  offset += 4;
 
  buf.writeUInt32LE(obj.product, offset);
  offset += 4;
 
  buf.writeUInt32LE(obj.version, offset);
  offset += 4;
 
  return buf;
};
 
module.exports = Packet;