All files / src/lifx/packets setUserPosition.js

97.05% Statements 33/34
50% Branches 1/2
100% Functions 2/2
97.05% Lines 33/34

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    1x   1x                                 1x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x               1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     1x  
'use strict';
 
const {validate} = require('../../lifx');
 
const Packet = {
  size: 11
};
 
/**
 * @typedef {Object} SetUserPosition
 * @property {Number} - UInt8 tileIndex
 * @property {Number} - UInt16 reserved
 * @property {Number} - Float userX
 * @property {Number} - Float userY
 */
 
/**
 * Converts packet specific data from a buffer to an object
 * @param  {Buffer} buf Buffer containing only packet specific data no header
 * @return {SetUserPosition}  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}`);
  }
  let offset = 0;
  const obj = {};
  obj.tileIndex = buf.readUInt8(offset);
  offset += 1;
  obj.reserved = buf.readUInt16LE(offset);
  offset += 2;
  obj.userX = buf.readFloatLE(offset);
  offset += 4;
  obj.userY = buf.readFloatLE(offset);
  offset += 4;
  return obj;
};
 
/**
 * Converts the given packet specific object into a packet
 * @param  {SetUserPosition} obj object with configuration data
 * @return {Buffer} packet
 */
Packet.toBuffer = function(obj) {
  validate.isUInt8(obj.tileIndex);
  validate.isUInt16(obj.reserved);
  validate.isFloat(obj.userX);
  validate.isFloat(obj.userY);
  const buf = Buffer.alloc(Packet.size);
  buf.fill(0);
  let offset = 0;
  buf.writeUInt8(obj.tileIndex, offset);
  offset += 1;
  buf.writeUInt16LE(obj.reserved, offset);
  offset += 2;
  buf.writeFloatLE(obj.userX, offset);
  offset += 4;
  buf.writeFloatLE(obj.userY, offset);
  offset += 4;
  return buf;
};
 
module.exports = Packet;