All files / src/lifx/packets stateInfo.js

23.8% Statements 5/21
0% Branches 0/4
0% Functions 0/2
23.8% Lines 5/21

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    1x   1x       1x                               1x                                         1x  
'use strict';
 
const utils = require('../../lifx').utils;
 
const Packet = {
  size: 24
};
 
Packet.parseNanoseconds = function(buf) {
  if (buf.length !== 8) {
    throw new Error('Invalid length given for nanoseconds field');
  }
 
  const low = buf.readUInt32LE(0);
  const high = buf.readUInt32LE(4);
 
  return (high * 2 * 32 + low) / 1.0E9;
};
 
/**
 * 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 stateInfo LIFX packet');
  }
 
  obj.time = this.parseNanoseconds(utils.readUInt64LE(buf, offset));
  offset += 8;
 
  obj.uptime = this.parseNanoseconds(utils.readUInt64LE(buf, offset));
  offset += 8;
 
  obj.downtime = this.parseNanoseconds(utils.readUInt64LE(buf, offset));
  offset += 8;
 
  return obj;
};
 
module.exports = Packet;