2013-10-30 01:43:11 +08:00
|
|
|
/*
|
|
|
|
* basestar
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
2014-02-28 02:58:50 +08:00
|
|
|
* Copyright (c) 2013-2014 The Hybrid Group
|
2013-10-30 01:43:11 +08:00
|
|
|
* Licensed under the Apache 2.0 license.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
"use strict";
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
require('./utils');
|
|
|
|
var namespace = require('node-namespace');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Basestar is a base class to be used when writing external Cylon adaptors and
|
|
|
|
// drivers. It provides some useful base methods and functionality
|
|
|
|
//
|
|
|
|
// It also extends EventEmitter, so child classes are capable of emitting events
|
|
|
|
// for other parts of the system to handle.
|
2014-02-28 02:58:50 +08:00
|
|
|
namespace("Cylon", function() {
|
|
|
|
this.Basestar = (function(klass) {
|
|
|
|
subclass(Basestar, klass);
|
2013-11-05 03:41:51 +08:00
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
function Basestar(opts) {
|
|
|
|
this.self = this;
|
|
|
|
}
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Proxies calls from all methods in the object to a target object
|
|
|
|
//
|
|
|
|
// methods - array of methods to proxy
|
|
|
|
// target - object to proxy methods to
|
|
|
|
// source - object to proxy methods from
|
|
|
|
// force - whether or not to overwrite existing method definitions
|
|
|
|
//
|
|
|
|
// Returns the klass where the methods have been proxied
|
2014-02-28 02:58:50 +08:00
|
|
|
Basestar.prototype.proxyMethods = function(methods, target, source, force) {
|
2014-02-28 13:35:12 +08:00
|
|
|
if (force == null) { force = false; }
|
2014-02-28 02:58:50 +08:00
|
|
|
return proxyFunctionsToObject(methods, target, source, force);
|
|
|
|
};
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Defines an event handler that proxies events from a source object
|
|
|
|
// to a target object
|
|
|
|
//
|
|
|
|
// opts - object containing options:
|
|
|
|
// - targetEventName or eventName - event that should be emitted from the
|
|
|
|
// target
|
|
|
|
// - target - object to proxy event to
|
|
|
|
// - source - object to proxy event from
|
2014-02-28 10:13:21 +08:00
|
|
|
// - sendUpdate - whether or not to send an 'update' event
|
2014-02-28 06:42:33 +08:00
|
|
|
//
|
|
|
|
// Returns the source
|
2014-02-28 02:58:50 +08:00
|
|
|
Basestar.prototype.defineEvent = function(opts) {
|
2014-02-28 10:21:30 +08:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
opts.sendUpdate = opts.sendUpdate || false;
|
|
|
|
opts.targetEventName = opts.targetEventName || opts.eventName;
|
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
opts.source.on(opts.eventName, function() {
|
2014-02-28 10:21:30 +08:00
|
|
|
var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : [];
|
|
|
|
args.unshift(opts.targetEventName);
|
|
|
|
opts.target.emit.apply(opts.target, args);
|
|
|
|
|
|
|
|
if (opts.sendUpdate) {
|
|
|
|
args.unshift('update');
|
|
|
|
opts.target.emit.apply(opts.target, args);
|
2013-11-01 06:15:23 +08:00
|
|
|
}
|
2014-02-28 13:35:12 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return opts.source;
|
2014-02-28 02:58:50 +08:00
|
|
|
};
|
2013-11-01 06:15:23 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Creates an event handler that proxies events from an adaptor's
|
|
|
|
// 'connector' (reference to whatever module is actually talking to the hw)
|
|
|
|
// to the adaptor's associated connection.
|
|
|
|
//
|
|
|
|
// opts - hash of opts to be passed to defineEvent()
|
|
|
|
//
|
2014-02-28 13:35:12 +08:00
|
|
|
// Returns this.connector
|
2014-02-28 02:58:50 +08:00
|
|
|
Basestar.prototype.defineAdaptorEvent = function(opts) {
|
2014-03-08 15:33:59 +08:00
|
|
|
if (typeof opts === 'string') { opts = { eventName: opts }; }
|
2014-02-28 13:17:44 +08:00
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
opts['source'] = this.connector;
|
|
|
|
opts['target'] = this.connection;
|
2014-02-28 13:17:44 +08:00
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
return this.defineEvent(opts);
|
|
|
|
};
|
2013-11-01 06:15:23 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Creates an event handler that proxies events from an device's
|
|
|
|
// 'connector' (reference to whatever module is actually talking to the hw)
|
|
|
|
// to the device's associated connection.
|
|
|
|
//
|
|
|
|
// opts - hash of opts to be passed to defineEvent()
|
|
|
|
//
|
2014-02-28 13:35:12 +08:00
|
|
|
// Returns this.connection
|
2014-02-28 02:58:50 +08:00
|
|
|
Basestar.prototype.defineDriverEvent = function(opts) {
|
2014-03-08 15:33:59 +08:00
|
|
|
if (typeof opts === 'string') { opts = { eventName: opts }; }
|
2014-02-28 13:17:44 +08:00
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
opts['source'] = this.connection;
|
|
|
|
opts['target'] = this.device;
|
2014-02-28 13:17:44 +08:00
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
return this.defineEvent(opts);
|
|
|
|
};
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
return Basestar;
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
})(EventEmitter);
|
|
|
|
});
|