2013-10-25 05:25:42 +08:00
|
|
|
/*
|
|
|
|
* connection
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
2014-02-28 02:33:43 +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 02:33:43 +08:00
|
|
|
'use strict';
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-05-07 23:38:06 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2014-02-28 06:42:33 +08:00
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
var Logger = require('./logger'),
|
|
|
|
Utils = require('./utils');
|
2014-05-14 10:44:40 +08:00
|
|
|
|
2014-05-07 23:38:06 +08:00
|
|
|
// Public: Creates a new Connection
|
|
|
|
//
|
|
|
|
// opts - hash of acceptable params:
|
|
|
|
// robot - Robot the Connection belongs to
|
|
|
|
// name - name for the connection
|
|
|
|
// adaptor - string module name of the adaptor to be set up
|
|
|
|
// port - string port to use for the Connection
|
|
|
|
//
|
|
|
|
// Returns the newly set-up connection
|
2014-07-13 02:00:22 +08:00
|
|
|
var Connection = module.exports = function Connection(opts) {
|
|
|
|
opts = opts || {};
|
2014-05-07 23:38:06 +08:00
|
|
|
|
2014-06-18 10:35:07 +08:00
|
|
|
this.connect = this.connect.bind(this);
|
2014-05-07 23:38:06 +08:00
|
|
|
|
|
|
|
this.robot = opts.robot;
|
|
|
|
this.name = opts.name;
|
|
|
|
this.port = opts.port;
|
|
|
|
this.adaptor = this.initAdaptor(opts);
|
|
|
|
|
2014-07-26 06:55:16 +08:00
|
|
|
this.details = {};
|
|
|
|
|
|
|
|
for (var opt in opts) {
|
|
|
|
if (['robot', 'name', 'adaptor'].indexOf(opt) < 0) {
|
|
|
|
this.details[opt] = opts[opt];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 06:25:51 +08:00
|
|
|
for (var opt in this.adaptor) {
|
|
|
|
if (!this[opt] && typeof this.adaptor[opt] === 'function') {
|
|
|
|
this[opt] = this.adaptor[opt].bind(this.adaptor);
|
|
|
|
}
|
|
|
|
}
|
2014-06-17 04:09:13 +08:00
|
|
|
};
|
2014-05-07 23:38:06 +08:00
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils.subclass(Connection, EventEmitter);
|
2014-05-07 23:38:06 +08:00
|
|
|
|
2014-06-07 05:11:35 +08:00
|
|
|
// Public: Expresses the Connection in JSON format
|
2014-05-07 23:38:06 +08:00
|
|
|
//
|
|
|
|
// Returns an Object containing Connection data
|
2014-06-07 05:11:35 +08:00
|
|
|
Connection.prototype.toJSON = function() {
|
2014-05-07 23:38:06 +08:00
|
|
|
return {
|
|
|
|
name: this.name,
|
|
|
|
adaptor: this.adaptor.constructor.name || this.adaptor.name,
|
2014-07-26 06:55:16 +08:00
|
|
|
details: this.details
|
2014-05-07 23:38:06 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Connect the adaptor's connection
|
|
|
|
//
|
|
|
|
// callback - callback function to run when the adaptor is connected
|
|
|
|
//
|
2014-09-09 00:27:18 +08:00
|
|
|
// Returns nothing
|
2014-05-07 23:38:06 +08:00
|
|
|
Connection.prototype.connect = function(callback) {
|
2014-06-18 10:18:12 +08:00
|
|
|
var msg = this._logstring("Connecting to");
|
2014-05-07 23:38:06 +08:00
|
|
|
Logger.info(msg);
|
2014-09-09 00:27:18 +08:00
|
|
|
this.adaptor.connect(callback);
|
2014-05-07 23:38:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Disconnect the adaptor's connection
|
|
|
|
//
|
2014-06-13 06:31:49 +08:00
|
|
|
// callback - function to be triggered then the adaptor has disconnected
|
|
|
|
//
|
2014-05-07 23:38:06 +08:00
|
|
|
// Returns nothing
|
2014-06-13 06:31:49 +08:00
|
|
|
Connection.prototype.disconnect = function(callback) {
|
2014-06-18 10:18:12 +08:00
|
|
|
var msg = this._logstring("Disconnecting from");
|
2014-05-07 23:38:06 +08:00
|
|
|
Logger.info(msg);
|
2014-09-05 23:43:40 +08:00
|
|
|
this.removeAllListeners();
|
2014-09-09 00:27:18 +08:00
|
|
|
this.adaptor.disconnect(callback);
|
2014-05-07 23:38:06 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Public: sets up adaptor with @robot
|
|
|
|
//
|
|
|
|
// opts - options for adaptor being initialized
|
|
|
|
// adaptor - name of the adaptor
|
|
|
|
//
|
|
|
|
// Returns the set-up adaptor
|
|
|
|
Connection.prototype.initAdaptor = function(opts) {
|
2014-06-11 02:33:52 +08:00
|
|
|
Logger.debug("Loading adaptor '" + opts.adaptor + "'.");
|
2014-07-04 00:35:24 +08:00
|
|
|
return this.robot.initAdaptor(opts.adaptor, this, opts);
|
2014-05-07 23:38:06 +08:00
|
|
|
};
|
|
|
|
|
2014-06-18 10:18:12 +08:00
|
|
|
Connection.prototype._logstring = function _logstring(action) {
|
|
|
|
var msg = action + " '" + this.name + "'";
|
|
|
|
|
|
|
|
if (this.port != null) {
|
|
|
|
msg += " on port " + this.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg += ".";
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
};
|