Skip to content

Commit f8c37e7

Browse files
authored
Merge pull request #26 from mintuz/stop_commands_not_server
Stop commands not server
2 parents ce1c5be + c745d80 commit f8c37e7

18 files changed

+257
-78
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ node_modules
3535

3636
#Intellij settings
3737
.idea
38+
39+
.DS_Store

.jshintrc

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
3+
{
4+
// JSHint Default Configuration File (as on JSHint website)
5+
// See http://jshint.com/docs/ for more details
6+
7+
"maxerr" : 25, // {int} Maximum error before stopping
8+
9+
// Enforcing
10+
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
11+
"camelcase" : false, // true: Identifiers must be in camelCase
12+
"curly" : true, // true: Require {} for every new block or scope
13+
"eqeqeq" : true, // true: Require triple equals (===) for comparison
14+
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
15+
"immed" : true, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
16+
"indent" : 4, // {int} Number of spaces to use for indentation
17+
"latedef" : true, // true: Require variables/functions to be defined before being used
18+
"newcap" : true, // true: Require capitalization of all constructor functions e.g. `new F()`
19+
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
20+
"noempty" : true, // true: Prohibit use of empty blocks
21+
"nonew" : true, // true: Prohibit use of constructors for side-effects (without assignment)
22+
"plusplus" : true, // true: Prohibit use of `++` & `--`
23+
"quotmark" : false, // Quotation mark consistency:
24+
// false : do nothing (default)
25+
// true : ensure whatever is used is consistent
26+
// "single" : require single quotes
27+
// "double" : require double quotes
28+
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
29+
"unused" : true, // true: Require all defined variables be used
30+
"strict" : false, // true: Requires all functions run in ES5 Strict Mode
31+
"maxparams" : false, // {int} Max number of formal params allowed per function
32+
"maxdepth" : 5, // {int} Max depth of nested blocks (within functions)
33+
"maxstatements" : false, // {int} Max number statements per function
34+
"maxcomplexity" : false,
35+
"maxlen" : 250, // {int} Max number of characters per line
36+
37+
// Relaxing
38+
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
39+
"boss" : false, // true: Tolerate assignments where comparisons would be expected
40+
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
41+
"eqnull" : false, // true: Tolerate use of `== null`
42+
"es5" : false, // true: Allow ES5 syntax (ex: getters and setters)
43+
"esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`)
44+
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
45+
// (ex: `for each`, multiple try/catch, function expression…)
46+
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
47+
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
48+
"funcscope" : false, // true: Tolerate defining variables inside control statements"
49+
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
50+
"iterator" : false, // true: Tolerate using the `__iterator__` property
51+
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
52+
"laxbreak" : true, // true: Tolerate possibly unsafe line breakings
53+
"laxcomma" : false, // true: Tolerate comma-first style coding
54+
"loopfunc" : false, // true: Tolerate functions being defined in loops
55+
"multistr" : false, // true: Tolerate multi-line strings
56+
"proto" : false, // true: Tolerate using the `__proto__` property
57+
"scripturl" : false, // true: Tolerate script-targeted URLs
58+
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
59+
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
60+
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
61+
"validthis" : false, // true: Tolerate using this in a non-constructor function
62+
63+
// Environments
64+
"browser" : true, // Web Browser (window, document, etc)
65+
"couch" : false, // CouchDB
66+
"devel" : true, // Development/debugging (alert, confirm, etc)
67+
"dojo" : false, // Dojo Toolkit
68+
"jquery" : true, // jQuery
69+
"mootools" : false, // MooTools
70+
"node" : false, // Node.js
71+
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
72+
"prototypejs" : false, // Prototype and Scriptaculous
73+
"rhino" : false, // Rhino
74+
"worker" : false, // Web Workers
75+
"wsh" : false, // Windows Scripting Host
76+
"yui" : false, // Yahoo User Interface
77+
78+
// Custom Globals
79+
// additional predefined global variables
80+
"globals" : {
81+
"module": true,
82+
"require": true,
83+
"exports": true,
84+
"define": true,
85+
// for when we lint tests
86+
"describe": true,
87+
"xdescribe": true,
88+
"xit": true,
89+
"it": true,
90+
"expect": true,
91+
"beforeEach": true,
92+
"waitsFor": true,
93+
"runs": true,
94+
"afterEach": true,
95+
"jasmine": true,
96+
"__base": true,
97+
"__dirname": true,
98+
"requirejs": true,
99+
"global": true,
100+
"process": true,
101+
"morph": true,
102+
"React": true,
103+
"spyOn": true,
104+
"Promise": true,
105+
"twttr": true,
106+
"instgrm": true
107+
}
108+
}

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Not yet on npm so you'll have to do it the good'ol fasioned way with a cheeky gi
1010
* `bb8 setup`
1111
* Use commands below
1212

13+
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).
14+
1315
# Commands
1416

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

108110
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.
109111

