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');
|
2014-04-25 03:43:00 +08:00
|
|
|
|
2014-02-28 02:58:50 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2014-04-25 03:43:00 +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-04-25 06:31:47 +08:00
|
|
|
var Basestar;
|
|
|
|
|
|
|
|
module.exports = Basestar = function Basestar(opts) {
|
2014-04-25 03:43:00 +08:00
|
|
|
this.self = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
subclass(Basestar, EventEmitter);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
Basestar.prototype.proxyMethods = function(methods, target, source, force) {
|
|
|
|
if (force == null) { force = false; }
|
|
|
|
return proxyFunctionsToObject(methods, target, source, force);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// - sendUpdate - whether or not to send an 'update' event
|
|
|
|
//
|
|
|
|
// Returns the source
|
|
|
|
Basestar.prototype.defineEvent = function(opts) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
opts.sendUpdate = opts.sendUpdate || false;
|
|
|
|
opts.targetEventName = opts.targetEventName || opts.eventName;
|
|
|
|
|
|
|
|
opts.source.on(opts.eventName, function() {
|
|
|
|
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);
|
2014-02-28 02:58:50 +08:00
|
|
|
}
|
2014-04-25 03:43:00 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return opts.source;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
//
|
|
|
|
// Returns this.connector
|
|
|
|
Basestar.prototype.defineAdaptorEvent = function(opts) {
|
|
|
|
if (typeof opts === 'string') { opts = { eventName: opts }; }
|
|
|
|
|
|
|
|
opts['source'] = this.connector;
|
|
|
|
opts['target'] = this.connection;
|
|
|
|
|
|
|
|
return this.defineEvent(opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
//
|
|
|
|
// Returns this.connection
|
|
|
|
Basestar.prototype.defineDriverEvent = function(opts) {
|
|
|
|
if (typeof opts === 'string') { opts = { eventName: opts }; }
|
|
|
|
|
|
|
|
opts['source'] = this.connection;
|
|
|
|
opts['target'] = this.device;
|
|
|
|
|
|
|
|
return this.defineEvent(opts);
|
|
|
|
};
|