sonos-simple-cli
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "sonos-simple-cli", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Control Sonos from CLI with pause|play mute toggle and more, with Alfred support for keyboard shortcuts", | ||
@@ -27,5 +27,7 @@ "preferGlobal": "true", | ||
"flat-cache": "^1.0.3", | ||
"nconf": "^0.7.1", | ||
"path": "^0.11.14", | ||
"sonos": "^0.8.0" | ||
"sonos": "^0.8.0", | ||
"user-home": "^1.1.1" | ||
} | ||
} |
@@ -10,7 +10,10 @@ # Sonos Simple CLI | ||
- [ ] configuration file to setup which `roomName` you want to use | ||
- [x] acts only only the configured device | ||
- [x] acts only only the configured device via `roomName` config file `~/.sonos.json` | ||
- [x] caches a found/configured device (faster startup) | ||
- [x] supports pause/play toggle, and prev/next, vol +/- | ||
- [x] built in support for Alfred Workflow (and thus a global hotkey shortcut) | ||
- [x] configuration file `~/.sonos.json` to setup which `roomName` you want to use | ||
- [ ] fix Alfred path so we don't need the `~/bin/` symlink | ||
- [ ] better configuration file setup | ||
- [ ] support for "get first" vs. configured `roomName` | ||
@@ -29,12 +32,26 @@ ## Thanks | ||
### Manual Crappy Install | ||
If you want to use Alfred Workflow - it has path problems which I'm currently | ||
solving with an explicit symlink *(something more elegant coming)* | ||
cd ~ | ||
mkdir bin | ||
git clone https://github.com/zeroasterisk/sonos-simple-cli.git | ||
cd sonos-simple-cli | ||
npm install | ||
cd .. | ||
ln -s sonos-simple-cli/sonos.js ./ | ||
``` | ||
cd ~ | ||
mkdir bin | ||
cd bin | ||
ln -s $(which sonos-simple-cli) sonos-simple-cli | ||
``` | ||
### Development / Manual Install | ||
if for some reason, you want to develop on this and play with it, | ||
here's a guide which shuld get you setup and functional pretty easily. | ||
``` | ||
cd ~ | ||
mkdir bin | ||
git clone https://github.com/zeroasterisk/sonos-simple-cli.git sonos-simple-cli-repo | ||
ln -s sonos-simple-cli-repo/sonos.js sonos-simple-cli | ||
cd sonos-simple-cli-repo | ||
npm install | ||
``` | ||
## Manual Script Running | ||
@@ -47,16 +64,16 @@ | ||
----------------------------- | ||
node sonos.js playpause | ||
node sonos.js play | ||
node sonos.js pause | ||
node sonos.js next | ||
node sonos.js prev | ||
node sonos.js volup | ||
node sonos.js voldown | ||
node sonos.js mute | ||
node sonos.js unmute | ||
node sonos.js mutetoggle | ||
node sonos.js clearCache | ||
sonos-simple-cli playpause | ||
sonos-simple-cli play | ||
sonos-simple-cli pause | ||
sonos-simple-cli next | ||
sonos-simple-cli prev | ||
sonos-simple-cli volup | ||
sonos-simple-cli voldown | ||
sonos-simple-cli mute | ||
sonos-simple-cli unmute | ||
sonos-simple-cli mutetoggle | ||
sonos-simple-cli clearCache | ||
----------------------------- | ||
$ node ~/bin/sonos.js playpause | ||
$ ~/bin/sonos-simple-cli playpause | ||
> SONOS set to PAUSED | ||
@@ -63,0 +80,0 @@ Beastie Boys "I Don't Know" |
115
sonos.js
#!/usr/bin/env node | ||
var sonos = require('sonos'); | ||
var userHome = require('user-home'); | ||
var path = require('path'); | ||
var nconf = require('nconf'); | ||
var flatCache = require('flat-cache') | ||
var sonos = require('sonos'); | ||
@@ -14,7 +16,7 @@ // ----------------------------------------- | ||
this.cache = null; | ||
this.config = { | ||
roomName : 'Office/Upstairs', | ||
action: 'pausePlay' | ||
}; | ||
this.setup(args); | ||
this.conf(args); | ||
// look for cached "found" devices -- a heck of a lot faster | ||
this.cache = flatCache.load('sonosSimpleCliCache', path.resolve(nconf.get('tmp'))); | ||
this.setupDevice(this.run.bind(this)); | ||
@@ -44,9 +46,50 @@ }; | ||
// setup and configure | ||
// uses nconf for handling conditions | ||
SonosSimpleCli.prototype.conf = function(args) { | ||
nconf.env().argv(); | ||
nconf.use('file', { | ||
type: 'file', | ||
file: '.sonos.json', | ||
dir: userHome, | ||
search: true | ||
}).load(); | ||
nconf.defaults({ | ||
// What is the name of your SonosController? | ||
roomName : null, | ||
// Where we store the cache files (directory) | ||
tmp : '/tmp', | ||
// Placeholder where we will store the action | ||
action: this.getActionFromArgs(args) | ||
}); | ||
// validate that we have a roomName | ||
if (nconf.get('roomName') === null) { | ||
console.log(nconf); | ||
console.log('You need to configure your Sonos Controller room name'); | ||
console.log('echo \'{\"roomName\": \"My Controller Room Here\"}\' > ~/.sonos.json'); | ||
process.exit(1); | ||
} | ||
// validate that we have an action | ||
if (nconf.get('action') === null) { | ||
console.log('You need to pass in an action'); | ||
this.help(); | ||
} | ||
// if we are clearCache | ||
if (nconf.get('action') == 'clearCache') { | ||
this.cache.removeKey('device'); | ||
this.cache.save(); | ||
console.log(' cache cleared'); | ||
process.exit(0); | ||
} | ||
} | ||
// determine runAction | ||
// parses CLI arguments for supported actions | ||
SonosSimpleCli.prototype.setup = function(args) { | ||
// get the action | ||
this.config.action = null; | ||
for (i = 0; i < process.argv.length; i++) { | ||
switch(process.argv[i]) { | ||
SonosSimpleCli.prototype.getActionFromArgs = function(args) { | ||
for (i = 0; i < args.length; i++) { | ||
switch(args[i]) { | ||
case 'help': | ||
@@ -56,58 +99,39 @@ this.help(); | ||
case 'playpause': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
case 'play': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
case 'pause': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
case 'next': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
case 'prev': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
case 'volup': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
case 'voldown': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
case 'mute': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
case 'unmute': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
case 'mutetoggle': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
case 'clearCache': | ||
this.config.action = process.argv[i]; | ||
return args[i]; | ||
break; | ||
} | ||
} | ||
// get the controller roomName | ||
// TODO | ||
// no action? fail | ||
if (this.config.action == null) { | ||
console.log('BAD ACTION'); | ||
this.help(); | ||
} | ||
return null; | ||
}; | ||
// look for cached "found" devices -- a heck of a lot faster | ||
this.cache = flatCache.load('sonosSimpleCliCache'); | ||
// if we are clearCache | ||
if (this.config.action == 'clearCache') { | ||
this.cache.removeKey('device'); | ||
this.cache.save(); | ||
console.log(' cache cleared'); | ||
process.exit(0); | ||
} | ||
} | ||
// setupDevice | ||
@@ -141,3 +165,3 @@ SonosSimpleCli.prototype.setupDevice = function(callback) { | ||
return this.sc.runAction(this.config.action); | ||
return this.sc.runAction(nconf.get('action')); | ||
}; | ||
@@ -161,3 +185,3 @@ | ||
} | ||
if (info.roomName != this.config.roomName) { | ||
if (info.roomName != nconf.get('roomName')) { | ||
return; | ||
@@ -184,3 +208,2 @@ } | ||
this.sc = new SonosController(device); | ||
this.sc.config = this.config; | ||
}; | ||
@@ -187,0 +210,0 @@ |
Sorry, the diff of this file is not supported yet
55892
377
135
5
+ Addednconf@^0.7.1
+ Addeduser-home@^1.1.1
+ Addedalign-text@0.1.4(transitive)
+ Addedcamelcase@1.2.1(transitive)
+ Addedcenter-align@0.1.3(transitive)
+ Addedcliui@2.1.0(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addedini@1.3.8(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedkind-of@3.2.2(transitive)
+ Addedlazy-cache@1.0.4(transitive)
+ Addedlongest@1.0.1(transitive)
+ Addednconf@0.7.2(transitive)
+ Addedrepeat-string@1.6.1(transitive)
+ Addedright-align@0.1.3(transitive)
+ Addeduser-home@1.1.1(transitive)
+ Addedwindow-size@0.1.4(transitive)
+ Addedwordwrap@0.0.2(transitive)
+ Addedyargs@3.15.0(transitive)