112+
### Stopping a custom command with the express server.
113+
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.
114+
115+
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.
116+
117+
Post Request - localhost:3000/
118+
119+
Request Body
120+
121+
```
122+
{
123+
"mode":"custom",
124+
"command":"stop"
125+
}
126+
```
127+
128+
### Using BB8 Commander in your own projects.
129+
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.
130+
131+
Run `npm install bb8-commander --save` within your projects root directory and here is some example code.
132+
133+
Some commands such as `disco` return a setInterval ID. This allows you to stop a command from continuously running by running `clearInterval(id)`
134+
135+
```
136+
var bb8 = require('bb8-commander');
137+
138+
// Used to create a .bb8config file within your users home directory.
139+
bb8.setup();
140+
141+
var name = 'express';
142+
var options = {
143+
port: 4000
144+
};
145+
146+
// Used to execute the command express
147+
bb8.executeCommand(name, options);
148+
149+
// Used to execute the command disco
150+
var id = bb8.executeCommand('disco');
151+
152+
// Used to cancel the disco command.
153+
clearInterval(id);
154+
```
155+
110156
# Examples
111157
* [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)
112158

bin/index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ program
1717
.command('disconnect')
1818
.description('Command to disconnect from your BB8 Unit')
1919
.action(function () {
20-
executeCommand('disconnect');
20+
executeCommand.connectAndSendCommand('disconnect');
2121
});
2222

2323
// Real Actions
@@ -26,14 +26,14 @@ program
2626
.command("dance")
2727
.description("dance dance BB-8")
2828
.action(function () {
29-
executeCommand("dance");
29+
executeCommand.connectAndSendCommand("dance");
3030
});
3131

3232
program
3333
.command('disco')
3434
.description('Command to make your BB8 Unit A disco ball')
3535
.action(function () {
36-
executeCommand('disco');
36+
executeCommand.connectAndSendCommand('disco');
3737
});
3838

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

4949
program
@@ -56,7 +56,7 @@ program
5656
.command('roll')
5757
.description('BB8 will roll!')
5858
.action(function () {
59-
executeCommand('roll');
59+
executeCommand.connectAndSendCommand('roll');
6060
});
6161

6262

@@ -70,43 +70,43 @@ program
7070
.option('--access-token-key <accessTokenKey>', 'Twitter api access token key')
7171
.option('--access-token-secret <accessTokenSecret>', 'Twitter api access token secret')
7272
.action(function (options) {
73-
executeCommand('tweet', options);
73+
executeCommand.connectAndSendCommand('tweet', options);
7474
});
7575

7676
program
7777
.command('express')
7878
.description('Command to setup express server')
7979
.option('-p, --port <port>', 'Port to run express on. Defaults to 3000')
8080
.action(function (options) {
81-
executeCommand('express', options);
81+
executeCommand.connectAndSendCommand('express', options);
8282
});
8383

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

9191
program
9292
.command('power')
9393
.description('Command to get the power state of the BB-8')
9494
.action(function (options) {
95-
executeCommand('power');
95+
executeCommand.connectAndSendCommand('power');
9696
});
9797

9898
program
9999
.command('gestures')
100100
.description('Some gestures for your BB-8')
101101
.action(function (options) {
102-
executeCommand('gestures');
102+
executeCommand.connectAndSendCommand('gestures');
103103
});
104104

105105
program
106106
.command('drive')
107107
.description('Command to accept keyboard input--use arrow keys')
108108
.action(function (options) {
109-
executeCommand('drive');
109+
executeCommand.connectAndSendCommand('drive');
110110
});
111111

112112
try {

commands/dance.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = function (bb8) {
2+
23
console.log("BB-8 got moves!!");
34

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

21+
return false;
2022
};

commands/desk-buddy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function (bb8) {
1313

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

16-
setInterval(partiallyAppliedMoveHead, 4000);
16+
return setInterval(partiallyAppliedMoveHead, 4000);
1717
};
1818

1919

commands/disco.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
module.exports = function (bb8) {
2+
23
console.log('Let\'s Party!!');
34

45
bb8.randomColor();
5-
setInterval(function () {
6+
7+
return setInterval(function () {
68
bb8.randomColor();
79
}, 1000);
810
};

commands/drive.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = function (bb8) {
1616
console.log("starting to listen for arrow key presses");
1717

1818
stdin.on("keypress", function(ch,key) {
19-
//console.log('got "keypress"', key);
19+
2020
bb8.color('#000000');
2121

2222
if(key && key.name === 'e') {
@@ -29,33 +29,35 @@ module.exports = function (bb8) {
2929
}, 300);
3030
});
3131
}
32+
3233
if(key && key.name === 'q') {
3334
bb8.finishCalibration();
3435
}
36+
3537
if(key && key.name === 'w'){
36-
//console.log('up');
3738
bb8.stop();
3839
bb8.roll(150, 0);
3940
}
41+
4042
if(key && key.name === 'd'){
41-
//console.log('right');
4243
bb8.stop();
4344
bb8.roll(150, 90);
4445
}
46+
4547
if(key && key.name === 's'){
46-
//console.log('down');
4748
bb8.stop();
4849
bb8.roll(150, 180);
4950
}
51+
5052
if(key && key.name === 'a'){
51-
//console.log('left');
5253
bb8.stop();
5354
bb8.roll(150, 270);
5455
}
56+
5557
if(key && key.name === 'space'){
56-
//console.log('space');
5758
bb8.stop();
5859
}
60+
5961
if (key && key.ctrl && key.name === 'c') {
6062
process.stdin.pause();
6163
process.exit();
@@ -64,4 +66,6 @@ module.exports = function (bb8) {
6466

6567
stdin.setRawMode(true);
6668
stdin.resume();
69+
70+
return false;
6771
};

0 commit comments

Comments
 (0)