From ed76890b8a00b6a2a8371b1e576e2e2ae35e48bb Mon Sep 17 00:00:00 2001 From: deadprogram Date: Mon, 7 Sep 2015 10:51:34 -0700 Subject: [PATCH] Making connections dynamic as well --- lib/robot.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index ffcf0ff..d870872 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -283,26 +283,42 @@ Robot.prototype.startWork = function() { Robot.prototype.startConnections = function(callback) { this.log("Starting connections."); - var starters = _.map(this.connections, function(conn, name) { - this[name] = conn; - + var starters = _.map(this.connections, function(conn) { return function(cb) { - var str = "Starting connection '" + name + "'"; - - if (conn.host) { - str += " on host " + conn.host; - } else if (conn.port) { - str += " on port " + conn.port; - } - - this.log(str + "."); - return conn.connect.call(conn, cb); + return this.startConnection(conn, cb); }.bind(this); }, this); return _.parallel(starters, callback); }; +/** + * Starts a single connection on Robot + * + * @param {Object} connection to start + * @param {Function} callback function to be triggered after the connection is + * started + * @return {void} + */ +Robot.prototype.startConnection = function(connection, callback) { + if (connection.connected === true) { + return callback.call(connection); + } + + var str = "Starting connection '" + connection.name + "'"; + + if (connection.host) { + str += " on host " + connection.host; + } else if (connection.port) { + str += " on port " + connection.port; + } + + this.log(str + "."); + connection.connect.call(connection, callback); + connection.connected = true; + return true; +}; + /** * Starts the Robot's devices *