cylon/dist/connection.js

88 lines
2.6 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';
2013-11-05 04:13:40 +08:00
var EventEmitter, Port, namespace,
__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");
2013-11-05 04:13:40 +08:00
namespace = require('node-namespace');
2013-10-25 05:25:42 +08:00
Port = require("./port");
EventEmitter = require('events').EventEmitter;
2013-11-05 04:13:40 +08:00
namespace('Cylon', function() {
return this.Connection = (function(_super) {
__extends(Connection, _super);
2013-10-25 05:25:42 +08:00
2013-11-05 04:13:40 +08:00
function Connection(opts) {
if (opts == null) {
opts = {};
}
this.connect = __bind(this.connect, this);
if (opts.id == null) {
opts.id = Math.floor(Math.random() * 10000);
}
this.self = this;
this.robot = opts.robot;
this.name = opts.name;
this.connection_id = opts.id;
this.adaptor = this.requireAdaptor(opts.adaptor);
this.port = new Port(opts.port);
proxyFunctionsToObject(this.adaptor.commands(), this.adaptor, this.self);
}
2013-11-05 04:13:40 +08:00
Connection.prototype.data = function() {
return {
name: this.name,
port: this.port.toString(),
adaptor: this.adaptor.constructor.name || this.adaptor.name,
connection_id: this.connection_id
};
};
2013-11-05 04:13:40 +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);
};
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();
};
Connection.prototype.requireAdaptor = function(adaptorName) {
Logger.debug("Loading adaptor '" + adaptorName + "'");
return this.robot.requireAdaptor(adaptorName, this.self);
};
2013-10-25 05:25:42 +08:00
2013-11-05 04:13:40 +08:00
return Connection;
2013-10-25 05:25:42 +08:00
2013-11-05 04:13:40 +08:00
})(EventEmitter);
});
2013-10-25 05:25:42 +08:00
2013-11-05 04:13:40 +08:00
module.exports = Cylon.Connection;
2013-10-25 05:25:42 +08:00
}).call(this);