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-12-16 03:15:29 +08:00
|
|
|
"use strict";
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
var initConnection = require("./connection"),
|
|
|
|
initDevice = require("./device"),
|
|
|
|
Logger = require("./logger"),
|
|
|
|
Utils = require("./utils"),
|
|
|
|
Config = require("./config");
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-03-04 06:33:29 +08:00
|
|
|
var Async = require("async"),
|
2014-12-16 03:15:29 +08:00
|
|
|
EventEmitter = require("events").EventEmitter;
|
2014-06-11 09:37:19 +08:00
|
|
|
|
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:
|
2014-12-16 03:15:29 +08:00
|
|
|
// name: "sphero", adaptor: "sphero", port: "/dev/rfcomm0"
|
2014-05-08 23:57:20 +08:00
|
|
|
//
|
|
|
|
// device:
|
2014-12-16 03:15:29 +08:00
|
|
|
// name: "sphero", driver: "sphero"
|
2014-05-08 23:57:20 +08:00
|
|
|
//
|
|
|
|
// 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-07-13 01:48:54 +08:00
|
|
|
opts = opts || {};
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
var methods = [
|
|
|
|
"toString",
|
|
|
|
"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);
|
2014-12-01 07:17:57 +08:00
|
|
|
}, this);
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-07-13 01:48:54 +08:00
|
|
|
this.name = opts.name || Robot.randomName();
|
2014-05-08 23:57:20 +08:00
|
|
|
this.connections = {};
|
|
|
|
this.devices = {};
|
|
|
|
this.adaptors = {};
|
|
|
|
this.drivers = {};
|
2014-08-06 09:41:57 +08:00
|
|
|
this.commands = {};
|
2014-05-08 23:57:20 +08:00
|
|
|
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) {
|
2014-10-03 02:35:33 +08:00
|
|
|
this.work = function() { Logger.debug("No work yet."); };
|
2014-06-11 09:38:55 +08:00
|
|
|
}
|
2014-06-10 04:39:16 +08:00
|
|
|
|
2014-11-11 02:52:55 +08:00
|
|
|
this.initConnections(opts);
|
|
|
|
this.initDevices(opts);
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
for (var n in opts) {
|
2014-10-28 04:40:38 +08:00
|
|
|
var opt = opts[n];
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-10-28 04:40:38 +08:00
|
|
|
if (this[n] !== undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-06 09:41:57 +08:00
|
|
|
|
2014-10-28 04:40:38 +08:00
|
|
|
this[n] = opt;
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
if (typeof opt === "function" && opts.commands == null) {
|
2014-10-28 04:40:38 +08:00
|
|
|
this.commands[n] = opt;
|
2014-08-06 09:41:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 04:40:38 +08:00
|
|
|
if (opts.commands) {
|
|
|
|
var cmds = opts.commands;
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
if (typeof cmds === "object") {
|
2014-10-28 04:40:38 +08:00
|
|
|
this.commands = cmds;
|
2014-08-06 09:41:57 +08:00
|
|
|
}
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
if (typeof cmds === "function") {
|
2014-10-28 04:40:38 +08:00
|
|
|
var result = cmds.call(this, this);
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
if (typeof result === "object" && !Array.isArray(result)) {
|
2014-10-28 04:40:38 +08:00
|
|
|
this.commands = result;
|
|
|
|
} else {
|
|
|
|
throw new Error("#commands function must return an object");
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
}
|
2014-08-06 09:41:57 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
var mode = Utils.fetch(Config, "mode", "manual");
|
2014-09-09 04:41:59 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
if (mode === "auto") {
|
|
|
|
// run on the next tick, to allow for "work" event handlers to be set up
|
2014-09-09 05:33:07 +08:00
|
|
|
setTimeout(this.start, 0);
|
2014-09-09 04:41:59 +08:00
|
|
|
}
|
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
|
|
|
|
|
|
|
// 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-07-30 09:49:35 +08:00
|
|
|
var devices = [],
|
2014-10-03 02:35:33 +08:00
|
|
|
connections = [],
|
|
|
|
n;
|
2014-07-13 01:48:54 +08:00
|
|
|
|
2014-10-03 02:35:33 +08:00
|
|
|
for (n in this.connections) {
|
2014-11-11 07:56:25 +08:00
|
|
|
connections.push(this.connections[n].toJSON());
|
2014-07-30 09:49:35 +08:00
|
|
|
}
|
2014-07-13 01:48:54 +08:00
|
|
|
|
2014-10-03 02:35:33 +08:00
|
|
|
for (n in this.devices) {
|
2014-11-11 07:56:25 +08:00
|
|
|
devices.push(this.devices[n].toJSON());
|
2014-07-30 09:49:35 +08:00
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
name: this.name,
|
|
|
|
connections: connections,
|
|
|
|
devices: devices,
|
2014-08-06 09:41:57 +08:00
|
|
|
commands: Object.keys(this.commands)
|
2014-05-08 23:57:20 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-11-27 01:33:19 +08:00
|
|
|
Robot.prototype.connection = function(name, conn) {
|
2014-11-21 00:43:08 +08:00
|
|
|
conn.robot = this;
|
2014-11-27 01:33:19 +08:00
|
|
|
conn.name = name;
|
2014-11-21 00:43:08 +08:00
|
|
|
|
|
|
|
if (this.connections[conn.name]) {
|
2014-12-16 03:15:29 +08:00
|
|
|
var original = conn.name,
|
|
|
|
str;
|
2014-11-21 00:43:08 +08:00
|
|
|
conn.name = Utils.makeUnique(original, Object.keys(this.connections));
|
2014-12-16 03:15:29 +08:00
|
|
|
|
|
|
|
str = "Connection names must be unique.";
|
|
|
|
str += "Renaming '" + original + "' to '" + conn.name + "'";
|
|
|
|
Logger.warn(str);
|
2014-11-21 00:43:08 +08:00
|
|
|
}
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
this.connections[conn.name] = initConnection(conn);
|
2014-11-21 00:43:08 +08:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
// Public: Initializes all connections for the robot
|
|
|
|
//
|
2014-11-11 02:52:55 +08:00
|
|
|
// opts - options array passed to constructor
|
2014-05-08 23:57:20 +08:00
|
|
|
//
|
|
|
|
// Returns initialized connections
|
2014-11-11 02:52:55 +08:00
|
|
|
Robot.prototype.initConnections = function(opts) {
|
2014-06-10 04:55:44 +08:00
|
|
|
Logger.info("Initializing connections.");
|
2014-06-10 05:02:06 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
var str;
|
|
|
|
|
2014-11-11 02:52:55 +08:00
|
|
|
var isArray = Array.isArray;
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-11-11 02:52:55 +08:00
|
|
|
if (opts.connection == null && opts.connections == null) {
|
|
|
|
return this.connections;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.connection) {
|
2014-12-16 03:15:29 +08:00
|
|
|
str = "Specifying a single connection with the 'connection' key ";
|
|
|
|
str += "is deprecated. It will be removed in 1.0.0.";
|
|
|
|
|
|
|
|
Logger.warn(str);
|
|
|
|
|
2014-11-27 01:33:19 +08:00
|
|
|
this.connection(opts.connection.name, opts.connection);
|
2014-11-11 02:52:55 +08:00
|
|
|
return this.connections;
|
|
|
|
}
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
if (typeof opts.connections === "object" && !isArray(opts.connections)) {
|
2014-11-11 02:52:55 +08:00
|
|
|
for (var name in opts.connections) {
|
2014-11-27 01:33:19 +08:00
|
|
|
this.connection(name, opts.connections[name]);
|
2014-11-11 02:52:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isArray(opts.connections)) {
|
2014-12-16 03:15:29 +08:00
|
|
|
str = "Specifying connections as an array is deprecated. ";
|
|
|
|
str += "It will be removed in 1.0.0.";
|
|
|
|
|
|
|
|
Logger.warn(str);
|
|
|
|
|
2014-11-11 02:52:55 +08:00
|
|
|
opts.connections.forEach(function(conn) {
|
2014-11-27 01:33:19 +08:00
|
|
|
this.connection(conn.name, conn);
|
2014-12-01 07:17:57 +08:00
|
|
|
}, this);
|
2014-11-11 02:52:55 +08:00
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
return this.connections;
|
|
|
|
};
|
|
|
|
|
2014-11-27 01:33:19 +08:00
|
|
|
Robot.prototype.device = function(name, device) {
|
2014-12-16 03:15:29 +08:00
|
|
|
var str;
|
|
|
|
|
2014-11-21 00:43:08 +08:00
|
|
|
device.robot = this;
|
2014-11-27 01:33:19 +08:00
|
|
|
device.name = name;
|
2014-11-21 00:43:08 +08:00
|
|
|
|
|
|
|
if (this.devices[device.name]) {
|
|
|
|
var original = device.name;
|
|
|
|
device.name = Utils.makeUnique(original, Object.keys(this.devices));
|
2014-12-16 03:15:29 +08:00
|
|
|
|
|
|
|
str = "Device names must be unique.";
|
|
|
|
str += "Renaming '" + original + "' to '" + device.name + "'";
|
|
|
|
Logger.warn(str);
|
2014-11-21 00:43:08 +08:00
|
|
|
}
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
if (typeof device.connection === "string") {
|
2014-11-21 00:43:08 +08:00
|
|
|
if (this.connections[device.connection] == null) {
|
2014-12-16 03:15:29 +08:00
|
|
|
str = "No connection found with the name " + device.connection + ".\n";
|
2014-11-21 00:43:08 +08:00
|
|
|
Logger.fatal(str);
|
2014-12-16 03:15:29 +08:00
|
|
|
process.emit("SIGINT");
|
2014-11-21 00:43:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
device.connection = this.connections[device.connection];
|
|
|
|
} else {
|
|
|
|
for (var conn in this.connections) {
|
|
|
|
device.connection = this.connections[conn];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
this.devices[device.name] = initDevice(device);
|
2014-11-21 00:43:08 +08:00
|
|
|
|
|
|
|
return this;
|
2014-12-16 03:15:29 +08:00
|
|
|
};
|
2014-11-21 00:43:08 +08:00
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
// Public: Initializes all devices for the robot
|
|
|
|
//
|
2014-11-11 02:52:55 +08:00
|
|
|
// opts - options array passed to constructor
|
2014-05-08 23:57:20 +08:00
|
|
|
//
|
|
|
|
// Returns initialized devices
|
2014-11-11 02:52:55 +08:00
|
|
|
Robot.prototype.initDevices = function(opts) {
|
2014-12-16 03:15:29 +08:00
|
|
|
var isArray = Array.isArray,
|
|
|
|
str;
|
2014-06-10 05:02:06 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
Logger.info("Initializing devices.");
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-11-11 02:52:55 +08:00
|
|
|
if (opts.device == null && opts.devices == null) {
|
|
|
|
return this.devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that there are connections to use
|
|
|
|
if (!Object.keys(this.connections).length) {
|
2014-12-16 03:15:29 +08:00
|
|
|
throw new Error("No connections specified");
|
2014-11-11 02:52:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.device) {
|
2014-12-16 03:15:29 +08:00
|
|
|
str = "Specifying a single device with the 'device' key is deprecated. ";
|
|
|
|
str += "It will be removed in 1.0.0.";
|
|
|
|
|
|
|
|
Logger.warn(str);
|
2014-11-27 01:33:19 +08:00
|
|
|
this.device(opts.device.name, opts.device);
|
2014-11-11 02:52:55 +08:00
|
|
|
return this.devices;
|
|
|
|
}
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
if (typeof opts.devices === "object" && !isArray(opts.devices)) {
|
2014-11-11 02:52:55 +08:00
|
|
|
for (var name in opts.devices) {
|
2014-11-27 01:33:19 +08:00
|
|
|
this.device(name, opts.devices[name]);
|
2014-11-11 02:52:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isArray(opts.devices)) {
|
2014-12-16 03:15:29 +08:00
|
|
|
str = "Specifying devices as an array is deprecated.";
|
|
|
|
str += "It will be removed in 1.0.0.";
|
|
|
|
Logger.warn(str);
|
2014-11-11 02:52:55 +08:00
|
|
|
opts.devices.forEach(function(device) {
|
2014-11-27 01:33:19 +08:00
|
|
|
this.device(device.name, device);
|
2014-12-01 07:17:57 +08:00
|
|
|
}, this);
|
2014-11-11 02:52:55 +08:00
|
|
|
}
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
return this.devices;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Starts the Robot working.
|
|
|
|
//
|
|
|
|
// Starts the connections, devices, and work.
|
|
|
|
//
|
|
|
|
// Returns the result of the work
|
2014-10-04 15:56:08 +08:00
|
|
|
Robot.prototype.start = function(callback) {
|
2014-10-01 03:15:46 +08:00
|
|
|
if (this.running) {
|
2014-10-01 03:22:00 +08:00
|
|
|
return this;
|
2014-10-01 03:15:46 +08:00
|
|
|
}
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
var mode = Utils.fetch(Config, "workMode", "async");
|
2014-06-10 04:55:44 +08:00
|
|
|
|
|
|
|
Async.series([
|
2014-07-04 00:35:24 +08:00
|
|
|
this.startConnections,
|
|
|
|
this.startDevices,
|
2014-10-04 14:07:07 +08:00
|
|
|
function(callback) {
|
2014-12-16 03:15:29 +08:00
|
|
|
if (mode === "async") {
|
2014-10-04 14:07:07 +08:00
|
|
|
this.startWork();
|
|
|
|
}
|
|
|
|
callback(null, true);
|
|
|
|
}.bind(this)
|
2014-10-04 15:56:08 +08:00
|
|
|
], function(err, results) {
|
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-12-16 03:15:29 +08:00
|
|
|
if (typeof(this.error) === "function") {
|
2014-10-01 07:13:20 +08:00
|
|
|
this.error.call(this, err);
|
|
|
|
}
|
2014-12-16 03:15:29 +08:00
|
|
|
this.emit("error", err);
|
2014-06-10 04:55:44 +08:00
|
|
|
}
|
2014-12-16 03:15:29 +08:00
|
|
|
if (typeof(callback) === "function") {
|
2014-10-04 15:56:08 +08:00
|
|
|
callback(err, results);
|
|
|
|
}
|
2014-10-01 07:13:20 +08:00
|
|
|
}.bind(this));
|
2014-10-01 03:15:46 +08:00
|
|
|
|
2014-09-09 04:41:59 +08:00
|
|
|
return this;
|
2014-05-08 23:57:20 +08:00
|
|
|
};
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
// Public: Starts the Robot"s work and triggers a callback
|
2014-10-04 14:07:07 +08:00
|
|
|
//
|
|
|
|
// callback - callback function to be triggered
|
|
|
|
//
|
|
|
|
// Returns nothing
|
|
|
|
Robot.prototype.startWork = function() {
|
2014-12-16 03:15:29 +08:00
|
|
|
Logger.info("Working.");
|
2014-10-04 14:07:07 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
this.emit("ready", this);
|
2014-10-04 14:07:07 +08:00
|
|
|
this.work.call(this, this);
|
|
|
|
this.running = true;
|
|
|
|
};
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
// Public: Starts the Robot"s connections and triggers a callback
|
2014-05-08 23:57:20 +08:00
|
|
|
//
|
|
|
|
// callback - callback function to be triggered
|
|
|
|
//
|
|
|
|
// Returns nothing
|
|
|
|
Robot.prototype.startConnections = function(callback) {
|
2014-06-10 04:55:44 +08:00
|
|
|
Logger.info("Starting connections.");
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-11-29 06:42:26 +08:00
|
|
|
var starters = Object.keys(this.connections).map(function(n) {
|
|
|
|
var conn = this[n] = this.connections[n];
|
|
|
|
|
|
|
|
return function(cb) {
|
|
|
|
var str = "Starting connection '" + n + "'";
|
|
|
|
|
|
|
|
if (conn.host) {
|
|
|
|
str += " on host " + conn.host;
|
|
|
|
} else if (conn.port) {
|
|
|
|
str += " on port " + conn.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.debug(str + ".");
|
|
|
|
return conn.connect.call(conn, cb);
|
|
|
|
};
|
2014-12-01 07:17:57 +08:00
|
|
|
}, this);
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
return Async.parallel(starters, callback);
|
|
|
|
};
|
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
// Public: Starts the Robot"s devices and triggers a callback
|
2014-05-08 23:57:20 +08:00
|
|
|
//
|
|
|
|
// callback - callback function to be triggered
|
|
|
|
//
|
|
|
|
// Returns nothing
|
|
|
|
Robot.prototype.startDevices = function(callback) {
|
2014-06-10 04:55:44 +08:00
|
|
|
Logger.info("Starting devices.");
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2014-11-29 06:42:26 +08:00
|
|
|
var starters = Object.keys(this.devices).map(function(n) {
|
|
|
|
var device = this[n] = this.devices[n];
|
|
|
|
|
|
|
|
return function(cb) {
|
|
|
|
var str = "Starting device '" + n + "'";
|
|
|
|
|
|
|
|
if (device.pin) {
|
|
|
|
str += " on pin " + device.pin;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.debug(str + ".");
|
|
|
|
return device.start.call(device, cb);
|
|
|
|
};
|
2014-12-01 07:17:57 +08:00
|
|
|
}, this);
|
2014-05-08 23:57:20 +08:00
|
|
|
|
|
|
|
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) {
|
2014-09-05 02:14:47 +08:00
|
|
|
callback = callback || function() {};
|
|
|
|
|
2014-10-15 01:25:58 +08:00
|
|
|
var fns = Object.keys(this.devices).map(function(d) {
|
2014-06-13 06:31:49 +08:00
|
|
|
var device = this.devices[d];
|
2014-12-01 03:47:33 +08:00
|
|
|
return device.halt.bind(device);
|
2014-12-01 07:17:57 +08:00
|
|
|
}, this);
|
2014-06-10 05:02:06 +08:00
|
|
|
|
2014-09-05 01:18:18 +08:00
|
|
|
Async.parallel(fns, function() {
|
2014-10-15 01:25:58 +08:00
|
|
|
var fns = Object.keys(this.connections).map(function(c) {
|
2014-09-05 01:18:18 +08:00
|
|
|
var connection = this.connections[c];
|
2014-12-01 03:47:33 +08:00
|
|
|
return connection.disconnect.bind(connection);
|
2014-12-01 07:17:57 +08:00
|
|
|
}, this);
|
2014-06-13 06:31:49 +08:00
|
|
|
|
2014-09-05 01:18:18 +08:00
|
|
|
Async.parallel(fns, callback);
|
|
|
|
}.bind(this));
|
2014-10-01 03:15:46 +08:00
|
|
|
|
|
|
|
this.running = false;
|
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 + "']";
|
|
|
|
};
|