2013-10-25 05:25:42 +08:00
|
|
|
/*
|
|
|
|
* robot
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
2014-02-28 03:17:02 +08:00
|
|
|
* Copyright (c) 2013-2014 The Hybrid Group
|
2013-10-25 05:25:42 +08:00
|
|
|
* Licensed under the Apache 2.0 license.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
'use strict';
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
var Connection = require("./connection"),
|
2014-05-14 09:14:36 +08:00
|
|
|
Device = require("./device"),
|
2014-05-14 10:44:40 +08:00
|
|
|
Logger = require('./logger'),
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils = require('./utils'),
|
2014-05-14 09:14:36 +08:00
|
|
|
config = require('./config');
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-03-04 06:33:29 +08:00
|
|
|
var Async = require("async"),
|
2014-05-08 23:57:20 +08:00
|
|
|
EventEmitter = require('events').EventEmitter;
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2014-06-11 09:37:19 +08:00
|
|
|
var missingModuleError = function(module) {
|
|
|
|
var string = "Cannot find the '" + module + "' module. ";
|
|
|
|
string += "Please install it with 'npm install " + module + "' and try again.";
|
|
|
|
|
|
|
|
console.log(string);
|
|
|
|
|
|
|
|
process.emit('SIGINT');
|
|
|
|
};
|
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
// Public: Creates a new Robot
|
|
|
|
//
|
|
|
|
// opts - object containing Robot options
|
|
|
|
// name - optional, string name of the robot
|
|
|
|
// 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) ->
|
2014-06-06 03:11:37 +08:00
|
|
|
// Utils.every 1.second(), ->
|
2014-05-08 23:57:20 +08:00
|
|
|
// me.sphero.roll 60, Math.floor(Math.random() * 360//
|
2014-06-10 04:39:16 +08:00
|
|
|
var Robot = module.exports = function Robot(opts) {
|
2014-05-08 23:57:20 +08:00
|
|
|
if (opts == null) {
|
|
|
|
opts = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var methods = [
|
|
|
|
"toString",
|
|
|
|
"registerDriver",
|
|
|
|
"requireDriver",
|
|
|
|
"registerAdaptor",
|
|
|
|
"requireAdaptor",
|
|
|
|
"halt",
|
|
|
|
"startDevices",
|
|
|
|
"startConnections",
|
|
|
|
"start",
|
|
|
|
"initDevices",
|
|
|
|
"initConnections"
|
|
|
|
];
|
|
|
|
|
2014-06-10 04:39:16 +08:00
|
|
|
methods.forEach(function(method) {
|
2014-07-04 00:35:24 +08:00
|
|
|
this[method] = this[method].bind(this);
|
|
|
|
}.bind(this));
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
this.name = opts.name || this.constructor.randomName();
|
|
|
|
this.connections = {};
|
|
|
|
this.devices = {};
|
|
|
|
this.adaptors = {};
|
|
|
|
this.drivers = {};
|
|
|
|
this.commands = [];
|
|
|
|
this.running = false;
|
2014-06-10 04:39:16 +08:00
|
|
|
this.work = opts.work || opts.play;
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-06-11 09:38:55 +08:00
|
|
|
if (!this.work) {
|
|
|
|
this.work = function() { console.log("No work yet."); }
|
|
|
|
}
|
2014-06-10 04:39:16 +08:00
|
|
|
|
|
|
|
this.registerDefaults();
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
this.initConnections(opts.connection || opts.connections);
|
|
|
|
this.initDevices(opts.device || opts.devices);
|
|
|
|
|
|
|
|
for (var n in opts) {
|
|
|
|
var func = opts[n],
|
|
|
|
reserved = ['connection', 'connections', 'device', 'devices', 'work'];
|
|
|
|
|
2014-06-10 04:39:16 +08:00
|
|
|
if (reserved.indexOf(n) < 0) {
|
|
|
|
this[n] = func;
|
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils.subclass(Robot, EventEmitter);
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-06-10 04:39:16 +08:00
|
|
|
// Public: Registers the default Drivers and Adaptors with Cylon.
|
|
|
|
//
|
|
|
|
// Returns nothing.
|
|
|
|
Robot.prototype.registerDefaults = function registerDefaults() {
|
|
|
|
this.registerAdaptor("./test/loopback", "loopback");
|
|
|
|
this.registerAdaptor("./test/test-adaptor", "test");
|
|
|
|
|
|
|
|
this.registerDriver("./test/ping", "ping");
|
|
|
|
this.registerDriver("./test/test-driver", "test");
|
|
|
|
};
|
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
// Public: Generates a random name for a Robot.
|
|
|
|
//
|
|
|
|
// Returns a string name
|
|
|
|
Robot.randomName = function() {
|
|
|
|
return "Robot " + (Math.floor(Math.random() * 100000));
|
|
|
|
};
|
|
|
|
|
2014-06-07 05:11:35 +08:00
|
|
|
// Public: Expresses the Robot in a JSON-serializable format
|
2014-05-08 23:57:20 +08:00
|
|
|
//
|
|
|
|
// Returns an Object containing Robot data
|
2014-06-07 05:11:35 +08:00
|
|
|
Robot.prototype.toJSON = function() {
|
2014-05-08 23:57:20 +08:00
|
|
|
var connections = (function() {
|
|
|
|
var results = [];
|
|
|
|
for (var n in this.connections) {
|
|
|
|
var conn = this.connections[n];
|
2014-06-07 05:11:35 +08:00
|
|
|
results.push(conn.toJSON());
|
2014-02-28 03:17:02 +08:00
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
return results;
|
|
|
|
}).call(this);
|
|
|
|
|
|
|
|
var devices = (function() {
|
|
|
|
var results = [];
|
|
|
|
for (var n in this.devices) {
|
|
|
|
var device = this.devices[n];
|
2014-06-07 05:11:35 +08:00
|
|
|
results.push(device.toJSON());
|
2014-05-08 23:57:20 +08:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}).call(this);
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: this.name,
|
|
|
|
connections: connections,
|
|
|
|
devices: devices,
|
|
|
|
commands: this.commands
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Initializes all connections for the robot
|
|
|
|
//
|
|
|
|
// connections - connections to initialize
|
|
|
|
//
|
|
|
|
// Returns initialized connections
|
|
|
|
Robot.prototype.initConnections = function(connections) {
|
2014-06-10 04:55:44 +08:00
|
|
|
Logger.info("Initializing connections.");
|
2014-06-10 05:02:06 +08:00
|
|
|
|
|
|
|
if (connections == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
connections = [].concat(connections);
|
|
|
|
|
|
|
|
for (var i = 0; i < connections.length; i++) {
|
|
|
|
var connection = connections[i];
|
2014-06-10 04:55:44 +08:00
|
|
|
Logger.info("Initializing connection '" + connection.name + "'.");
|
2014-05-08 23:57:20 +08:00
|
|
|
connection['robot'] = this;
|
|
|
|
this.connections[connection.name] = new Connection(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.connections;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Initializes all devices for the robot
|
|
|
|
//
|
|
|
|
// devices - devices to initialize
|
|
|
|
//
|
|
|
|
// Returns initialized devices
|
|
|
|
Robot.prototype.initDevices = function(devices) {
|
2014-06-10 04:55:44 +08:00
|
|
|
Logger.info("Initializing devices.");
|
2014-06-10 05:02:06 +08:00
|
|
|
|
|
|
|
if (devices == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
devices = [].concat(devices);
|
|
|
|
|
|
|
|
for (var i = 0; i < devices.length; i++) {
|
|
|
|
var device = devices[i];
|
2014-06-10 04:55:44 +08:00
|
|
|
Logger.info("Initializing device '" + device.name + "'.");
|
2014-05-08 23:57:20 +08:00
|
|
|
device['robot'] = this;
|
|
|
|
this.devices[device.name] = this._createDevice(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.devices;
|
|
|
|
};
|
|
|
|
|
|
|
|
Robot.prototype._createDevice = function(device) {
|
|
|
|
return new Device(device);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Starts the Robot working.
|
|
|
|
//
|
|
|
|
// Starts the connections, devices, and work.
|
|
|
|
//
|
|
|
|
// Returns the result of the work
|
|
|
|
Robot.prototype.start = function() {
|
2014-06-10 04:55:44 +08:00
|
|
|
var begin = function(callback) {
|
2014-07-04 00:35:24 +08:00
|
|
|
this.work.call(this, this);
|
|
|
|
this.running = true;
|
|
|
|
this.emit('working');
|
2014-06-10 04:55:44 +08:00
|
|
|
|
|
|
|
Logger.info('Working.');
|
|
|
|
|
|
|
|
callback(null, true);
|
2014-07-04 00:35:24 +08:00
|
|
|
}.bind(this);
|
2014-06-10 04:55:44 +08:00
|
|
|
|
|
|
|
Async.series([
|
2014-07-04 00:35:24 +08:00
|
|
|
this.startConnections,
|
|
|
|
this.startDevices,
|
2014-06-10 04:55:44 +08:00
|
|
|
begin
|
2014-06-11 09:38:00 +08:00
|
|
|
], function(err) {
|
2014-06-10 04:55:44 +08:00
|
|
|
if (!!err) {
|
|
|
|
Logger.fatal("An error occured while trying to start the robot:");
|
|
|
|
Logger.fatal(err);
|
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Starts the Robot's connections and triggers a callback
|
|
|
|
//
|
|
|
|
// callback - callback function to be triggered
|
|
|
|
//
|
|
|
|
// Returns nothing
|
|
|
|
Robot.prototype.startConnections = function(callback) {
|
|
|
|
var starters = {};
|
|
|
|
|
2014-06-10 04:55:44 +08:00
|
|
|
Logger.info("Starting connections.");
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
for (var n in this.connections) {
|
|
|
|
var connection = this.connections[n];
|
2014-06-10 04:24:42 +08:00
|
|
|
this[n] = connection;
|
2014-05-08 23:57:20 +08:00
|
|
|
starters[n] = connection.connect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Async.parallel(starters, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Starts the Robot's devices and triggers a callback
|
|
|
|
//
|
|
|
|
// callback - callback function to be triggered
|
|
|
|
//
|
|
|
|
// Returns nothing
|
|
|
|
Robot.prototype.startDevices = function(callback) {
|
|
|
|
var starters = {};
|
|
|
|
|
2014-06-10 04:55:44 +08:00
|
|
|
Logger.info("Starting devices.");
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
for (var n in this.devices) {
|
|
|
|
var device = this.devices[n];
|
2014-06-10 04:24:42 +08:00
|
|
|
this[n] = device;
|
2014-05-08 23:57:20 +08:00
|
|
|
starters[n] = device.start;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Async.parallel(starters, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Halts the Robot.
|
|
|
|
//
|
|
|
|
// Halts the devices, disconnects the connections.
|
|
|
|
//
|
2014-06-13 06:31:49 +08:00
|
|
|
// callback - callback to be triggered when the Robot is stopped
|
|
|
|
//
|
2014-05-08 23:57:20 +08:00
|
|
|
// Returns nothing
|
2014-06-13 06:31:49 +08:00
|
|
|
Robot.prototype.halt = function(callback) {
|
|
|
|
var fns = [];
|
|
|
|
|
2014-06-10 05:02:06 +08:00
|
|
|
for (var d in this.devices) {
|
2014-06-13 06:31:49 +08:00
|
|
|
var device = this.devices[d];
|
|
|
|
fns.push(device.halt.bind(device));
|
2014-06-10 05:02:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var c in this.connections) {
|
2014-06-13 06:31:49 +08:00
|
|
|
var connection = this.connections[c];
|
|
|
|
fns.push(connection.halt.bind(connection));
|
2014-06-10 05:02:06 +08:00
|
|
|
}
|
2014-06-13 06:31:49 +08:00
|
|
|
|
|
|
|
Async.parallel(fns, callback);
|
2014-05-08 23:57:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Initialize an 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 adaptor
|
|
|
|
Robot.prototype.initAdaptor = function(adaptorName, connection, opts) {
|
2014-06-10 05:02:06 +08:00
|
|
|
if (opts == null) {
|
|
|
|
opts = {};
|
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-06-10 04:24:42 +08:00
|
|
|
var adaptor = this.requireAdaptor(adaptorName, opts).adaptor({
|
2014-05-08 23:57:20 +08:00
|
|
|
name: adaptorName,
|
|
|
|
connection: connection,
|
|
|
|
extraParams: opts
|
|
|
|
});
|
|
|
|
|
2014-05-14 09:14:36 +08:00
|
|
|
if (config.testing_mode) {
|
2014-06-10 04:24:42 +08:00
|
|
|
var testAdaptor = this.requireAdaptor('test').adaptor({
|
2014-05-08 23:57:20 +08:00
|
|
|
name: adaptorName,
|
|
|
|
connection: connection,
|
|
|
|
extraParams: opts
|
|
|
|
});
|
2014-06-11 03:29:44 +08:00
|
|
|
return Utils.proxyTestStubs(adaptor.commands, testAdaptor);
|
2014-05-08 23:57:20 +08:00
|
|
|
} else {
|
|
|
|
return adaptor;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Requires a hardware adaptor and adds it to @robot.adaptors
|
|
|
|
//
|
|
|
|
// adaptorName - module name of adaptor to require
|
|
|
|
//
|
|
|
|
// Returns the module for the adaptor
|
2014-05-24 09:42:29 +08:00
|
|
|
Robot.prototype.requireAdaptor = function(adaptorName, opts) {
|
2014-06-10 04:24:42 +08:00
|
|
|
if (this.adaptors[adaptorName] == null) {
|
2014-05-24 09:42:29 +08:00
|
|
|
var moduleName = opts.module || adaptorName;
|
2014-06-10 04:24:42 +08:00
|
|
|
this.registerAdaptor("cylon-" + moduleName, adaptorName);
|
|
|
|
this.adaptors[adaptorName].register(this);
|
2014-05-08 23:57:20 +08:00
|
|
|
}
|
2014-06-10 04:24:42 +08:00
|
|
|
return this.adaptors[adaptorName];
|
2014-05-08 23:57:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
Robot.prototype.registerAdaptor = function(moduleName, adaptorName) {
|
|
|
|
if (this.adaptors[adaptorName] == null) {
|
|
|
|
try {
|
|
|
|
return this.adaptors[adaptorName] = require(moduleName);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code === "MODULE_NOT_FOUND") {
|
|
|
|
missingModuleError(moduleName);
|
2014-02-28 03:17:02 +08:00
|
|
|
} else {
|
2014-05-08 23:57:20 +08:00
|
|
|
throw e;
|
2014-02-28 03:17:02 +08:00
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Init a hardware driver
|
|
|
|
//
|
|
|
|
// driverName - driver name
|
|
|
|
// device - the Device that requested the driver be initialized
|
|
|
|
// opts - object containing options when initializing driver
|
|
|
|
//
|
|
|
|
// Returns the new driver
|
|
|
|
Robot.prototype.initDriver = function(driverName, device, opts) {
|
2014-06-10 05:02:06 +08:00
|
|
|
if (opts == null) {
|
|
|
|
opts = {};
|
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-06-10 04:24:42 +08:00
|
|
|
var driver = this.requireDriver(driverName).driver({
|
2014-05-08 23:57:20 +08:00
|
|
|
name: driverName,
|
|
|
|
device: device,
|
|
|
|
extraParams: opts
|
|
|
|
});
|
|
|
|
|
2014-05-14 09:14:36 +08:00
|
|
|
if (config.testing_mode) {
|
2014-06-10 04:24:42 +08:00
|
|
|
var testDriver = this.requireDriver('test').driver({
|
2014-05-08 23:57:20 +08:00
|
|
|
name: driverName,
|
|
|
|
device: device,
|
|
|
|
extraParams: opts
|
|
|
|
});
|
|
|
|
|
2014-06-11 03:29:44 +08:00
|
|
|
return Utils.proxyTestStubs(driver.commands, testDriver);
|
2014-05-08 23:57:20 +08:00
|
|
|
} else {
|
|
|
|
return driver;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Requires module for a driver and adds it to @robot.drivers
|
|
|
|
//
|
|
|
|
// driverName - module name of driver to require
|
|
|
|
//
|
|
|
|
// Returns the module for driver
|
|
|
|
Robot.prototype.requireDriver = function(driverName) {
|
2014-06-10 04:24:42 +08:00
|
|
|
if (this.drivers[driverName] == null) {
|
|
|
|
this.registerDriver("cylon-" + driverName, driverName);
|
|
|
|
this.drivers[driverName].register(this);
|
2014-05-08 23:57:20 +08:00
|
|
|
}
|
2014-06-10 04:24:42 +08:00
|
|
|
return this.drivers[driverName];
|
2014-05-08 23:57:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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 nam//
|
|
|
|
Robot.prototype.registerDriver = function(moduleName, driverName) {
|
|
|
|
if (this.drivers[driverName] == null) {
|
|
|
|
try {
|
|
|
|
return this.drivers[driverName] = require(moduleName);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code === "MODULE_NOT_FOUND") {
|
|
|
|
missingModuleError(moduleName);
|
2014-02-28 03:17:02 +08:00
|
|
|
} else {
|
2014-05-08 23:57:20 +08:00
|
|
|
throw e;
|
2014-02-28 03:17:02 +08:00
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-03-27 05:32:52 +08:00
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
// Public: Returns basic info about the robot as a String
|
|
|
|
//
|
|
|
|
// Returns a String
|
|
|
|
Robot.prototype.toString = function() {
|
|
|
|
return "[Robot name='" + this.name + "']";
|
|
|
|
};
|