Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the interactive-cli #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 66 additions & 3 deletions example/interactive-cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const readline = require('readline');

const LifxClient = require('../lib/lifx').Client;
const client = new LifxClient();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you add all these shortcuts because you've found them useful? I'm not using this in the CLI, only via Node.js, so if it's useful to you, it's fine to add them.

Technically, this entire CLI section could be moved to its own project right? We could create this as a monorepo and add it there. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to move it out might be a good idea. And than build the cli tool from ground up again.

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) {
Expand All @@ -151,4 +213,5 @@ process.stdin.on('data', function(key) {
client.destroy();
process.exit(); // eslint-disable-line no-process-exit
}
lineReader.prompt();
});
69 changes: 66 additions & 3 deletions src/lifx/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -319,7 +323,8 @@ Light.prototype.getWifiInfo = function(callback) {
callback(null, pick(msg, [
'signal',
'tx',
'rx'
'rx',
'mcuTemperature'
]));
}, sqnNumber);
};
Expand All @@ -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;
Expand All @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the rename?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Lifx Api name is getWifiFirmware.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This library is not supposed to be the LIFX API but a library to interface with it. I think this is just confusing. Confusing stuff does not belong in a usage example.


/**
* Requests the label of the light
Expand Down Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't realize this was missing. Nice addition!

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
Expand Down