diff --git a/src/lifx/packet.js b/src/lifx/packet.js index a3bd7d8..090ef05 100644 --- a/src/lifx/packet.js +++ b/src/lifx/packet.js @@ -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; diff --git a/src/lifx/packets/echoRequest.js b/src/lifx/packets/echoRequest.js index 37bdb7a..4eee3f6 100644 --- a/src/lifx/packets/echoRequest.js +++ b/src/lifx/packets/echoRequest.js @@ -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; diff --git a/src/lifx/packets/echoResponse.js b/src/lifx/packets/echoResponse.js index e8fbee4..299d1f7 100644 --- a/src/lifx/packets/echoResponse.js +++ b/src/lifx/packets/echoResponse.js @@ -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; diff --git a/src/lifx/packets/getColorZones.js b/src/lifx/packets/getColorZones.js index dd8f4af..f09afe4 100644 --- a/src/lifx/packets/getColorZones.js +++ b/src/lifx/packets/getColorZones.js @@ -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; diff --git a/src/lifx/packets/getCountZone.js b/src/lifx/packets/getCountZone.js index 249bcab..165c798 100644 --- a/src/lifx/packets/getCountZone.js +++ b/src/lifx/packets/getCountZone.js @@ -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; diff --git a/src/lifx/packets/getTileState64.js b/src/lifx/packets/getTileState64.js new file mode 100644 index 0000000..bc375d7 --- /dev/null +++ b/src/lifx/packets/getTileState64.js @@ -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; diff --git a/src/lifx/packets/setColor.js b/src/lifx/packets/setColor.js index 385490d..a9c32df 100644 --- a/src/lifx/packets/setColor.js +++ b/src/lifx/packets/setColor.js @@ -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; diff --git a/src/lifx/packets/setColorZones.js b/src/lifx/packets/setColorZones.js index 6c01269..8851d04 100644 --- a/src/lifx/packets/setColorZones.js +++ b/src/lifx/packets/setColorZones.js @@ -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; diff --git a/src/lifx/packets/setInfrared.js b/src/lifx/packets/setInfrared.js index a0d251d..1803736 100644 --- a/src/lifx/packets/setInfrared.js +++ b/src/lifx/packets/setInfrared.js @@ -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; diff --git a/src/lifx/packets/setLabel.js b/src/lifx/packets/setLabel.js index 2985080..d6d41fe 100644 --- a/src/lifx/packets/setLabel.js +++ b/src/lifx/packets/setLabel.js @@ -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; diff --git a/src/lifx/packets/setPower.js b/src/lifx/packets/setPower.js index dff62ce..905ddf3 100644 --- a/src/lifx/packets/setPower.js +++ b/src/lifx/packets/setPower.js @@ -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; diff --git a/src/lifx/packets/setRgbw.js b/src/lifx/packets/setRgbw.js index 7b45247..48c9d1d 100644 --- a/src/lifx/packets/setRgbw.js +++ b/src/lifx/packets/setRgbw.js @@ -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; diff --git a/src/lifx/packets/setTileState64.js b/src/lifx/packets/setTileState64.js new file mode 100644 index 0000000..f40ed1e --- /dev/null +++ b/src/lifx/packets/setTileState64.js @@ -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; diff --git a/src/lifx/packets/setWaveform.js b/src/lifx/packets/setWaveform.js index 777b670..a717d2b 100644 --- a/src/lifx/packets/setWaveform.js +++ b/src/lifx/packets/setWaveform.js @@ -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; diff --git a/src/lifx/packets/stateAmbientLight.js b/src/lifx/packets/stateAmbientLight.js index 137c9c2..cc6ffdc 100644 --- a/src/lifx/packets/stateAmbientLight.js +++ b/src/lifx/packets/stateAmbientLight.js @@ -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; diff --git a/src/lifx/packets/stateDeviceChain.js b/src/lifx/packets/stateDeviceChain.js new file mode 100644 index 0000000..9233726 --- /dev/null +++ b/src/lifx/packets/stateDeviceChain.js @@ -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; diff --git a/src/lifx/packets/stateGroup.js b/src/lifx/packets/stateGroup.js index b18ccea..f30aac2 100644 --- a/src/lifx/packets/stateGroup.js +++ b/src/lifx/packets/stateGroup.js @@ -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; diff --git a/src/lifx/packets/stateHostFirmware.js b/src/lifx/packets/stateHostFirmware.js index 2e36443..769e0ff 100644 --- a/src/lifx/packets/stateHostFirmware.js +++ b/src/lifx/packets/stateHostFirmware.js @@ -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; diff --git a/src/lifx/packets/stateHostInfo.js b/src/lifx/packets/stateHostInfo.js index ab5ff03..703a2a7 100644 --- a/src/lifx/packets/stateHostInfo.js +++ b/src/lifx/packets/stateHostInfo.js @@ -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; diff --git a/src/lifx/packets/stateInfrared.js b/src/lifx/packets/stateInfrared.js index 07f4baf..7bc05e9 100644 --- a/src/lifx/packets/stateInfrared.js +++ b/src/lifx/packets/stateInfrared.js @@ -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; diff --git a/src/lifx/packets/stateLabel.js b/src/lifx/packets/stateLabel.js index 55a9129..05f5177 100644 --- a/src/lifx/packets/stateLabel.js +++ b/src/lifx/packets/stateLabel.js @@ -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; diff --git a/src/lifx/packets/stateLight.js b/src/lifx/packets/stateLight.js index 1df17a2..61f8e94 100644 --- a/src/lifx/packets/stateLight.js +++ b/src/lifx/packets/stateLight.js @@ -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; diff --git a/src/lifx/packets/stateLocation.js b/src/lifx/packets/stateLocation.js index 1a2636c..adad12e 100644 --- a/src/lifx/packets/stateLocation.js +++ b/src/lifx/packets/stateLocation.js @@ -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; diff --git a/src/lifx/packets/stateMultiZone.js b/src/lifx/packets/stateMultiZone.js index 815b0ca..952d433 100644 --- a/src/lifx/packets/stateMultiZone.js +++ b/src/lifx/packets/stateMultiZone.js @@ -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; diff --git a/src/lifx/packets/stateOwner.js b/src/lifx/packets/stateOwner.js index 42adfcf..fab5716 100644 --- a/src/lifx/packets/stateOwner.js +++ b/src/lifx/packets/stateOwner.js @@ -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; diff --git a/src/lifx/packets/statePower.js b/src/lifx/packets/statePower.js index 8f7c7e7..21347f3 100644 --- a/src/lifx/packets/statePower.js +++ b/src/lifx/packets/statePower.js @@ -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; diff --git a/src/lifx/packets/stateService.js b/src/lifx/packets/stateService.js index 249f751..e5aa620 100644 --- a/src/lifx/packets/stateService.js +++ b/src/lifx/packets/stateService.js @@ -48,7 +48,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; diff --git a/src/lifx/packets/stateTemperature.js b/src/lifx/packets/stateTemperature.js index d45fd3f..aacbaa7 100644 --- a/src/lifx/packets/stateTemperature.js +++ b/src/lifx/packets/stateTemperature.js @@ -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; diff --git a/src/lifx/packets/stateTileState64.js b/src/lifx/packets/stateTileState64.js new file mode 100644 index 0000000..4d790f7 --- /dev/null +++ b/src/lifx/packets/stateTileState64.js @@ -0,0 +1,53 @@ +'use strict'; + +const Packet = { + size: + 1 + 1 + 1 + 1 + 1 + + 64 * (2 + 2 + 2 + 2), + HSBK: { + 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}; + } + } +}; + +/** + * 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 stateTileState64 LIFX packet:${buf.length}:${this.size}`); + } + let offset = 0; + const obj = {}; + obj.tileIndex = buf.readUInt8(offset); + offset += 1; + obj.reserved = buf.readUInt8(offset); + offset += 1; + obj.x = buf.readUInt8(offset); + offset += 1; + obj.y = buf.readUInt8(offset); + offset += 1; + obj.width = buf.readUInt8(offset); + offset += 1; + obj.colors = new Array(64).fill(undefined).map(() => { + const ret = Packet.HSBK.toObject(buf, offset); + offset = ret.offset; + return ret.hsbk; + }); + return obj; +}; + +module.exports = Packet; + diff --git a/src/lifx/packets/stateVersion.js b/src/lifx/packets/stateVersion.js index 4d5565e..a804c3b 100644 --- a/src/lifx/packets/stateVersion.js +++ b/src/lifx/packets/stateVersion.js @@ -43,7 +43,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; diff --git a/src/lifx/packets/stateWifiFirmware.js b/src/lifx/packets/stateWifiFirmware.js index a16f85e..062d7ec 100644 --- a/src/lifx/packets/stateWifiFirmware.js +++ b/src/lifx/packets/stateWifiFirmware.js @@ -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; diff --git a/src/lifx/packets/stateWifiInfo.js b/src/lifx/packets/stateWifiInfo.js index 2ed2c25..5cc7606 100644 --- a/src/lifx/packets/stateWifiInfo.js +++ b/src/lifx/packets/stateWifiInfo.js @@ -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; diff --git a/src/lifx/packets/stateZone.js b/src/lifx/packets/stateZone.js index d7e8e29..bbd7c29 100644 --- a/src/lifx/packets/stateZone.js +++ b/src/lifx/packets/stateZone.js @@ -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;