Merge pull request #25 from hybridgroup/api-configuration

Allow configuration of API options
This commit is contained in:
Ron Evans 2013-11-06 16:42:02 -08:00
commit 9d7cb3e5c3
3 changed files with 58 additions and 5 deletions

18
dist/cylon.js vendored
View File

@ -43,12 +43,17 @@
};
Master = (function() {
var api, robots;
var api, api_config, robots;
robots = [];
api = null;
api_config = {
host: '127.0.0.1',
port: '3000'
};
function Master() {
this.robot = __bind(this.robot, this);
var rl;
@ -80,6 +85,12 @@
return robots;
};
Master.prototype.api = function(opts) {
api_config.host = opts.host || "127.0.0.1";
api_config.port = opts.port || "3000";
return api_config;
};
Master.prototype.findRobot = function(name, callback) {
var bot, error, robot, _i, _len;
robot = null;
@ -167,9 +178,8 @@
};
Master.prototype.startAPI = function() {
return api != null ? api : api = new Api.Server({
master: this.self
});
api_config.master = this.self;
return api != null ? api : api = new Api.Server(api_config);
};
return Master;

29
examples/api.coffee Normal file
View File

@ -0,0 +1,29 @@
Cylon = require '..'
Cylon.api host: '0.0.0.0', port: '8080'
bots = [
{ port: '/dev/rfcomm0', name: 'Thelma' },
{ port: '/dev/rfcomm1', name: 'Louise' }
]
SpheroRobot =
connection:
name: 'Sphero', adaptor: 'sphero'
device:
name: 'sphero', driver: 'sphero'
work: (me) ->
every 1.seconds(), ->
Logger.info me.name
me.sphero.setRGB Math.floor(Math.random() * 100000)
me.sphero.roll 60, Math.floor(Math.random() * 360)
for bot in bots
robot = Object.create(SpheroRobot)
robot.connection.port = bot.port
robot.name = bot.name
Cylon.robot robot
Cylon.start()

View File

@ -35,6 +35,7 @@ class Cylon
class Master
robots = []
api = null
api_config = { host: '127.0.0.1', port: '3000' }
# Public: Creates a new Master
#
@ -76,6 +77,18 @@ class Cylon
# Returns an array of all Robot instances
robots: -> robots
# Public: Configures the API host and port based on passed options
#
# opts - object containing API options
# host - host address API should serve from
# port - port API should listen for requests on
#
# Returns the API configuration
api: (opts) ->
api_config.host = opts.host || "127.0.0.1"
api_config.port = opts.port || "3000"
api_config
# Public: Finds a particular robot by name
#
# name - name of the robot to find
@ -146,6 +159,7 @@ class Cylon
#
# Returns an Api.Server instance
startAPI: ->
api ?= new Api.Server(master: @self)
api_config.master = @self
api ?= new Api.Server(api_config)
module.exports = Cylon.getInstance()