Refactor connection & device to pure JS style

This commit is contained in:
deadprogram 2014-02-27 10:33:43 -08:00
parent 04a32e522f
commit 30228c7f3c
2 changed files with 131 additions and 152 deletions

View File

@ -2,88 +2,77 @@
* connection
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
'use strict';
(function() {
'use strict';
var EventEmitter, namespace,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__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; };
require("./robot");
require("./port");
require("./adaptor");
var namespace = require('node-namespace');
var EventEmitter = require('events').EventEmitter;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
require("./robot");
namespace("Cylon", function() {
this.Connection = (function(klass) {
subclass(Connection, klass);
require("./port");
require("./adaptor");
namespace = require('node-namespace');
EventEmitter = require('events').EventEmitter;
namespace('Cylon', function() {
return this.Connection = (function(_super) {
__extends(Connection, _super);
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.initAdaptor(opts);
this.port = new Cylon.Port(opts.port);
proxyFunctionsToObject(this.adaptor.commands(), this.adaptor, this.self);
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.initAdaptor(opts);
this.port = new Cylon.Port(opts.port);
proxyFunctionsToObject(this.adaptor.commands(), this.adaptor, this.self);
}
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
};
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
};
};
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.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.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.initAdaptor = function(opts) {
Logger.debug("Loading adaptor '" + opts.adaptor + "'");
return this.robot.initAdaptor(opts.adaptor, this.self, opts);
};
Connection.prototype.initAdaptor = function(opts) {
Logger.debug("Loading adaptor '" + opts.adaptor + "'");
return this.robot.initAdaptor(opts.adaptor, this.self, opts);
};
return Connection;
return Connection;
})(EventEmitter);
});
})(EventEmitter);
});
module.exports = Cylon.Connection;
}).call(this);
module.exports = Cylon.Connection;

View File

@ -2,100 +2,90 @@
* device
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
'use strict';
(function() {
'use strict';
var EventEmitter, namespace,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__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; };
require('./cylon');
require('./driver');
var namespace = require('node-namespace');
var EventEmitter = require('events').EventEmitter;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
require('./cylon');
namespace("Cylon", function() {
this.Device = (function(klass) {
subclass(Device, klass);
require('./driver');
namespace = require('node-namespace');
EventEmitter = require('events').EventEmitter;
namespace('Cylon', function() {
return this.Device = (function(_super) {
__extends(Device, _super);
function Device(opts) {
if (opts == null) {
opts = {};
}
this.stop = __bind(this.stop, this);
this.start = __bind(this.start, this);
this.self = this;
this.robot = opts.robot;
this.name = opts.name;
this.pin = opts.pin;
this.connection = this.determineConnection(opts.connection) || this.defaultConnection();
this.driver = this.initDriver(opts);
proxyFunctionsToObject(this.driver.commands(), this.driver, this.self);
function Device(opts) {
if (opts == null) {
opts = {};
}
this.stop = __bind(this.stop, this);
this.start = __bind(this.start, this);
this.self = this;
this.robot = opts.robot;
this.name = opts.name;
this.pin = opts.pin;
this.connection = this.determineConnection(opts.connection) || this.defaultConnection();
this.driver = this.initDriver(opts);
proxyFunctionsToObject(this.driver.commands(), this.driver, this.self);
}
Device.prototype.start = function(callback) {
var msg;
msg = "Starting device '" + this.name + "'";
if (this.pin != null) {
msg += " on pin " + this.pin;
}
Logger.info(msg);
return this.driver.start(callback);
Device.prototype.start = function(callback) {
var msg;
msg = "Starting device '" + this.name + "'";
if (this.pin != null) {
msg += " on pin " + this.pin;
}
Logger.info(msg);
return this.driver.start(callback);
};
Device.prototype.stop = function() {
Logger.info("Stopping device '" + this.name + "'");
return this.driver.stop();
};
Device.prototype.data = function() {
return {
name: this.name,
driver: this.driver.constructor.name || this.driver.name,
pin: this.pin != null ? this.pin.toString : null,
connection: this.connection.data(),
commands: this.driver.commands()
};
};
Device.prototype.stop = function() {
Logger.info("Stopping device '" + this.name + "'");
return this.driver.stop();
};
Device.prototype.determineConnection = function(c) {
if (c) {
return this.robot.connections[c];
}
};
Device.prototype.data = function() {
return {
name: this.name,
driver: this.driver.constructor.name || this.driver.name,
pin: this.pin != null ? this.pin.toString : null,
connection: this.connection.data(),
commands: this.driver.commands()
};
};
Device.prototype.defaultConnection = function() {
var first, k, v, _ref;
first = 0;
_ref = this.robot.connections;
for (k in _ref) {
v = _ref[k];
first || (first = v);
}
return first;
};
Device.prototype.determineConnection = function(c) {
if (c) {
return this.robot.connections[c];
}
};
Device.prototype.initDriver = function(opts) {
if (opts == null) {
opts = {};
}
Logger.debug("Loading driver '" + opts.driver + "'");
return this.robot.initDriver(opts.driver, this.self, opts);
};
Device.prototype.defaultConnection = function() {
var first, k, v, _ref;
first = 0;
_ref = this.robot.connections;
for (k in _ref) {
v = _ref[k];
first || (first = v);
}
return first;
};
return Device;
Device.prototype.initDriver = function(opts) {
if (opts == null) {
opts = {};
}
Logger.debug("Loading driver '" + opts.driver + "'");
return this.robot.initDriver(opts.driver, this.self, opts);
};
})(EventEmitter);
});
return Device;
})(EventEmitter);
});
module.exports = Cylon.Device;
}).call(this);
module.exports = Cylon.Device;