Skip to content

Commit

Permalink
Merge pull request #26 from mintuz/stop_commands_not_server
Browse files Browse the repository at this point in the history
Stop commands not server
  • Loading branch information
mintuz committed Aug 28, 2016
2 parents ce1c5be + c745d80 commit f8c37e7
Show file tree
Hide file tree
Showing 18 changed files with 257 additions and 78 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -35,3 +35,5 @@ node_modules

#Intellij settings
.idea

.DS_Store
108 changes: 108 additions & 0 deletions .jshintrc
@@ -0,0 +1,108 @@


{
// JSHint Default Configuration File (as on JSHint website)
// See http://jshint.com/docs/ for more details

"maxerr" : 25, // {int} Maximum error before stopping

// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
"eqeqeq" : true, // true: Require triple equals (===) for comparison
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
"immed" : true, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
"indent" : 4, // {int} Number of spaces to use for indentation
"latedef" : true, // true: Require variables/functions to be defined before being used
"newcap" : true, // true: Require capitalization of all constructor functions e.g. `new F()`
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
"noempty" : true, // true: Prohibit use of empty blocks
"nonew" : true, // true: Prohibit use of constructors for side-effects (without assignment)
"plusplus" : true, // true: Prohibit use of `++` & `--`
"quotmark" : false, // Quotation mark consistency:
// false : do nothing (default)
// true : ensure whatever is used is consistent
// "single" : require single quotes
// "double" : require double quotes
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
"unused" : true, // true: Require all defined variables be used
"strict" : false, // true: Requires all functions run in ES5 Strict Mode
"maxparams" : false, // {int} Max number of formal params allowed per function
"maxdepth" : 5, // {int} Max depth of nested blocks (within functions)
"maxstatements" : false, // {int} Max number statements per function
"maxcomplexity" : false,
"maxlen" : 250, // {int} Max number of characters per line

// Relaxing
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
"boss" : false, // true: Tolerate assignments where comparisons would be expected
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
"eqnull" : false, // true: Tolerate use of `== null`
"es5" : false, // true: Allow ES5 syntax (ex: getters and setters)
"esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`)
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (ex: `for each`, multiple try/catch, function expression…)
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
"funcscope" : false, // true: Tolerate defining variables inside control statements"
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
"iterator" : false, // true: Tolerate using the `__iterator__` property
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
"laxbreak" : true, // true: Tolerate possibly unsafe line breakings
"laxcomma" : false, // true: Tolerate comma-first style coding
"loopfunc" : false, // true: Tolerate functions being defined in loops
"multistr" : false, // true: Tolerate multi-line strings
"proto" : false, // true: Tolerate using the `__proto__` property
"scripturl" : false, // true: Tolerate script-targeted URLs
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
"validthis" : false, // true: Tolerate using this in a non-constructor function

// Environments
"browser" : true, // Web Browser (window, document, etc)
"couch" : false, // CouchDB
"devel" : true, // Development/debugging (alert, confirm, etc)
"dojo" : false, // Dojo Toolkit
"jquery" : true, // jQuery
"mootools" : false, // MooTools
"node" : false, // Node.js
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
"prototypejs" : false, // Prototype and Scriptaculous
"rhino" : false, // Rhino
"worker" : false, // Web Workers
"wsh" : false, // Windows Scripting Host
"yui" : false, // Yahoo User Interface

// Custom Globals
// additional predefined global variables
"globals" : {
"module": true,
"require": true,
"exports": true,
"define": true,
// for when we lint tests
"describe": true,
"xdescribe": true,
"xit": true,
"it": true,
"expect": true,
"beforeEach": true,
"waitsFor": true,
"runs": true,
"afterEach": true,
"jasmine": true,
"__base": true,
"__dirname": true,
"requirejs": true,
"global": true,
"process": true,
"morph": true,
"React": true,
"spyOn": true,
"Promise": true,
"twttr": true,
"instgrm": true
}
}
46 changes: 46 additions & 0 deletions README.md
Expand Up @@ -10,6 +10,8 @@ Not yet on npm so you'll have to do it the good'ol fasioned way with a cheeky gi
* `bb8 setup`
* Use commands below

If you are struggling to connect to BB8 it might be worth checking out the troubleshoot section of @saraford's [blog post](https://medium.com/@saraford/how-to-have-hubot-in-slack-send-commands-to-bb-8-700d2f3c953d#.4zjscutl83).

# Commands

