2014-02-28 01:38:38 +08:00
|
|
|
"use strict";
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
var Basestar = require("./basestar"),
|
2015-02-21 10:49:31 +08:00
|
|
|
Utils = require("./utils"),
|
|
|
|
_ = require("./utils/helpers");
|
2014-04-25 06:31:47 +08:00
|
|
|
|
2015-03-20 08:33:31 +08:00
|
|
|
function formatErrorMessage(name, message) {
|
|
|
|
return ["Error in connection", "'" + name + "'", "- " + message].join(" ");
|
|
|
|
}
|
|
|
|
|
2015-06-30 01:21:25 +08:00
|
|
|
/**
|
|
|
|
* Adaptor class
|
|
|
|
*
|
|
|
|
* @constructor Adaptor
|
|
|
|
*
|
|
|
|
* @param {Object} [opts] adaptor options
|
|
|
|
* @param {String} [opts.name] the adaptor's name
|
|
|
|
* @param {Object} [opts.robot] the robot the adaptor belongs to
|
|
|
|
* @param {Object} [opts.host] the host the adaptor will connect to
|
|
|
|
* @param {Object} [opts.port] the port the adaptor will connect to
|
|
|
|
*/
|
2014-07-11 00:51:23 +08:00
|
|
|
var Adaptor = module.exports = function Adaptor(opts) {
|
2015-05-13 09:47:27 +08:00
|
|
|
Adaptor.__super__.constructor.apply(this, arguments);
|
|
|
|
|
2014-07-11 00:51:23 +08:00
|
|
|
opts = opts || {};
|
2014-05-07 09:18:20 +08:00
|
|
|
|
|
|
|
this.name = opts.name;
|
2014-11-11 07:56:25 +08:00
|
|
|
|
|
|
|
// the Robot the adaptor belongs to
|
|
|
|
this.robot = opts.robot;
|
|
|
|
|
|
|
|
// some default options
|
|
|
|
this.host = opts.host;
|
|
|
|
this.port = opts.port;
|
|
|
|
|
|
|
|
// misc. details provided in args hash
|
|
|
|
this.details = {};
|
|
|
|
|
2015-02-21 10:49:31 +08:00
|
|
|
_.each(opts, function(opt, name) {
|
2015-03-12 03:31:45 +08:00
|
|
|
if (!_.includes(["robot", "name", "adaptor", "events"], name)) {
|
2015-02-20 09:23:41 +08:00
|
|
|
this.details[name] = opt;
|
|
|
|
}
|
2015-02-21 10:49:31 +08:00
|
|
|
}, this);
|
2014-05-07 09:18:20 +08:00
|
|
|
};
|
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils.subclass(Adaptor, Basestar);
|
2014-11-11 07:56:25 +08:00
|
|
|
|
2015-06-30 01:21:25 +08:00
|
|
|
/**
|
|
|
|
* A base connect function. Must be overwritten by a descendent.
|
|
|
|
*
|
|
|
|
* @throws Error if not overridden by a child class
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2015-03-20 08:33:31 +08:00
|
|
|
Adaptor.prototype.connect = function() {
|
|
|
|
var message = formatErrorMessage(
|
|
|
|
this.name,
|
|
|
|
"Adaptor#connect method must be overwritten by descendant classes."
|
|
|
|
);
|
|
|
|
|
|
|
|
throw new Error(message);
|
|
|
|
};
|
|
|
|
|
2015-06-30 01:21:25 +08:00
|
|
|
/**
|
|
|
|
* A base disconnect function. Must be overwritten by a descendent.
|
|
|
|
*
|
|
|
|
* @throws Error if not overridden by a child class
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2015-03-20 08:33:31 +08:00
|
|
|
Adaptor.prototype.disconnect = function() {
|
|
|
|
var message = formatErrorMessage(
|
|
|
|
this.name,
|
|
|
|
"Adaptor#disconnect method must be overwritten by descendant classes."
|
|
|
|
);
|
|
|
|
|
|
|
|
throw new Error(message);
|
|
|
|
};
|
|
|
|
|
2015-06-30 01:21:25 +08:00
|
|
|
/**
|
|
|
|
* Expresses the Adaptor in a JSON-serializable format
|
|
|
|
*
|
|
|
|
* @return {Object} a representation of the Adaptor in a serializable format
|
|
|
|
*/
|
2014-11-11 07:56:25 +08:00
|
|
|
Adaptor.prototype.toJSON = function() {
|
|
|
|
return {
|
|
|
|
name: this.name,
|
|
|
|
adaptor: this.constructor.name || this.name,
|
|
|
|
details: this.details
|
|
|
|
};
|
|
|
|
};
|