All files / src/lifx/packets hsbk.js

100% Statements 28/28
100% Branches 0/0
100% Functions 2/2
100% Lines 28/28

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    1x   1x                                                 1x 385x 385x 385x 385x 385x 385x 385x 385x 385x 385x                             1x 323x 323x 323x 323x 323x 323x 323x 323x 323x 323x 323x 323x 323x     1x  
'use strict';
 
const {validate} = require('../../lifx');
 
const Packet = {
  size: 8
};
 
/**
 * @typedef {Object} HSBK
 * @property {Number} hsbk.hue - hue value
 * @property {Number} hsbk.saturation - saturation value
 * @property {Number} hsbk.brightness - brightness value
 * @property {Number} hsbk.kelvin - kelvin value
 */
 
/**
 * @typedef {Object} OffsetHSBK
 * @property {Number} offset - offset after the buffer
 * @property {HSBK} hsbk - HSBK value
 *
 */
 
/**
 * Converts the given packet specific object into a packet
 * @param  {Buffer} buf hsbk as buffer
 * @param  {Number} offset offset to the buffer
 * @return {OffsetHSBK} packet
 */
Packet.toObject = function(buf, offset) {
  const 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};
};
 
/**
 * @typedef {Object} OffsetBuffer
 * @property {number} offset - offset after the buffer
 * @property {Buffer} buffer - buffer
 */
/**
 * Converts the given packet specific object into a packet
 * @param  {Buffer} buffer output buffer
 * @param  {Number} offset offset in the output buffer
 * @param  {HSBK} hsbk offset in the output buffer
 * @return {OffsetBuffer} packet
 */
Packet.toBuffer = function(buffer, offset, hsbk) {
  validate.isUInt16(hsbk.hue);
  validate.isUInt16(hsbk.saturation);
  validate.isUInt16(hsbk.brightness);
  validate.isUInt16(hsbk.kelvin);
  buffer.writeUInt16LE(hsbk.hue, offset);
  offset += 2;
  buffer.writeUInt16LE(hsbk.saturation, offset);
  offset += 2;
  buffer.writeUInt16LE(hsbk.brightness, offset);
  offset += 2;
  buffer.writeUInt16LE(hsbk.kelvin, offset);
  offset += 2;
  return {offset, buffer};
};
 
module.exports = Packet;