Namespace Robot class

This commit is contained in:
Andrew Stewart 2013-11-04 11:57:27 -08:00
parent 483dcf26b9
commit 9d48cba3cf
4 changed files with 359 additions and 349 deletions

344
dist/robot.js vendored
View File

@ -9,7 +9,7 @@
(function() {
'use strict';
var Async, Connection, Device, Robot,
var Async, Connection, Device, namespace,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
@ -17,206 +17,212 @@
require('./basestar');
namespace = require('node-namespace');
Connection = require("./connection");
Device = require("./device");
Async = require("async");
module.exports = Robot = (function() {
var klass;
namespace('Cylon', function() {
return this.Robot = (function() {
var klass;
klass = Robot;
klass = Robot;
function Robot(opts) {
var func, n, reserved;
if (opts == null) {
opts = {};
}
this.registerDriver = __bind(this.registerDriver, this);
this.startDevices = __bind(this.startDevices, this);
this.startConnections = __bind(this.startConnections, this);
this.start = __bind(this.start, this);
this.initDevices = __bind(this.initDevices, this);
this.initConnections = __bind(this.initConnections, this);
this.robot = this;
this.name = opts.name || this.constructor.randomName();
this.master = opts.master;
this.connections = {};
this.devices = {};
this.adaptors = {};
this.drivers = {};
this.registerAdaptor("./loopback", "loopback");
this.registerDriver("./ping", "ping");
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
this.work = opts.work || function() {
return Logger.info("No work yet");
};
for (n in opts) {
func = opts[n];
reserved = ['connection', 'connections', 'device', 'devices', 'work'];
if (__indexOf.call(reserved, n) < 0) {
this.robot[n] = func;
function Robot(opts) {
var func, n, reserved;
if (opts == null) {
opts = {};
}
this.registerDriver = __bind(this.registerDriver, this);
this.startDevices = __bind(this.startDevices, this);
this.startConnections = __bind(this.startConnections, this);
this.start = __bind(this.start, this);
this.initDevices = __bind(this.initDevices, this);
this.initConnections = __bind(this.initConnections, this);
this.robot = this;
this.name = opts.name || this.constructor.randomName();
this.master = opts.master;
this.connections = {};
this.devices = {};
this.adaptors = {};
this.drivers = {};
this.registerAdaptor("./loopback", "loopback");
this.registerDriver("./ping", "ping");
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
this.work = opts.work || function() {
return Logger.info("No work yet");
};
for (n in opts) {
func = opts[n];
reserved = ['connection', 'connections', 'device', 'devices', 'work'];
if (__indexOf.call(reserved, n) < 0) {
this.robot[n] = func;
}
}
}
}
Robot.randomName = function() {
return "Robot " + (Math.floor(Math.random() * 100000));
};
Robot.prototype.data = function() {
var connection, device, n;
return {
name: this.name,
connections: (function() {
var _ref, _results;
_ref = this.connections;
_results = [];
for (n in _ref) {
connection = _ref[n];
_results.push(connection.data());
}
return _results;
}).call(this),
devices: (function() {
var _ref, _results;
_ref = this.devices;
_results = [];
for (n in _ref) {
device = _ref[n];
_results.push(device.data());
}
return _results;
}).call(this)
Robot.randomName = function() {
return "Robot " + (Math.floor(Math.random() * 100000));
};
};
Robot.prototype.initConnections = function(connections) {
var connection, _i, _len;
Logger.info("Initializing connections...");
if (connections == null) {
return;
}
connections = [].concat(connections);
for (_i = 0, _len = connections.length; _i < _len; _i++) {
connection = connections[_i];
Logger.info("Initializing connection '" + connection.name + "'...");
connection['robot'] = this;
this.connections[connection.name] = new Connection(connection);
}
return this.connections;
};
Robot.prototype.data = function() {
var connection, device, n;
return {
name: this.name,
connections: (function() {
var _ref, _results;
_ref = this.connections;
_results = [];
for (n in _ref) {
connection = _ref[n];
_results.push(connection.data());
}
return _results;
}).call(this),
devices: (function() {
var _ref, _results;
_ref = this.devices;
_results = [];
for (n in _ref) {
device = _ref[n];
_results.push(device.data());
}
return _results;
}).call(this)
};
};
Robot.prototype.initDevices = function(devices) {
var device, _i, _len, _results;
Logger.info("Initializing devices...");
if (devices == null) {
return;
}
devices = [].concat(devices);
_results = [];
for (_i = 0, _len = devices.length; _i < _len; _i++) {
device = devices[_i];
Logger.info("Initializing device '" + device.name + "'...");
device['robot'] = this;
_results.push(this.devices[device.name] = new Device(device));
}
return _results;
};
Robot.prototype.initConnections = function(connections) {
var connection, _i, _len;
Logger.info("Initializing connections...");
if (connections == null) {
return;
}
connections = [].concat(connections);
for (_i = 0, _len = connections.length; _i < _len; _i++) {
connection = connections[_i];
Logger.info("Initializing connection '" + connection.name + "'...");
connection['robot'] = this;
this.connections[connection.name] = new Connection(connection);
}
return this.connections;
};
Robot.prototype.start = function() {
var _this = this;
return this.startConnections(function() {
return _this.robot.startDevices(function() {
return _this.robot.work.call(_this.robot, _this.robot);
Robot.prototype.initDevices = function(devices) {
var device, _i, _len, _results;
Logger.info("Initializing devices...");
if (devices == null) {
return;
}
devices = [].concat(devices);
_results = [];
for (_i = 0, _len = devices.length; _i < _len; _i++) {
device = devices[_i];
Logger.info("Initializing device '" + device.name + "'...");
device['robot'] = this;
_results.push(this.devices[device.name] = new Device(device));
}
return _results;
};
Robot.prototype.start = function() {
var _this = this;
return this.startConnections(function() {
return _this.robot.startDevices(function() {
return _this.robot.work.call(_this.robot, _this.robot);
});
});
});
};
};
Robot.prototype.startConnections = function(callback) {
var connection, n, starters, _ref;
Logger.info("Starting connections...");
starters = {};
_ref = this.connections;
for (n in _ref) {
connection = _ref[n];
starters[n] = connection.connect;
}
return Async.parallel(starters, callback);
};
Robot.prototype.startConnections = function(callback) {
var connection, n, starters, _ref;
Logger.info("Starting connections...");
starters = {};
_ref = this.connections;
for (n in _ref) {
connection = _ref[n];
starters[n] = connection.connect;
}
return Async.parallel(starters, callback);
};
Robot.prototype.startDevices = function(callback) {
var device, n, starters, _ref;
Logger.info("Starting devices...");
starters = {};
_ref = this.devices;
for (n in _ref) {
device = _ref[n];
this.robot[n] = device;
starters[n] = device.start;
}
return Async.parallel(starters, callback);
};
Robot.prototype.startDevices = function(callback) {
var device, n, starters, _ref;
Logger.info("Starting devices...");
starters = {};
_ref = this.devices;
for (n in _ref) {
device = _ref[n];
this.robot[n] = device;
starters[n] = device.start;
}
return Async.parallel(starters, callback);
};
Robot.prototype.requireAdaptor = function(adaptorName, connection) {
if (this.robot.adaptors[adaptorName] != null) {
if (typeof this.robot.adaptors[adaptorName] === 'string') {
this.robot.adaptors[adaptorName] = require(this.robot.adaptors[adaptorName]).adaptor({
Robot.prototype.requireAdaptor = function(adaptorName, connection) {
if (this.robot.adaptors[adaptorName] != null) {
if (typeof this.robot.adaptors[adaptorName] === 'string') {
this.robot.adaptors[adaptorName] = require(this.robot.adaptors[adaptorName]).adaptor({
name: adaptorName,
connection: connection
});
}
} else {
require("cylon-" + adaptorName).register(this);
this.robot.adaptors[adaptorName] = require("cylon-" + adaptorName).adaptor({
name: adaptorName,
connection: connection
});
}
} else {
require("cylon-" + adaptorName).register(this);
this.robot.adaptors[adaptorName] = require("cylon-" + adaptorName).adaptor({
name: adaptorName,
connection: connection
});
}
return this.robot.adaptors[adaptorName];
};
return this.robot.adaptors[adaptorName];
};
Robot.prototype.registerAdaptor = function(moduleName, adaptorName) {
if (this.adaptors[adaptorName] != null) {
return;
}
return this.adaptors[adaptorName] = moduleName;
};
Robot.prototype.registerAdaptor = function(moduleName, adaptorName) {
if (this.adaptors[adaptorName] != null) {
return;
}
return this.adaptors[adaptorName] = moduleName;
};
Robot.prototype.requireDriver = function(driverName, device, opts) {
if (opts == null) {
opts = {};
}
if (this.robot.drivers[driverName] != null) {
if (typeof this.robot.drivers[driverName] === 'string') {
this.robot.drivers[driverName] = require(this.robot.drivers[driverName]).driver({
Robot.prototype.requireDriver = function(driverName, device, opts) {
if (opts == null) {
opts = {};
}
if (this.robot.drivers[driverName] != null) {
if (typeof this.robot.drivers[driverName] === 'string') {
this.robot.drivers[driverName] = require(this.robot.drivers[driverName]).driver({
name: driverName,
device: device,
extraParams: opts
});
}
} else {
require("cylon-" + driverName).register(this);
this.robot.drivers[driverName] = require("cylon-" + driverName).driver({
name: driverName,
device: device,
extraParams: opts
});
}
} else {
require("cylon-" + driverName).register(this);
this.robot.drivers[driverName] = require("cylon-" + driverName).driver({
name: driverName,
device: device,
extraParams: opts
});
}
return this.robot.drivers[driverName];
};
return this.robot.drivers[driverName];
};
Robot.prototype.registerDriver = function(moduleName, driverName) {
if (this.drivers[driverName] != null) {
return;
}
return this.drivers[driverName] = moduleName;
};
Robot.prototype.registerDriver = function(moduleName, driverName) {
if (this.drivers[driverName] != null) {
return;
}
return this.drivers[driverName] = moduleName;
};
return Robot;
return Robot;
})();
})();
});
module.exports = Cylon.Robot;
}).call(this);

View File

@ -8,191 +8,197 @@
'use strict';
require('./cylon')
require('./basestar')
Connection = require("./connection")
Device = require("./device")
Async = require("async")
require './cylon'
require './basestar'
namespace = require 'node-namespace'
Connection = require "./connection"
Device = require "./device"
Async = require "async"
# A Robot is the primary interface for interacting with a collection of physical
# computing capabilities.
module.exports = class Robot
klass = this
namespace 'Cylon', ->
class @Robot
klass = this
# Public: Creates a new Robot
#
# opts - object containing Robot options
# name - optional, string name of the robot
# master - Cylon.Master class that orchestrates robots
# connection/connections - object connections to connect to
# device/devices - object devices to connect to
# work - work to be performed when the Robot is started
#
# Returns a new Robot
# Example (CoffeeScript):
# Cylon.robot
# name: "Spherobot!"
#
# connection:
# name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0'
#
# device:
# name: 'sphero', driver: 'sphero'
#
# work: (me) ->
# every 1.second(), ->
# me.sphero.roll 60, Math.floor(Math.random() * 360)
constructor: (opts = {}) ->
@robot = this
@name = opts.name or @constructor.randomName()
@master = opts.master
@connections = {}
@devices = {}
@adaptors = {}
@drivers = {}
# Public: Creates a new Robot
#
# opts - object containing Robot options
# name - optional, string name of the robot
# master - Cylon.Master class that orchestrates robots
# connection/connections - object connections to connect to
# device/devices - object devices to connect to
# work - work to be performed when the Robot is started
#
# Returns a new Robot
# Example (CoffeeScript):
# Cylon.robot
# name: "Spherobot!"
#
# connection:
# name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0'
#
# device:
# name: 'sphero', driver: 'sphero'
#
# work: (me) ->
# every 1.second(), ->
# me.sphero.roll 60, Math.floor(Math.random() * 360)
constructor: (opts = {}) ->
@robot = this
@name = opts.name or @constructor.randomName()
@master = opts.master
@connections = {}
@devices = {}
@adaptors = {}
@drivers = {}
@registerAdaptor "./loopback", "loopback"
@registerDriver "./ping", "ping"
@registerAdaptor "./loopback", "loopback"
@registerDriver "./ping", "ping"
@initConnections(opts.connection or opts.connections)
@initDevices(opts.device or opts.devices)
@work = opts.work or -> (Logger.info "No work yet")
@initConnections(opts.connection or opts.connections)
@initDevices(opts.device or opts.devices)
@work = opts.work or -> (Logger.info "No work yet")
for n, func of opts
reserved = ['connection', 'connections', 'device', 'devices', 'work']
@robot[n] = func unless n in reserved
for n, func of opts
reserved = ['connection', 'connections', 'device', 'devices', 'work']
@robot[n] = func unless n in reserved
# Public: Generates a random name for a Robot.
#
# Returns a string name
@randomName: ->
"Robot #{ Math.floor(Math.random() * 100000) }"
# Public: Generates a random name for a Robot.
#
# Returns a string name
@randomName: ->
"Robot #{ Math.floor(Math.random() * 100000) }"
# Public: Exports basic data for the Robot
#
# Returns an Object containing Robot data
data: ->
{
name: @name
connections: (connection.data() for n, connection of @connections)
devices: (device.data() for n, device of @devices)
}
# Public: Exports basic data for the Robot
#
# Returns an Object containing Robot data
data: ->
{
name: @name
connections: (connection.data() for n, connection of @connections)
devices: (device.data() for n, device of @devices)
}
# Public: Initializes all connections for the robot
#
# connections - connections to initialize
#
# Returns initialized connections
initConnections: (connections) =>
Logger.info "Initializing connections..."
return unless connections?
connections = [].concat connections
for connection in connections
Logger.info "Initializing connection '#{ connection.name }'..."
connection['robot'] = this
@connections[connection.name] = new Connection(connection)
# Public: Initializes all connections for the robot
#
# connections - connections to initialize
#
# Returns initialized connections
initConnections: (connections) =>
Logger.info "Initializing connections..."
return unless connections?
connections = [].concat connections
for connection in connections
Logger.info "Initializing connection '#{ connection.name }'..."
connection['robot'] = this
@connections[connection.name] = new Connection(connection)
@connections
@connections
# Public: Initializes all devices for the robot
#
# devices - devices to initialize
#
# Returns initialized devices
initDevices: (devices) =>
Logger.info "Initializing devices..."
return unless devices?
devices = [].concat devices
for device in devices
Logger.info "Initializing device '#{ device.name }'..."
device['robot'] = this
@devices[device.name] = new Device(device)
# Public: Initializes all devices for the robot
#
# devices - devices to initialize
#
# Returns initialized devices
initDevices: (devices) =>
Logger.info "Initializing devices..."
return unless devices?
devices = [].concat devices
for device in devices
Logger.info "Initializing device '#{ device.name }'..."
device['robot'] = this
@devices[device.name] = new Device(device)
# Public: Starts the Robot working.
#
# Starts the connections, devices, and work.
#
# Returns the result of the work
start: =>
@startConnections =>
@robot.startDevices =>
@robot.work.call(@robot, @robot)
# Public: Starts the Robot working.
#
# Starts the connections, devices, and work.
#
# Returns the result of the work
start: =>
@startConnections =>
@robot.startDevices =>
@robot.work.call(@robot, @robot)
# Public: Starts the Robot's connections and triggers a callback
#
# callback - callback function to be triggered
#
# Returns nothing
startConnections: (callback) =>
Logger.info "Starting connections..."
starters = {}
for n, connection of @connections
starters[n] = connection.connect
# Public: Starts the Robot's connections and triggers a callback
#
# callback - callback function to be triggered
#
# Returns nothing
startConnections: (callback) =>
Logger.info "Starting connections..."
starters = {}
for n, connection of @connections
starters[n] = connection.connect
Async.parallel starters, callback
Async.parallel starters, callback
# Public: Starts the Robot's devices and triggers a callback
#
# callback - callback function to be triggered
#
# Returns nothing
startDevices: (callback) =>
Logger.info "Starting devices..."
starters = {}
for n, device of @devices
@robot[n] = device
starters[n] = device.start
# Public: Starts the Robot's devices and triggers a callback
#
# callback - callback function to be triggered
#
# Returns nothing
startDevices: (callback) =>
Logger.info "Starting devices..."
starters = {}
for n, device of @devices
@robot[n] = device
starters[n] = device.start
Async.parallel starters, callback
Async.parallel starters, callback
# Public: Requires a hardware adaptor and adds it to @robot.adaptors
#
# adaptorName - module name of adaptor to require
# connection - the Connection that requested the adaptor be required
#
# Returns the set-up adaptor
requireAdaptor: (adaptorName, connection) ->
if @robot.adaptors[adaptorName]?
if typeof @robot.adaptors[adaptorName] is 'string'
@robot.adaptors[adaptorName] = require(@robot.adaptors[adaptorName]).adaptor(name: adaptorName, connection: connection)
else
require("cylon-#{adaptorName}").register(this)
@robot.adaptors[adaptorName] = require("cylon-#{adaptorName}").adaptor(name: adaptorName, connection: connection)
# Public: Requires a hardware adaptor and adds it to @robot.adaptors
#
# adaptorName - module name of adaptor to require
# connection - the Connection that requested the adaptor be required
#
# Returns the set-up adaptor
requireAdaptor: (adaptorName, connection) ->
if @robot.adaptors[adaptorName]?
if typeof @robot.adaptors[adaptorName] is 'string'
@robot.adaptors[adaptorName] = require(@robot.adaptors[adaptorName]).adaptor(name: adaptorName, connection: connection)
else
require("cylon-#{adaptorName}").register(this)
@robot.adaptors[adaptorName] = require("cylon-#{adaptorName}").adaptor(name: adaptorName, connection: connection)
return @robot.adaptors[adaptorName]
return @robot.adaptors[adaptorName]
# Public: Registers an Adaptor with the Robot
#
# moduleName - name of the Node module to require
# adaptorName - name of the adaptor to register the moduleName under
#
# Returns the registered module name
registerAdaptor: (moduleName, adaptorName) ->
return if @adaptors[adaptorName]?
@adaptors[adaptorName] = moduleName
# Public: Registers an Adaptor with the Robot
#
# moduleName - name of the Node module to require
# adaptorName - name of the adaptor to register the moduleName under
#
# Returns the registered module name
registerAdaptor: (moduleName, adaptorName) ->
return if @adaptors[adaptorName]?
@adaptors[adaptorName] = moduleName
# Public: Requires a hardware driver and adds it to @robot.drivers
#
# driverName - module name of driver to require
# connection - the Connection that requested the driver be required
#
# Returns the set-up driver
requireDriver: (driverName, device, opts = {}) ->
if @robot.drivers[driverName]?
if typeof @robot.drivers[driverName] is 'string'
@robot.drivers[driverName] = require(@robot.drivers[driverName]).driver(name: driverName, device: device, extraParams: opts)
else
require("cylon-#{driverName}").register(this)
@robot.drivers[driverName] = require("cylon-#{driverName}").driver(name: driverName, device: device, extraParams: opts)
# Public: Requires a hardware driver and adds it to @robot.drivers
#
# driverName - module name of driver to require
# connection - the Connection that requested the driver be required
#
# Returns the set-up driver
requireDriver: (driverName, device, opts = {}) ->
if @robot.drivers[driverName]?
if typeof @robot.drivers[driverName] is 'string'
@robot.drivers[driverName] = require(@robot.drivers[driverName]).driver(name: driverName, device: device, extraParams: opts)
else
require("cylon-#{driverName}").register(this)
@robot.drivers[driverName] = require("cylon-#{driverName}").driver(name: driverName, device: device, extraParams: opts)
return @robot.drivers[driverName]
return @robot.drivers[driverName]
# Public: Registers an Driver with the Robot
#
# moduleName - name of the Node module to require
# driverName - name of the driver to register the moduleName under
#
# Returns the registered module name
registerDriver: (moduleName, driverName) =>
return if @drivers[driverName]?
@drivers[driverName] = moduleName
# Public: Registers an Driver with the Robot
#
# moduleName - name of the Node module to require
# driverName - name of the driver to register the moduleName under
#
# Returns the registered module name
registerDriver: (moduleName, driverName) =>
return if @drivers[driverName]?
@drivers[driverName] = moduleName
module.exports = Cylon.Robot

View File

@ -1,8 +1,6 @@
(function() {
'use strict';
var Robot;
Robot = source("robot");
source("robot");
source("logger");
@ -16,7 +14,7 @@
whateverFunc = function() {
return Logger.info("whatever!");
};
robot = new Robot({
robot = new Cylon.Robot({
name: "irobot",
work: testWork,
whatever: whateverFunc
@ -26,8 +24,8 @@
});
it("has a random name, if not given", function() {
var r;
sinon.stub(Robot, 'randomName').returns('Electra');
r = new Robot;
sinon.stub(Cylon.Robot, 'randomName').returns('Electra');
r = new Cylon.Robot;
return r.name.should.be.equal('Electra');
});
it("has work", function() {

View File

@ -1,25 +1,25 @@
'use strict';
Robot = source("robot")
source("robot")
source("logger")
Logger.setup(false) # quiet logger for tests
describe "Robot", ->
testWork = ->
Logger.info "hi"
testWork = -> Logger.info "hi"
whateverFunc = -> Logger.info "whatever!"
whateverFunc = ->
Logger.info "whatever!"
robot = new Robot(name: "irobot", work: testWork, whatever: whateverFunc)
robot = new Cylon.Robot
name: "irobot"
work: testWork
whatever: whateverFunc
it "has a name, if given", ->
robot.name.should.be.equal 'irobot'
it "has a random name, if not given", ->
sinon.stub(Robot, 'randomName').returns('Electra')
r = new Robot
sinon.stub(Cylon.Robot, 'randomName').returns('Electra')
r = new Cylon.Robot
r.name.should.be.equal 'Electra'
it "has work", ->