2013-10-25 05:25:42 +08:00
|
|
|
/*
|
|
|
|
* adaptor
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
2014-02-28 01:38:38 +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 01:38:38 +08:00
|
|
|
"use strict";
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-05-14 10:44:40 +08:00
|
|
|
var Basestar = require('./basestar'),
|
2014-06-06 03:11:37 +08:00
|
|
|
Logger = require('./logger'),
|
|
|
|
Utils = require('./utils');
|
2014-04-25 06:31:47 +08:00
|
|
|
|
2014-02-28 03:45:09 +08:00
|
|
|
// The Adaptor class is a base class for Adaptor classes in external Cylon
|
|
|
|
// modules to use. It offers basic functions for connecting/disconnecting that
|
|
|
|
// descendant classes can use.
|
2014-05-07 09:18:20 +08:00
|
|
|
var Adaptor;
|
|
|
|
|
|
|
|
// Public: Creates a new Adaptor
|
|
|
|
//
|
|
|
|
// opts - hash of acceptable params
|
|
|
|
// name - name of the Adaptor, used when printing to console
|
|
|
|
// connection - Connection the adaptor will use to proxy commands/events
|
|
|
|
//
|
|
|
|
// Returns a new Adaptor
|
|
|
|
module.exports = Adaptor = function Adaptor(opts) {
|
|
|
|
if (opts == null) {
|
|
|
|
opts = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.self = this;
|
|
|
|
this.name = opts.name;
|
|
|
|
this.connection = opts.connection;
|
|
|
|
};
|
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils.subclass(Adaptor, Basestar);
|
2014-05-07 09:18:20 +08:00
|
|
|
|
2014-06-11 05:18:35 +08:00
|
|
|
Adaptor.prototype.commands = [];
|
|
|
|
|
2014-06-11 00:20:23 +08:00
|
|
|
// Public: Connects to the adaptor, and triggers the provided callback when
|
|
|
|
// done.
|
2014-05-07 09:18:20 +08:00
|
|
|
//
|
|
|
|
// callback - function to run when the adaptor is connected
|
|
|
|
//
|
|
|
|
// Returns nothing
|
|
|
|
Adaptor.prototype.connect = function(callback) {
|
2014-06-11 02:33:52 +08:00
|
|
|
Logger.info("Connecting to adaptor '" + this.name + "'.");
|
2014-05-07 09:18:20 +08:00
|
|
|
callback(null);
|
2014-06-11 02:33:52 +08:00
|
|
|
return true;
|
2014-05-07 09:18:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Disconnects from the adaptor
|
|
|
|
//
|
2014-06-13 06:31:49 +08:00
|
|
|
// callback - function to run when the adaptor is disconnected
|
|
|
|
//
|
2014-05-07 09:18:20 +08:00
|
|
|
// Returns nothing
|
2014-06-13 06:31:49 +08:00
|
|
|
Adaptor.prototype.disconnect = function(callback) {
|
2014-06-11 02:33:52 +08:00
|
|
|
Logger.info("Disconnecting from adaptor '" + this.name + "'.");
|
2014-06-13 06:31:49 +08:00
|
|
|
this.removeAllListeners();
|
|
|
|
callback();
|
2014-05-07 09:18:20 +08:00
|
|
|
};
|
2014-07-03 08:42:39 +08:00
|
|
|
|
|
|
|
// Public: Voids all command functions so they do not interact
|
|
|
|
// with anything after disconnect has been called.
|
|
|
|
//
|
|
|
|
// Returns nothing
|
|
|
|
Adaptor.prototype.noop = function() {
|
|
|
|
var fnVoid = (function(command) {
|
|
|
|
this[command] = function() {return null };
|
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
this.commands.forEach(fnVoid);
|
|
|
|
};
|