diff --git a/example/interactive-cli.js b/example/interactive-cli.js index 38fc890..9fb3bf3 100644 --- a/example/interactive-cli.js +++ b/example/interactive-cli.js @@ -1,4 +1,5 @@ 'use strict'; +const readline = require('readline'); const LifxClient = require('../lib/lifx').Client; const client = new LifxClient(); @@ -53,14 +54,27 @@ console.log('Press 6 to turn the lights a bright bluish white'); console.log('Press 7 to turn the lights a bright reddish white'); console.log('Press 8 to show debug messages including network traffic'); console.log('Press 9 to hide debug messages including network traffic'); +console.log('Press hf run getHostFirmware'); +console.log('Press wf run getWifiFirmware'); +console.log('Press hi run getHostFirmware'); +console.log('Press wi run getWifiFirmware'); +console.log('Press gl run getLocation'); +console.log('Press gg run getGroup'); +console.log('Press gs run getState'); +console.log('Press dc run getDeviceChain'); console.log('Press u to request uptime from all lights'); console.log('Press r to reboot all lights'); console.log('Press 0 to exit\n'); -process.stdin.setEncoding('utf8'); -process.stdin.setRawMode(true); +const lineReader = readline.createInterface({ + input: process.stdin, + output: process.stdout, + prompt: 'lifx> ' +}); + +lineReader.prompt(); -process.stdin.on('data', function(key) { +lineReader.on('line', (key) => { if (key === '1') { client.lights().forEach(function(light) { light.on(0, function(err) { @@ -130,6 +144,54 @@ process.stdin.on('data', function(key) { } else if (key === '9') { client.setDebug(false); console.log('Debug messages are hidden'); + } else if (key === 'hf') { + client.lights().forEach(function(light) { + light.getHostFirmware((err, msg) => { + console.log('StateHostFirmware:', light.id, ':', msg); + }) + }); + } else if (key === 'wf') { + client.lights().forEach(function(light) { + light.getWifiFirmware((err, msg) => { + console.log('StateWifiFirmware:', light.id, ':', msg); + }) + }); + } else if (key === 'hi') { + client.lights().forEach(function(light) { + light.getHostInfo((err, msg) => { + console.log('StateHostInfo:', light.id, ':', msg); + }) + }); + } else if (key === 'wi') { + client.lights().forEach(function(light) { + light.getWifiInfo((err, msg) => { + console.log('StateWifiInfo:', light.id, ':', msg); + }) + }); + } else if (key === 'gl') { + client.lights().forEach(function(light) { + light.getLocation((err, msg) => { + console.log('StateLocation:', light.id, ':', msg); + }) + }); + } else if (key === 'gg') { + client.lights().forEach(function(light) { + light.getGroup((err, msg) => { + console.log('StateGroup:', light.id, ':', msg); + }) + }); + } else if (key === 'gs') { + client.lights().forEach(function(light) { + light.getState((err, msg) => { + console.log('State:', light.id, ':', msg); + }) + }); + } else if (key === 'dc') { + client.lights().forEach(function(light) { + light.getDeviceChain((err, msg) => { + console.log('StateDeviceChain:', light.id, ':', JSON.stringify(msg, null, 2)); + }) + }); } else if (key === 'u') { client.lights().forEach(function(light) { light.getUptime(function(err, uptime) { @@ -151,4 +213,5 @@ process.stdin.on('data', function(key) { client.destroy(); process.exit(); // eslint-disable-line no-process-exit } + lineReader.prompt(); }); diff --git a/src/lifx/light.js b/src/lifx/light.js index aa63cfd..5cea141 100644 --- a/src/lifx/light.js +++ b/src/lifx/light.js @@ -274,11 +274,13 @@ Light.prototype.getFirmwareVersion = function(callback) { return callback(err, null); } callback(null, pick(msg, [ + 'build', 'majorVersion', 'minorVersion' ])); }, sqnNumber); }; +Light.prototype.getHostFirmware = Light.prototype.getFirmwareVersion; /** * Requests infos from the microcontroller unit of the light @@ -297,10 +299,12 @@ Light.prototype.getFirmwareInfo = function(callback) { callback(null, pick(msg, [ 'signal', 'tx', - 'rx' + 'rx', + 'mcuTemperature' ])); }, sqnNumber); }; +Light.prototype.getHostInfo = Light.prototype.getFirmwareInfo; /** * Requests wifi infos from for the light @@ -319,7 +323,8 @@ Light.prototype.getWifiInfo = function(callback) { callback(null, pick(msg, [ 'signal', 'tx', - 'rx' + 'rx', + 'mcuTemperature' ])); }, sqnNumber); }; @@ -329,7 +334,7 @@ Light.prototype.getWifiInfo = function(callback) { * @param {Function} callback a function to accept the data */ Light.prototype.getWifiVersion = function(callback) { - validate.callback(callback, 'light getWifiVersion method'); + validate.callback(callback, 'light getWifiFirmware method'); const packetObj = packet.create('getWifiFirmware', {}, this.client.source); packetObj.target = this.id; @@ -339,11 +344,13 @@ Light.prototype.getWifiVersion = function(callback) { return callback(err, null); } return callback(null, pick(msg, [ + 'build', 'majorVersion', 'minorVersion' ])); }, sqnNumber); }; +Light.prototype.getWifiFirmware = Light.prototype.getWifiVersion; /** * Requests the label of the light @@ -397,6 +404,62 @@ Light.prototype.setLabel = function(label, callback) { this.client.send(packetObj, callback); }; +/** + * Requests the location of the light + * @param {Function} callback a function to accept the data + * @param {Boolean} [cache=false] return cached result if existent + * @return {Function} callback(err, location) + */ +Light.prototype.getLocation = function(callback, cache) { + validate.callback(callback, 'light getLocation method'); + + if (cache !== undefined && typeof cache !== 'boolean') { + throw new TypeError('LIFX light getLocation method expects cache to be a boolean'); + } + const packetObj = packet.create('getLocation', { + target: this.id + }, this.client.source); + const sqnNumber = this.client.send(packetObj); + this.client.addMessageHandler('stateLocation', function(err, msg) { + if (err) { + return callback(err, null); + } + return callback(null, pick(msg, [ + 'location', + 'label', + 'updatedAt' + ])); + }, sqnNumber); +}; + +/** + * Requests the group of the light + * @param {Function} callback a function to accept the data + * @param {Boolean} [cache=false] return cached result if existent + * @return {Function} callback(err, group) + */ +Light.prototype.getGroup = function(callback, cache) { + validate.callback(callback, 'light getGroup method'); + + if (cache !== undefined && typeof cache !== 'boolean') { + throw new TypeError('LIFX light getGroup method expects cache to be a boolean'); + } + const packetObj = packet.create('getGroup', { + target: this.id + }, this.client.source); + const sqnNumber = this.client.send(packetObj); + this.client.addMessageHandler('stateGroup', function(err, msg) { + if (err) { + return callback(err, null); + } + return callback(null, pick(msg, [ + 'group', + 'label', + 'updatedAt' + ])); + }, sqnNumber); +}; + /** * Requests ambient light value of the light * @param {Function} callback a function to accept the data