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

update to new hue-js interface, storing username token in config file #14

Merged
merged 5 commits into from
Sep 22, 2016
Merged
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
17 changes: 1 addition & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ Usage
options
-h, --help print this message and exit
-H, --host the hostname or ip of the bridge to control
-i, --init initialize the config file at ~/.hue.json
-j, --json force output to be in json
-s, --save save the config file at ~/.hue.json, same as --init
-u, --updates check for available updates
-v, --version print the version number and exit

Expand Down Expand Up @@ -111,19 +109,6 @@ Again, `-j` if you'd like json output.
Running with the command `lights` will give us a list of all the lights
connected to the base station.

Before we continue, let's create a configuration file. In the file we can
set the default host to connect to, so we don't have to keep supplying the
`-H` argument. Run:

$ hue --host 10.0.1.218 --save
config file written to `~/.hue.json`
{
"host": "10.0.1.218"
}

Now we'll no longer have to supply the `-H` argument
with every command.

From here, we can get information about a single light like:

$ hue lights 1
Expand Down Expand Up @@ -268,7 +253,7 @@ The `state` keyword tells `hue` to read from stdin
Config
------

An optional config file can be created at `~/.hue.json` that looks like...
A config file will be created at `~/.hue.json` upon registration that looks like...

``` json
{
Expand Down
40 changes: 27 additions & 13 deletions hue-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ function usage() {
'options',
' -h, --help print this message and exit',
' -H, --host the hostname or ip of the bridge to control',
' -i, --init initialize the config file at ' + configfile,
' -j, --json force output to be in json',
' -s, --save save the config file at ' + configfile + ', same as --init',
' -u, --updates check for available updates',
' -v, --version print the version number and exit'
].join('\n');
Expand All @@ -84,9 +82,7 @@ function usage() {
var options = [
'h(help)',
'H:(host)',
'i(init)',
'j(json)',
's(save)',
'u(updates)',
'v(version)'
].join('');
Expand All @@ -98,12 +94,6 @@ while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'h': console.log(usage()); process.exit(0);
case 'H': config.host = option.optarg; break;
case 'i': case 's':
var s = JSON.stringify(config, null, 2);
fs.writeFileSync(configfile, s + '\n');
console.log('config file written to `%s`', configfile);
console.log(s);
process.exit(0);
case 'j': json = true; break;
case 'u': // check for updates
require('latest').checkupdate(package, function(ret, msg) {
Expand Down Expand Up @@ -243,14 +233,30 @@ switch (args[0]) {
});
break;
case 'register': // register this app
// Check for existing config
var existingconfig = statPath(configfile);
if (existingconfig && existingconfig.isFile()) {
console.log('A config file already exists at %s', configfile);
console.log('please remove it before attempting to register a new hub')
process.exit(1);
}
// Attempt to pair with hue hub
client = getclient();
console.log('please go and press the link button on your base station');
client.register(function(err) {
client.register(function(err, resp) {
if (err) {
console.error('failed to pair to Hue Base Station %s', config.host);
throw err;
}

console.log('Hue Base Station paired!')
console.log('username: ' + resp[0].success.username);
config.username = resp[0].success.username;

// writing config file
var s = JSON.stringify(config, null, 2);
Copy link
Owner

@bahamas10 bahamas10 Sep 21, 2016

Choose a reason for hiding this comment

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

I didn't realize hue-cli had as large of a community as it did (thanks everyone!) so I'm interested in gathering insight from everyone. My only concern with this is, when a user runs hue register it will clobber their existing config file without passing the --save option. Admittedly, the --save function currently operates in a less-than-ideal way.

My current proposal would be to drop the --save functionality completely, and make it so hue register will automatically write out the config IF ONE DOES NOT ALREADY EXIST. If one exists, hue register should print a warning, dump the current config, and exit non-zero, and leave it up to the user to remove it before registering a new bridge. What do you all think of that?

Either way though, I'm currently 👍 on this change and appreciate the small diff!

addendum: if you all like this idea, i wouldn't mind merging this as-is and making the code do what i described in a second commit

fs.writeFileSync(configfile, s + '\n');
console.log('config file written to `%s`', configfile);
});
break;
case 'search': // search for base stations
Expand Down Expand Up @@ -282,15 +288,16 @@ function getclient() {
'error: host not set',
'',
'search for hosts with `hue search`',
'then run with `-H <host>` or create a config file with `--init`',
'then run with `-H <host>`',
].join('\n'));
process.exit(1);
}

// create the client
var client = Hue.createClient({
stationIp: config.host,
appName: app
appName: app,
username: config.username
});
return client;
}
Expand Down Expand Up @@ -330,3 +337,10 @@ function hex2rgb(hex) {
return parseInt(h + '' + i, 16)
}
}

function statPath(path) {
try {
return fs.statSync(path);
} catch (ex) {}
return false;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"posix-getopt": "~1.0.0",
"hue.js": "https://github.com/bahamas10/hue.js/tarball/dave-1432672068",
"hue.js": "https://github.com/bahamas10/hue.js/tarball/master",
"latest": "~0.1.1",
"css-color-names": "0.0.0",
"extsprintf": "~1.0.2"
Expand Down