Skip to content

Commit

Permalink
remove of deprected "new Buffer"
Browse files Browse the repository at this point in the history
  • Loading branch information
mabels committed Sep 23, 2019
1 parent d682ab9 commit acad593
Show file tree
Hide file tree
Showing 33 changed files with 280 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/lifx/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet header buffer
*/
Packet.headerToBuffer = function(obj) {
const buf = new Buffer(36);
const buf = Buffer.alloc(36);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/echoRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/echoResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/getColorZones.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Packet = {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/getCountZone.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
44 changes: 44 additions & 0 deletions src/lifx/packets/getTileState64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const {validate} = require('../../lifx');

const Packet = {
size: 1 + 1 + 1 + 1 + 1 + 1
};

/**
* Converts the given packet specific object into a packet
* @param {Object} obj object with configuration data
* @param {Number} obj.tileIndex an 8bit value
* @param {Number} obj.length an 8bit value
* @param {Number} obj.reserved an 8bit value
* @param {Number} obj.x an 8bit value
* @param {Number} obj.y an 8bit value
* @param {Number} obj.width an 8bit value
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

['tileIndex', 'length', 'reserved', 'x', 'y', 'width'].forEach((field) => {
validate.isUInt8(obj[field], `getTileState64:${field}`);
});
buf.writeUInt8(obj.tileIndex, offset);
offset += 1;
buf.writeUInt8(obj.length, offset);
offset += 1;
buf.writeUInt8(obj.reserved, offset);
offset += 1;
buf.writeUInt8(obj.x, offset);
offset += 1;
buf.writeUInt8(obj.y, offset);
offset += 1;
buf.writeUInt8(obj.width, offset);
offset += 1;

return buf;
};

module.exports = Packet;
2 changes: 1 addition & 1 deletion src/lifx/packets/setColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/setColorZones.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/setInfrared.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/setLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/setPower.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/setRgbw.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
75 changes: 75 additions & 0 deletions src/lifx/packets/setTileState64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

const {validate} = require('../../lifx');

const Packet = {
size: (obj) => 1 + 1 + 1 + 1 + 1 + 1 + 4 +
(obj.colors.length * (2 + 2 + 2 + 2)),
HSBK: {
toBuffer: (obj, buf, offset) => {
validate.isUInt16(obj.hue, 'setTileState64:HSBK:hue');
buf.writeUInt16LE(obj.hue, offset);
offset += 2;

validate.isUInt16(obj.saturation, 'setTileState64:HSBK:saturation');
buf.writeUInt16LE(obj.saturation, offset);
offset += 2;

validate.isUInt16(obj.brightness, 'setTileState64:HSBK:brightness');
buf.writeUInt16LE(obj.brightness, offset);
offset += 2;

validate.isUInt16(obj.kelvin, 'setTileState64:HSBK:kelvin');
buf.writeUInt16LE(obj.kelvin, offset);
offset += 2;

return offset;
}
}
};

/**
* Converts the given packet specific object into a packet
* @param {Object} obj object with configuration data
* @param {Number} obj.tileIndex 8bit value
* @param {Number} obj.length 8bit value
* @param {Number} obj.reserved 8bit value
* @param {Number} obj.x 8bit value
* @param {Number} obj.y 8bit value
* @param {Number} obj.width 8bit value
* @param {Number} obj.duration 8bit value
* @param {Number} [obj.duration] transition time in milliseconds
* @param {Array} obj.colors an array of HSBK values
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = Buffer.alloc(Packet.size(obj));
buf.fill(0);
let offset = 0;

['tileIndex', 'length', 'reserved', 'x', 'y', 'width'].forEach((field) => {
validate.isUInt8(obj[field], `setTileState64:${field}`);
});
// obj.stream field has unknown function so leave it as 0
buf.writeUInt8(obj.tileIndex, offset);
offset += 1;
buf.writeUInt8(obj.length, offset);
offset += 1;
buf.writeUInt8(obj.reserved || 0, offset);
offset += 1;
buf.writeUInt8(obj.x, offset);
offset += 1;
buf.writeUInt8(obj.y, offset);
offset += 1;
buf.writeUInt8(obj.width, offset);
offset += 1;
validate.isUInt32(obj.duration, 'setTileState64:duration');
buf.writeUInt32LE(obj.duration, offset);
offset += 4;
obj.colors.forEach((color) => {
offset = Packet.HSBK.toBuffer(color, buf, offset);
});
return buf;
};

module.exports = Packet;
2 changes: 1 addition & 1 deletion src/lifx/packets/setWaveform.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/stateAmbientLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
79 changes: 79 additions & 0 deletions src/lifx/packets/stateDeviceChain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

const 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(buf, offset) {
const 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) {
if (buf.length !== this.size) {
throw new Error(`Invalid length given for stateDeviceChain LIFX packet:${buf.length}:${this.size}`);
}
let offset = 0;
const obj = {};
obj.startIndex = buf.readUInt8(offset);
offset += 1;
obj.tileDevices = new Array(16).fill(undefined).map(() => {
const ret = Packet.Tile.toObject(buf, offset);
offset = ret.offset;
return ret.tile;
});
obj.totalCount = buf.readUInt8(offset);
offset += 1;
return obj;
};

module.exports = Packet;
2 changes: 1 addition & 1 deletion src/lifx/packets/stateGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/stateHostFirmware.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/stateHostInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/stateInfrared.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/stateLabel.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/stateLight.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/stateLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/stateMultiZone.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer();
const buf = Buffer.alloc();
buf.fill(0);
let offset = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/lifx/packets/stateOwner.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Packet.toObject = function(buf) {
* @return {Buffer} packet
*/
Packet.toBuffer = function(obj) {
const buf = new Buffer(this.size);
const buf = Buffer.alloc(this.size);
buf.fill(0);
let offset = 0;

Expand Down
Loading

0 comments on commit acad593

Please sign in to comment.