cylon/lib/robot.js

424 lines
9.8 KiB
JavaScript
Raw Normal View History

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
2014-12-16 03:15:29 +08:00
var initConnection = require("./connection"),
initDevice = require("./device"),
Logger = require("./logger"),
Utils = require("./utils"),
2014-12-18 06:42:34 +08:00
Config = require("./config"),
_ = require("./lodash");
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-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",
"initRobot",
2014-05-08 23:57:20 +08:00
"initDevices",
"initConnections"
];
2014-12-18 06:42:34 +08:00
_.bindAll(this, methods);
2014-05-08 23:57:20 +08:00
this.initRobot(opts);
this.initConnections(opts);
this.initDevices(opts);
2014-05-08 23:57:20 +08:00
2014-12-18 06:42:34 +08:00
_.forEach(opts, function(opt, name) {
if (this[name] !== undefined) {
return;
}
2014-08-06 09:41:57 +08:00
2014-12-18 06:42:34 +08:00
this[name] = opt;
2014-12-18 06:42:34 +08:00
if (opts.commands == null && _.isFunction(opt)) {
this.commands[name] = opt;
2014-08-06 09:41:57 +08:00
}
2014-12-18 06:42:34 +08:00
}, this);
2014-08-06 09:41:57 +08:00
if (opts.commands) {
var cmds = _.result(opts, "commands");
if (_.isObject(cmds) && !_.isArray(cmds)) {
this.commands = cmds;
} else {
var err = "#commands must be an object ";
err += "or a function that returns an object";
throw new Error(err);
}
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
};
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));
};
// Public: Expresses the Robot in a JSON-serializable format
2014-05-08 23:57:20 +08:00
//
// Returns an Object containing Robot data
Robot.prototype.toJSON = function() {
2014-05-08 23:57:20 +08:00
return {
name: this.name,
2014-12-18 06:42:34 +08:00
connections: _.invoke(this.connections, "toJSON"),
devices: _.invoke(this.devices, "toJSON"),
commands: _.keys(this.commands),
events: _.isArray(this.events) ? this.events : []
2014-05-08 23:57:20 +08:00
};
};
Robot.prototype.connection = function(name, conn) {
conn.robot = this;
conn.name = name;
if (this.connections[conn.name]) {
2014-12-16 03:15:29 +08:00
var original = conn.name,
str;
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-12-16 03:15:29 +08:00
this.connections[conn.name] = initConnection(conn);
return this;
};
// 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) {
this.work = function() { Logger.debug("No work yet."); };
}
};
2014-05-08 23:57:20 +08:00
// Public: Initializes all connections for the robot
//
// opts - options array passed to constructor
2014-05-08 23:57:20 +08:00
//
// Returns initialized connections
Robot.prototype.initConnections = function(opts) {
2014-12-16 03:15:29 +08:00
var str;
2014-12-18 06:42:34 +08:00
Logger.info("Initializing connections.");
2014-05-08 23:57:20 +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);
this.connection(opts.connection.name, opts.connection);
return this.connections;
}
2014-12-18 06:42:34 +08:00
if (_.isObject(opts.connections)) {
if (_.isArray(opts.connections)) {
this.performArraySetup(opts.connections, "connection", "connections");
return this.connections;
2014-12-18 06:42:34 +08:00
}
2014-12-16 03:15:29 +08:00
_.forIn(opts.connections, function(conn, key) {
2014-12-18 06:42:34 +08:00
var name = _.isString(key) ? key : conn.name;
if (conn.devices) {
_.forIn(conn.devices, function(device, deviceName) {
opts.devices = opts.devices || {};
device.connection = name;
opts.devices[deviceName] = device;
});
delete conn.devices;
}
2014-12-18 06:42:34 +08:00
this.connection(name, conn);
2014-12-01 07:17:57 +08:00
}, this);
}
2014-05-08 23:57:20 +08:00
return this.connections;
};
Robot.prototype.device = function(name, device) {
2014-12-16 03:15:29 +08:00
var str;
device.robot = this;
device.name = name;
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-12-16 03:15:29 +08:00
if (typeof device.connection === "string") {
if (this.connections[device.connection] == null) {
2014-12-16 03:15:29 +08:00
str = "No connection found with the name " + device.connection + ".\n";
Logger.fatal(str);
2014-12-16 03:15:29 +08:00
process.emit("SIGINT");
}
2014-12-18 06:42:34 +08:00
device.connection = this.connections[device.connection];
} else {
2014-12-18 06:42:34 +08:00
device.connection = _.first(_.values(this.connections));
}
2014-12-16 03:15:29 +08:00
this.devices[device.name] = initDevice(device);
return this;
2014-12-16 03:15:29 +08:00
};
2014-05-08 23:57:20 +08:00
// Public: Initializes all devices for the robot
//
// opts - options array passed to constructor
2014-05-08 23:57:20 +08:00
//
// Returns initialized devices
Robot.prototype.initDevices = function(opts) {
2014-12-18 06:42:34 +08:00
var str;
2014-12-16 03:15:29 +08:00
Logger.info("Initializing devices.");
2014-05-08 23:57:20 +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");
}
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);
this.device(opts.device.name, opts.device);
return this.devices;
}
2014-12-18 06:42:34 +08:00
if (_.isObject(opts.devices)) {
if (_.isArray(opts.devices)) {
this.performArraySetup(opts.devices, "device", "devices");
return this.devices;
}
_.forIn(opts.devices, function(device, key) {
2014-12-18 06:42:34 +08:00
var name = _.isString(key) ? key : device.name;
this.device(name, device);
2014-12-01 07:17:57 +08:00
}, this);
}
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) {
if (this.running) {
2014-10-01 03:22:00 +08:00
return this;
}
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([
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) {
Logger.fatal("An error occured while trying to start the robot:");
Logger.fatal(err);
2014-12-18 06:42:34 +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
if (_.isFunction(callback)) {
2014-10-04 15:56:08 +08:00
callback(err, results);
}
2014-10-01 07:13:20 +08:00
}.bind(this));
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-12-18 06:42:34 +08:00
var starters = _.map(this.connections, function(conn, name) {
this[name] = conn;
return function(cb) {
2014-12-18 06:42:34 +08:00
var str = "Starting connection '" + name + "'";
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-12-18 06:42:34 +08:00
var starters = _.map(this.devices, function(device, name) {
this[name] = device;
return function(cb) {
2014-12-18 06:42:34 +08:00
var str = "Starting device '" + name + "'";
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.
//
// callback - callback to be triggered when the Robot is stopped
//
2014-05-08 23:57:20 +08:00
// Returns nothing
Robot.prototype.halt = function(callback) {
callback = callback || function() {};
2014-12-18 06:42:34 +08:00
var devices = _.map(this.devices, "halt");
var connections = _.map(this.connections, "disconnect");
2014-12-18 06:42:34 +08:00
Async.parallel(devices, function() {
Async.parallel(connections, callback);
});
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
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.";
Logger.warn(str);
2015-01-18 13:29:07 +08:00
_.forEach(things, function(t, key) {
var name = _.isString(key) ? key : t.name;
this[typeOfThing](name, t);
}, this);
};