-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from mintuz/express
ExpressJS action
- Loading branch information
Showing
7 changed files
with
230 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
var bb8 = require('../libs/bb8-instance')(), | ||
config = require('../libs/bb8-instance').config, | ||
expressInstance = require('../libs/express'), | ||
executeCustomCommand = require('../libs/execute-command'), | ||
_ = require('lodash'); | ||
|
||
var callbackFactory = function(res){ | ||
return function(err, data){ | ||
if(!err) { | ||
res.send({data: data}); | ||
} else { | ||
res.send({error: err}); | ||
} | ||
}; | ||
}; | ||
|
||
var spheroCommandExecuter = function(bb8, requestBody, res) { | ||
|
||
if(_.isString(requestBody.value)) { | ||
|
||
bb8[requestBody.action](requestBody.value, callbackFactory(res)); | ||
|
||
} else if(_.isArray(requestBody.value)) { | ||
|
||
requestBody.value.push(callbackFactory(res)); | ||
|
||
bb8[requestBody.action].apply(this, requestBody.value); | ||
|
||
} else { | ||
|
||
bb8[requestBody.action](callbackFactory(res)); | ||
|
||
} | ||
|
||
}; | ||
|
||
var customCommandExecuter = function(bb8, requestBody, res){ | ||
|
||
if(_.isString(requestBody.value) || _.isObject(requestBody.value)) { | ||
|
||
executeCustomCommand.alreadyConnectedSingleValue(bb8, requestBody.action, requestBody.value); | ||
|
||
} else if(_.isArray(requestBody.value)) { | ||
|
||
executeCustomCommand.alreadyConnectedMultipleValues(bb8, requestBody.action, requestBody.value); | ||
|
||
} else { | ||
|
||
executeCustomCommand.alreadyConnectedSingleValue(bb8, requestBody.action, {}); | ||
|
||
} | ||
|
||
res.send('Command Executed - ' + requestBody.action); | ||
|
||
}; | ||
|
||
module.exports = function(bb8, options) { | ||
|
||
expressInstance(function (req, res) { | ||
|
||
var requestBody = req.body; | ||
|
||
if(requestBody.action && requestBody.mode === 'sphero') { | ||
|
||
spheroCommandExecuter(bb8, req.body, res); | ||
|
||
} else if(requestBody.action && requestBody.mode === 'custom') { | ||
|
||
customCommandExecuter(bb8, req.body, res); | ||
|
||
} else { | ||
|
||
res.send('Command is invalid'); | ||
|
||
} | ||
|
||
}, options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var bb8 = require('./bb8-instance')(), | ||
appRootPath = require('app-root-path'), | ||
_ = require('lodash'); | ||
|
||
module.exports = function (command, options) { | ||
if (bb8) { | ||
bb8.connect(function () { | ||
require(appRootPath + '/commands/' + command)(bb8, options); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports.alreadyConnected = function(bb8, command) { | ||
if (bb8) { | ||
require(appRootPath + '/commands/' + command)(bb8); | ||
} | ||
} | ||
|
||
module.exports.alreadyConnectedSingleValue = function(bb8, command, options) { | ||
if (bb8) { | ||
require(appRootPath + '/commands/' + command)(bb8, options); | ||
} | ||
}; | ||
|
||
module.exports.alreadyConnectedMultipleValues = function(bb8, command, options) { | ||
|
||
if (bb8) { | ||
var parameters = _.union([bb8], options); | ||
require(appRootPath + '/commands/' + command).apply(this, parameters); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var express = require('express'), | ||
app = express(), | ||
bodyParser = require('body-parser'); | ||
|
||
module.exports = function(callback, options) { | ||
|
||
var port = options.port || 3000; | ||
|
||
app.use(bodyParser.json()); | ||
|
||
app.post('/', callback); | ||
|
||
app.listen(port, function () { | ||
console.log( 'Server listening on port ' + port ); | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters