From 79ff3a1233bec76328c48590f955661a9b32e21a Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Mon, 3 Mar 2014 12:28:19 -0800 Subject: [PATCH] Clean up Connection and Device classes --- lib/connection.js | 33 +++++++++++++++++---------------- lib/device.js | 45 ++++++++++++++++++++++----------------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 32f3236..dc6d22e 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -11,10 +11,10 @@ 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("./utils"); +var namespace = require('node-namespace'), + EventEmitter = require('events').EventEmitter; // The Connection class represents the interface to // a specific group of hardware devices. Examples would be an @@ -34,19 +34,18 @@ namespace("Cylon", function() { // // Returns the newly set-up connection function Connection(opts) { - if (opts == null) { - opts = {}; - } - this.connect = __bind(this.connect, this); - if (opts.id == null) { - opts.id = Math.floor(Math.random() * 10000); - } + if (opts == null) { opts = {}; } + if (opts.id == null) { opts.id = Math.floor(Math.random() * 10000); } + + this.connect = bind(this.connect, this); + this.self = this; this.robot = opts.robot; this.name = opts.name; this.connection_id = opts.id; this.port = new Cylon.Port(opts.port); this.adaptor = this.initAdaptor(opts); + proxyFunctionsToObject(this.adaptor.commands(), this.adaptor, this.self); } @@ -68,11 +67,12 @@ namespace("Cylon", function() { // // Returns the result of the supplied callback function Connection.prototype.connect = function(callback) { - var msg; - msg = "Connecting to '" + this.name + "'"; + var msg = "Connecting to " + this.name; + if (this.port != null) { - msg += " on port '" + (this.port.toString()) + "'"; + msg += " on port " + this.port.toString(); } + Logger.info(msg); return this.adaptor.connect(callback); }; @@ -81,11 +81,12 @@ namespace("Cylon", function() { // // Returns nothing Connection.prototype.disconnect = function() { - var msg; - msg = "Disconnecting from '" + this.name + "'"; + var msg = "Disconnecting from " + this.name; + if (this.port != null) { - msg += " on port '" + (this.port.toString()) + "'"; + msg += " on port " + this.port.toString(); } + Logger.info(msg); return this.adaptor.disconnect(); }; diff --git a/lib/device.js b/lib/device.js index 8e50228..bceeb66 100644 --- a/lib/device.js +++ b/lib/device.js @@ -10,9 +10,10 @@ 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('./utils'); + +var namespace = require('node-namespace'), + EventEmitter = require('events').EventEmitter; // The Artoo::Device class represents the interface to // a specific individual hardware devices. Examples would be a digital @@ -32,17 +33,18 @@ namespace("Cylon", function() { // // Returns a new Device function Device(opts) { - if (opts == null) { - opts = {}; - } - this.stop = __bind(this.stop, this); - this.start = __bind(this.start, this); + 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); } @@ -52,11 +54,12 @@ namespace("Cylon", function() { // // Returns result of supplied callback Device.prototype.start = function(callback) { - var msg; - msg = "Starting device '" + this.name + "'"; + var msg = "Starting device " + this.name; + if (this.pin != null) { msg += " on pin " + this.pin; } + Logger.info(msg); return this.driver.start(callback); }; @@ -65,7 +68,7 @@ namespace("Cylon", function() { // // Returns result of supplied callback Device.prototype.stop = function() { - Logger.info("Stopping device '" + this.name + "'"); + Logger.info("Stopping device " + this.name); return this.driver.stop(); }; @@ -88,22 +91,20 @@ namespace("Cylon", function() { // // Returns a Connection instance Device.prototype.determineConnection = function(c) { - if (c) { - return this.robot.connections[c]; - } + if (c) { return this.robot.connections[c]; } }; // Public: Returns a default Connection to use // // Returns a Connection instance 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); + var first = 0; + + for (var c in this.robot.connections) { + var connection = this.robot.connections[c]; + first || (first = connection); } + return first; }; @@ -114,9 +115,7 @@ namespace("Cylon", function() { // // Returns the set-up driver Device.prototype.initDriver = function(opts) { - if (opts == null) { - opts = {}; - } + if (opts == null) { opts = {}; } Logger.debug("Loading driver '" + opts.driver + "'"); return this.robot.initDriver(opts.driver, this.self, opts); };