### Utility Commands
Expand Down Expand Up @@ -107,6 +109,50 @@ Obviously you wouldn’t pass your OAuth information like this (BB8 Commander su

A suite difference between native commands and custom commands is that native commands that require multiple parameters will be passed as an array whilst custom commands will be objects. The reason for this is custom commands are key value pairs due to them sharing the same code as the CLI tool.

### Stopping a custom command with the express server.
Some custom commands such as the desk-buddy command or the weather command loop forever until you tell BB8 to stop via the express server.

To stop a previous BB8 Command send the following POST Request. This will keep the express server running but will stop BB8 doing whatever he's doing.

Post Request - localhost:3000/

Request Body

```
{
"mode":"custom",
"command":"stop"
}
```

### Using BB8 Commander in your own projects.
It's cool being able to run a tool from your terminal but it's even cooler to be able to extend and build your own applications.

Run `npm install bb8-commander --save` within your projects root directory and here is some example code.

Some commands such as `disco` return a setInterval ID. This allows you to stop a command from continuously running by running `clearInterval(id)`

```
var bb8 = require('bb8-commander');
// Used to create a .bb8config file within your users home directory.
bb8.setup();
var name = 'express';
var options = {
port: 4000
};
// Used to execute the command express
bb8.executeCommand(name, options);
// Used to execute the command disco
var id = bb8.executeCommand('disco');
// Used to cancel the disco command.
clearInterval(id);
```

# Examples
* [How to have Hubot talk to BB-8 using Express Server](https://medium.com/@saraford/how-to-have-hubot-in-slack-send-commands-to-bb-8-700d2f3c953d) by [@saraford](https://github.com/saraford)

Expand Down
22 changes: 11 additions & 11 deletions bin/index.js
Expand Up @@ -17,7 +17,7 @@ program
.command('disconnect')
.description('Command to disconnect from your BB8 Unit')
.action(function () {
executeCommand('disconnect');
executeCommand.connectAndSendCommand('disconnect');
});

// Real Actions
Expand All @@ -26,14 +26,14 @@ program
.command("dance")
.description("dance dance BB-8")
.action(function () {
executeCommand("dance");
executeCommand.connectAndSendCommand("dance");
});

program
.command('disco')
.description('Command to make your BB8 Unit A disco ball')
.action(function () {
executeCommand('disco');
executeCommand.connectAndSendCommand('disco');
});

program
Expand All @@ -43,7 +43,7 @@ program
.option('-cc, --country <country>', 'Country name such as uk')
.option('-t, --access-token <accessToken>', 'API Key')
.action(function(options) {
executeCommand('weather', options);
executeCommand.connectAndSendCommand('weather', options);
});

program
Expand All @@ -56,7 +56,7 @@ program
.command('roll')
.description('BB8 will roll!')
.action(function () {
executeCommand('roll');
executeCommand.connectAndSendCommand('roll');
});


Expand All @@ -70,43 +70,43 @@ program
.option('--access-token-key <accessTokenKey>', 'Twitter api access token key')
.option('--access-token-secret <accessTokenSecret>', 'Twitter api access token secret')
.action(function (options) {
executeCommand('tweet', options);
executeCommand.connectAndSendCommand('tweet', options);
});

program
.command('express')
.description('Command to setup express server')
.option('-p, --port <port>', 'Port to run express on. Defaults to 3000')
.action(function (options) {
executeCommand('express', options);
executeCommand.connectAndSendCommand('express', options);
});

program
.command('desk-buddy')
.description('Command to keep you company whilst working at your desk. Place your BB8 in the charging station.')
.action(function (options) {
executeCommand('desk-buddy');
executeCommand.connectAndSendCommand('desk-buddy');
});

program
.command('power')
.description('Command to get the power state of the BB-8')
.action(function (options) {
executeCommand('power');
executeCommand.connectAndSendCommand('power');
});

program
.command('gestures')
.description('Some gestures for your BB-8')
.action(function (options) {
executeCommand('gestures');
executeCommand.connectAndSendCommand('gestures');
});

program
.command('drive')
.description('Command to accept keyboard input--use arrow keys')
.action(function (options) {
executeCommand('drive');
executeCommand.connectAndSendCommand('drive');
});

try {
Expand Down
2 changes: 2 additions & 0 deletions commands/dance.js
@@ -1,4 +1,5 @@
module.exports = function (bb8) {

console.log("BB-8 got moves!!");

var movesTimer = setInterval(function() {
Expand All @@ -17,4 +18,5 @@ module.exports = function (bb8) {
clearInterval(colorTimer);
}, 5000);

return false;
};
2 changes: 1 addition & 1 deletion commands/desk-buddy.js
Expand Up @@ -13,7 +13,7 @@ module.exports = function (bb8) {

var partiallyAppliedMoveHead = _.partial(moveHead, bb8);

setInterval(partiallyAppliedMoveHead, 4000);
return setInterval(partiallyAppliedMoveHead, 4000);
};


Expand Down
4 changes: 3 additions & 1 deletion commands/disco.js
@@ -1,8 +1,10 @@
module.exports = function (bb8) {

console.log('Let\'s Party!!');

bb8.randomColor();
setInterval(function () {

return setInterval(function () {
bb8.randomColor();
}, 1000);
};
16 changes: 10 additions & 6 deletions commands/drive.js
Expand Up @@ -16,7 +16,7 @@ module.exports = function (bb8) {
console.log("starting to listen for arrow key presses");

stdin.on("keypress", function(ch,key) {
//console.log('got "keypress"', key);

bb8.color('#000000');

if(key && key.name === 'e') {
Expand All @@ -29,33 +29,35 @@ module.exports = function (bb8) {
}, 300);
});
}

if(key && key.name === 'q') {
bb8.finishCalibration();
}

if(key && key.name === 'w'){
//console.log('up');
bb8.stop();
bb8.roll(150, 0);
}

if(key && key.name === 'd'){
//console.log('right');
bb8.stop();
bb8.roll(150, 90);
}

if(key && key.name === 's'){
//console.log('down');
bb8.stop();
bb8.roll(150, 180);
}

if(key && key.name === 'a'){
//console.log('left');
bb8.stop();
bb8.roll(150, 270);
}

if(key && key.name === 'space'){
//console.log('space');
bb8.stop();
}

if (key && key.ctrl && key.name === 'c') {
process.stdin.pause();
process.exit();
Expand All @@ -64,4 +66,6 @@ module.exports = function (bb8) {

stdin.setRawMode(true);
stdin.resume();

return false;
};

0 comments on commit f8c37e7

Please sign in to comment.