cylon/dist/connection.js

73 lines
2.0 KiB
JavaScript
Raw Normal View History

2013-10-25 05:25:42 +08:00
/*
* connection
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(function() {
'use strict';
var Connection, EventEmitter, Port,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
2013-10-25 05:25:42 +08:00
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
2013-10-25 05:25:42 +08:00
require("./robot");
Port = require("./port");
EventEmitter = require('events').EventEmitter;
module.exports = Connection = (function(_super) {
2013-10-29 08:48:29 +08:00
var klass;
2013-10-25 05:25:42 +08:00
__extends(Connection, _super);
2013-10-29 08:48:29 +08:00
klass = Connection;
2013-10-25 05:25:42 +08:00
function Connection(opts) {
if (opts == null) {
opts = {};
}
this.connect = __bind(this.connect, this);
2013-10-25 05:25:42 +08:00
this.self = this;
this.robot = opts.robot;
this.name = opts.name;
this.adaptor = this.requireAdaptor(opts.adaptor);
this.port = new Port(opts.port);
2013-10-29 08:48:29 +08:00
proxyFunctionsToObject(this.adaptor.commands(), this.adaptor, klass);
2013-10-25 05:25:42 +08:00
}
Connection.prototype.connect = function(callback) {
var msg;
msg = "Connecting to '" + this.name + "'";
if (this.port != null) {
msg += " on port '" + (this.port.toString()) + "'";
}
Logger.info(msg);
return this.adaptor.connect(callback);
2013-10-25 05:25:42 +08:00
};
Connection.prototype.disconnect = function() {
var msg;
msg = "Disconnecting from '" + this.name + "'";
if (this.port != null) {
msg += " on port '" + (this.port.toString()) + "'";
}
Logger.info(msg);
return this.adaptor.disconnect();
2013-10-25 05:25:42 +08:00
};
Connection.prototype.requireAdaptor = function(adaptorName) {
2013-10-26 15:04:42 +08:00
Logger.debug("Loading adaptor '" + adaptorName + "'");
2013-10-25 05:25:42 +08:00
return this.robot.requireAdaptor(adaptorName, this.self);
};
return Connection;
})(EventEmitter);
}).call(this);