2013-10-25 05:25:42 +08:00
|
|
|
/*
|
|
|
|
* robot
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
2015-01-08 04:58:50 +08:00
|
|
|
* Copyright (c) 2013-2015 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
|
|
|
|
2015-01-23 06:40:09 +08:00
|
|
|
var initializer = require("./initializer"),
|
2014-12-16 03:15:29 +08:00
|
|
|
Logger = require("./logger"),
|
|
|
|
Utils = require("./utils"),
|
2015-02-20 09:23:41 +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-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
|
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",
|
2015-01-18 07:48:45 +08:00
|
|
|
"initRobot",
|
2014-05-08 23:57:20 +08:00
|
|
|
"initDevices",
|
2015-01-21 11:29:40 +08:00
|
|
|
"initConnections",
|
|
|
|
"log"
|
2014-05-08 23:57:20 +08:00
|
|
|
];
|
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
methods.forEach(function(method) {
|
|
|
|
this[method] = this[method].bind(this);
|
|
|
|
}, this);
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2015-01-18 07:48:45 +08:00
|
|
|
this.initRobot(opts);
|
2014-11-11 02:52:55 +08:00
|
|
|
this.initConnections(opts);
|
|
|
|
this.initDevices(opts);
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
for (var name in opts) {
|
|
|
|
var opt = opts[name];
|
|
|
|
|
2014-12-18 06:42:34 +08:00
|
|
|
if (this[name] !== undefined) {
|
2015-02-20 09:23:41 +08:00
|
|
|
continue;
|
2014-10-28 04:40:38 +08:00
|
|
|
}
|
2014-08-06 09:41:57 +08:00
|
|
|
|
2014-12-18 06:42:34 +08:00
|
|
|
this[name] = opt;
|
2014-10-28 04:40:38 +08:00
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
if (opts.commands == null && typeof opt === "function") {
|
2014-12-18 06:42:34 +08:00
|
|
|
this.commands[name] = opt;
|
2014-08-06 09:41:57 +08:00
|
|
|
}
|
2015-02-20 09:23:41 +08:00
|
|
|
}
|
2014-08-06 09:41:57 +08:00
|
|
|
|
2014-10-28 04:40:38 +08:00
|
|
|
if (opts.commands) {
|
2015-02-20 09:23:41 +08:00
|
|
|
var cmds;
|
2014-10-28 04:40:38 +08:00
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
if (typeof opts.commands === "function") {
|
|
|
|
cmds = opts.commands.call(this);
|
|
|
|
} else {
|
|
|
|
cmds = opts.commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof cmds === "object" && !Array.isArray(cmds)) {
|
2014-10-28 04:40:38 +08:00
|
|
|
this.commands = cmds;
|
2014-12-18 07:07:03 +08:00
|
|
|
} else {
|
|
|
|
var err = "#commands must be an object ";
|
|
|
|
err += "or a function that returns an object";
|
|
|
|
throw new Error(err);
|
2014-10-28 04:40:38 +08:00
|
|
|
}
|
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() {
|
2015-02-20 09:23:41 +08:00
|
|
|
var connections = [],
|
|
|
|
devices = [],
|
|
|
|
n;
|
|
|
|
|
|
|
|
for (n in this.connections) {
|
|
|
|
var conn = this.connections[n];
|
|
|
|
connections.push(conn.toJSON.call(conn));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n in this.devices) {
|
|
|
|
var device = this.devices[n];
|
|
|
|
devices.push(device.toJSON.call(device));
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:57:20 +08:00
|
|
|
return {
|
|
|
|
name: this.name,
|
2015-02-20 09:23:41 +08:00
|
|
|
connections: connections,
|
|
|
|
devices: devices,
|
|
|
|
commands: Object.keys(this.commands),
|
|
|
|
events: Array.isArray(this.events) ? this.events : []
|
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;
|
2015-01-08 02:16:32 +08:00
|
|
|
|
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 + "'";
|
2015-01-21 11:29:40 +08:00
|
|
|
this.log("warn", str);
|
2014-11-21 00:43:08 +08:00
|
|
|
}
|
|
|
|
|
2015-01-23 06:40:09 +08:00
|
|
|
this.connections[conn.name] = initializer("adaptor", conn);
|
2014-11-21 00:43:08 +08:00
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2015-01-18 07:48:45 +08:00
|
|
|
// Public: Initializes all vars for robot
|
|
|
|
//
|
|
|
|
// opts - options array passed to constructor
|
|
|
|
//
|
|
|
|
// Returns null
|
|
|
|
Robot.prototype.initRobot = function(opts) {
|
|
|
|
this.name = opts.name || Robot.randomName();
|
|
|
|
this.connections = {};
|
|
|
|
this.devices = {};
|
|
|
|
this.adaptors = {};
|
|
|
|
this.drivers = {};
|
|
|
|
this.commands = {};
|
|
|
|
this.running = false;
|
|
|
|
this.work = opts.work || opts.play;
|
|
|
|
|
|
|
|
if (!this.work) {
|
2015-01-21 11:29:40 +08:00
|
|
|
this.work = function() { this.log("debug", "No work yet."); };
|
2015-01-18 07:48:45 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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) {
|
2015-01-21 11:29:40 +08:00
|
|
|
this.log("info", "Initializing connections.");
|
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) {
|
2015-01-23 11:25:45 +08:00
|
|
|
this.deprecationWarning("connection");
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
if (typeof opts.connections === "object") {
|
|
|
|
if (Array.isArray(opts.connections)) {
|
2015-01-18 13:35:40 +08:00
|
|
|
this.performArraySetup(opts.connections, "connection", "connections");
|
2015-01-08 02:16:32 +08:00
|
|
|
return this.connections;
|
2014-12-18 06:42:34 +08:00
|
|
|
}
|
2014-12-16 03:15:29 +08:00
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
for (var key in opts.connections) {
|
|
|
|
var conn = opts.connections[key],
|
|
|
|
name = typeof key === "string" ? key : conn.name;
|
2015-01-08 02:16:32 +08:00
|
|
|
|
|
|
|
if (conn.devices) {
|
2015-02-20 09:23:41 +08:00
|
|
|
opts.devices = opts.devices || {};
|
2015-01-08 02:16:32 +08:00
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
for (var d in conn.devices) {
|
|
|
|
var device = conn.devices[d];
|
2015-01-08 02:16:32 +08:00
|
|
|
device.connection = name;
|
2015-02-20 09:23:41 +08:00
|
|
|
opts.devices[d] = device;
|
|
|
|
}
|
2015-01-08 02:16:32 +08:00
|
|
|
|
|
|
|
delete conn.devices;
|
|
|
|
}
|
|
|
|
|
2014-12-18 06:42:34 +08:00
|
|
|
this.connection(name, conn);
|
2015-02-20 09:23:41 +08:00
|
|
|
}
|
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 + "'";
|
2015-01-21 11:29:40 +08:00
|
|
|
this.log("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";
|
2015-01-21 11:29:40 +08:00
|
|
|
this.log("fatal", str);
|
2014-12-16 03:15:29 +08:00
|
|
|
process.emit("SIGINT");
|
2014-11-21 00:43:08 +08:00
|
|
|
}
|
|
|
|
|
2014-12-18 06:42:34 +08:00
|
|
|
device.connection = this.connections[device.connection];
|
2014-11-21 00:43:08 +08:00
|
|
|
} else {
|
2015-02-20 09:23:41 +08:00
|
|
|
for (var c in this.connections) {
|
|
|
|
device.connection = this.connections[c];
|
|
|
|
break;
|
|
|
|
}
|
2014-11-21 00:43:08 +08:00
|
|
|
}
|
|
|
|
|
2015-01-23 06:40:09 +08:00
|
|
|
this.devices[device.name] = initializer("driver", 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) {
|
2015-01-21 11:29:40 +08:00
|
|
|
this.log("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) {
|
2015-01-23 11:25:45 +08:00
|
|
|
this.deprecationWarning("device");
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
if (typeof opts.devices === "object") {
|
|
|
|
if (Array.isArray(opts.devices)) {
|
2015-01-18 13:35:40 +08:00
|
|
|
this.performArraySetup(opts.devices, "device", "devices");
|
2015-01-08 02:16:32 +08:00
|
|
|
return this.devices;
|
2014-11-11 02:52:55 +08:00
|
|
|
}
|
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
for (var key in opts.devices) {
|
|
|
|
var device = opts.devices[key],
|
|
|
|
name = typeof key === "string" ? key : device.name;
|
|
|
|
|
2014-12-18 06:42:34 +08:00
|
|
|
this.device(name, device);
|
2015-02-20 09:23:41 +08:00
|
|
|
}
|
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
|
|
|
|
2014-12-18 06:42:34 +08:00
|
|
|
var start = function() {
|
|
|
|
if (mode === "async") {
|
|
|
|
this.startWork();
|
|
|
|
}
|
|
|
|
}.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-12-18 06:42:34 +08:00
|
|
|
start
|
2014-10-04 15:56:08 +08:00
|
|
|
], function(err, results) {
|
2014-06-10 04:55:44 +08:00
|
|
|
if (!!err) {
|
2015-01-21 11:29:40 +08:00
|
|
|
this.log("fatal", "An error occured while trying to start the robot:");
|
|
|
|
this.log("fatal", err);
|
2014-12-18 06:42:34 +08:00
|
|
|
|
2015-01-21 11:18:52 +08:00
|
|
|
this.halt(function() {
|
|
|
|
if (typeof(this.error) === "function") {
|
|
|
|
this.error.call(this, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.listeners("error").length) {
|
|
|
|
this.emit("error", err);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2014-06-10 04:55:44 +08:00
|
|
|
}
|
2014-12-18 06:42:34 +08:00
|
|
|
|
2015-02-20 09:23:41 +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() {
|
2015-01-21 11:29:40 +08:00
|
|
|
this.log("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) {
|
2015-01-21 11:29:40 +08:00
|
|
|
this.log("info", "Starting connections.");
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
var starters = [];
|
|
|
|
|
|
|
|
var createStarter = function(name) {
|
|
|
|
var conn = this.connections[name];
|
|
|
|
|
2014-12-18 06:42:34 +08:00
|
|
|
this[name] = conn;
|
2014-11-29 06:42:26 +08:00
|
|
|
|
|
|
|
return function(cb) {
|
2014-12-18 06:42:34 +08:00
|
|
|
var str = "Starting connection '" + name + "'";
|
2014-11-29 06:42:26 +08:00
|
|
|
|
|
|
|
if (conn.host) {
|
|
|
|
str += " on host " + conn.host;
|
|
|
|
} else if (conn.port) {
|
|
|
|
str += " on port " + conn.port;
|
|
|
|
}
|
|
|
|
|
2015-01-21 11:29:40 +08:00
|
|
|
this.log("debug", str + ".");
|
2014-11-29 06:42:26 +08:00
|
|
|
return conn.connect.call(conn, cb);
|
2015-01-21 11:29:40 +08:00
|
|
|
}.bind(this);
|
2015-02-20 09:23:41 +08:00
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
for (var name in this.connections) {
|
|
|
|
starters.push(createStarter(name));
|
|
|
|
}
|
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) {
|
2015-01-21 11:29:40 +08:00
|
|
|
var log = this.log;
|
|
|
|
|
|
|
|
log("info", "Starting devices.");
|
2014-05-08 23:57:20 +08:00
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
var starters = [];
|
|
|
|
|
|
|
|
var createStarter = function(name) {
|
|
|
|
var device = this.devices[name];
|
|
|
|
|
2014-12-18 06:42:34 +08:00
|
|
|
this[name] = device;
|
2014-11-29 06:42:26 +08:00
|
|
|
|
|
|
|
return function(cb) {
|
2014-12-18 06:42:34 +08:00
|
|
|
var str = "Starting device '" + name + "'";
|
2014-11-29 06:42:26 +08:00
|
|
|
|
|
|
|
if (device.pin) {
|
|
|
|
str += " on pin " + device.pin;
|
|
|
|
}
|
|
|
|
|
2015-01-21 11:29:40 +08:00
|
|
|
log("debug", str + ".");
|
2014-11-29 06:42:26 +08:00
|
|
|
return device.start.call(device, cb);
|
|
|
|
};
|
2015-02-20 09:23:41 +08:00
|
|
|
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
for (var name in this.devices) {
|
|
|
|
starters.push(createStarter(name));
|
|
|
|
}
|
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() {};
|
|
|
|
|
2015-01-22 00:56:13 +08:00
|
|
|
if (!this.isRunning) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
var devices = [],
|
|
|
|
connections = [],
|
|
|
|
n;
|
|
|
|
|
|
|
|
for (n in this.devices) {
|
|
|
|
var device = this.devices[n];
|
|
|
|
devices.push(device.halt.bind(device));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n in this.connections) {
|
|
|
|
var conn = this.connections[n];
|
|
|
|
connections.push(conn.disconnect.bind(conn));
|
|
|
|
}
|
2014-06-13 06:31:49 +08:00
|
|
|
|
2015-01-22 00:56:13 +08:00
|
|
|
try {
|
|
|
|
Async.parallel(devices, function() {
|
|
|
|
Async.parallel(connections, callback);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
var msg = "An error occured while attempting to safely halt the robot";
|
|
|
|
this.log("error", msg);
|
|
|
|
this.log("error", e.message);
|
|
|
|
}
|
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 + "']";
|
|
|
|
};
|
2015-01-18 13:29:07 +08:00
|
|
|
|
2015-01-21 11:29:40 +08:00
|
|
|
Robot.prototype.log = function(level) {
|
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
args.unshift("[" + this.name + "] -");
|
|
|
|
Logger[level].apply(null, args);
|
|
|
|
};
|
|
|
|
|
2015-01-18 13:35:40 +08:00
|
|
|
Robot.prototype.performArraySetup = function(things, typeOfThing, arrayName) {
|
|
|
|
var str = "Specifying ";
|
|
|
|
str += arrayName;
|
|
|
|
str += " as an array is deprecated. ";
|
|
|
|
str += "It will be removed in 1.0.0.";
|
|
|
|
|
2015-01-21 11:29:40 +08:00
|
|
|
this.log("warn", str);
|
2015-01-18 13:35:40 +08:00
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
things.forEach(function(t, key) {
|
|
|
|
var name = typeof key === "string" ? key : t.name;
|
2015-01-18 13:29:07 +08:00
|
|
|
this[typeOfThing](name, t);
|
|
|
|
}, this);
|
|
|
|
};
|
2015-01-23 11:25:45 +08:00
|
|
|
|
|
|
|
Robot.prototype.deprecationWarning = function(kind) {
|
|
|
|
var msg = "Specifying a single ";
|
|
|
|
msg += kind;
|
|
|
|
msg += " with the '";
|
|
|
|
msg += kind;
|
|
|
|
msg += "' key ";
|
|
|
|
msg += "is deprecated. It will be removed in 1.0.0.";
|
|
|
|
|
|
|
|
this.log("warn", msg);
|
|
|
|
};
|