Clean up Connection and Device classes
This commit is contained in:
parent
a88fe526f4
commit
79ff3a1233
|
@ -11,10 +11,10 @@
|
||||||
require("./robot");
|
require("./robot");
|
||||||
require("./port");
|
require("./port");
|
||||||
require("./adaptor");
|
require("./adaptor");
|
||||||
var namespace = require('node-namespace');
|
require("./utils");
|
||||||
var EventEmitter = require('events').EventEmitter;
|
|
||||||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
|
|
||||||
|
|
||||||
|
var namespace = require('node-namespace'),
|
||||||
|
EventEmitter = require('events').EventEmitter;
|
||||||
|
|
||||||
// The Connection class represents the interface to
|
// The Connection class represents the interface to
|
||||||
// a specific group of hardware devices. Examples would be an
|
// a specific group of hardware devices. Examples would be an
|
||||||
|
@ -34,19 +34,18 @@ namespace("Cylon", function() {
|
||||||
//
|
//
|
||||||
// Returns the newly set-up connection
|
// Returns the newly set-up connection
|
||||||
function Connection(opts) {
|
function Connection(opts) {
|
||||||
if (opts == null) {
|
if (opts == null) { opts = {}; }
|
||||||
opts = {};
|
if (opts.id == null) { opts.id = Math.floor(Math.random() * 10000); }
|
||||||
}
|
|
||||||
this.connect = __bind(this.connect, this);
|
this.connect = bind(this.connect, this);
|
||||||
if (opts.id == null) {
|
|
||||||
opts.id = Math.floor(Math.random() * 10000);
|
|
||||||
}
|
|
||||||
this.self = this;
|
this.self = this;
|
||||||
this.robot = opts.robot;
|
this.robot = opts.robot;
|
||||||
this.name = opts.name;
|
this.name = opts.name;
|
||||||
this.connection_id = opts.id;
|
this.connection_id = opts.id;
|
||||||
this.port = new Cylon.Port(opts.port);
|
this.port = new Cylon.Port(opts.port);
|
||||||
this.adaptor = this.initAdaptor(opts);
|
this.adaptor = this.initAdaptor(opts);
|
||||||
|
|
||||||
proxyFunctionsToObject(this.adaptor.commands(), this.adaptor, this.self);
|
proxyFunctionsToObject(this.adaptor.commands(), this.adaptor, this.self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,11 +67,12 @@ namespace("Cylon", function() {
|
||||||
//
|
//
|
||||||
// Returns the result of the supplied callback function
|
// Returns the result of the supplied callback function
|
||||||
Connection.prototype.connect = function(callback) {
|
Connection.prototype.connect = function(callback) {
|
||||||
var msg;
|
var msg = "Connecting to " + this.name;
|
||||||
msg = "Connecting to '" + this.name + "'";
|
|
||||||
if (this.port != null) {
|
if (this.port != null) {
|
||||||
msg += " on port '" + (this.port.toString()) + "'";
|
msg += " on port " + this.port.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.info(msg);
|
Logger.info(msg);
|
||||||
return this.adaptor.connect(callback);
|
return this.adaptor.connect(callback);
|
||||||
};
|
};
|
||||||
|
@ -81,11 +81,12 @@ namespace("Cylon", function() {
|
||||||
//
|
//
|
||||||
// Returns nothing
|
// Returns nothing
|
||||||
Connection.prototype.disconnect = function() {
|
Connection.prototype.disconnect = function() {
|
||||||
var msg;
|
var msg = "Disconnecting from " + this.name;
|
||||||
msg = "Disconnecting from '" + this.name + "'";
|
|
||||||
if (this.port != null) {
|
if (this.port != null) {
|
||||||
msg += " on port '" + (this.port.toString()) + "'";
|
msg += " on port " + this.port.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.info(msg);
|
Logger.info(msg);
|
||||||
return this.adaptor.disconnect();
|
return this.adaptor.disconnect();
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,9 +10,10 @@
|
||||||
|
|
||||||
require('./cylon');
|
require('./cylon');
|
||||||
require('./driver');
|
require('./driver');
|
||||||
var namespace = require('node-namespace');
|
require('./utils');
|
||||||
var EventEmitter = require('events').EventEmitter;
|
|
||||||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
|
var namespace = require('node-namespace'),
|
||||||
|
EventEmitter = require('events').EventEmitter;
|
||||||
|
|
||||||
// The Artoo::Device class represents the interface to
|
// The Artoo::Device class represents the interface to
|
||||||
// a specific individual hardware devices. Examples would be a digital
|
// a specific individual hardware devices. Examples would be a digital
|
||||||
|
@ -32,17 +33,18 @@ namespace("Cylon", function() {
|
||||||
//
|
//
|
||||||
// Returns a new Device
|
// Returns a new Device
|
||||||
function Device(opts) {
|
function Device(opts) {
|
||||||
if (opts == null) {
|
if (opts == null) { opts = {}; }
|
||||||
opts = {};
|
|
||||||
}
|
this.stop = bind(this.stop, this);
|
||||||
this.stop = __bind(this.stop, this);
|
this.start = bind(this.start, this);
|
||||||
this.start = __bind(this.start, this);
|
|
||||||
this.self = this;
|
this.self = this;
|
||||||
this.robot = opts.robot;
|
this.robot = opts.robot;
|
||||||
this.name = opts.name;
|
this.name = opts.name;
|
||||||
this.pin = opts.pin;
|
this.pin = opts.pin;
|
||||||
this.connection = this.determineConnection(opts.connection) || this.defaultConnection();
|
this.connection = this.determineConnection(opts.connection) || this.defaultConnection();
|
||||||
this.driver = this.initDriver(opts);
|
this.driver = this.initDriver(opts);
|
||||||
|
|
||||||
proxyFunctionsToObject(this.driver.commands(), this.driver, this.self);
|
proxyFunctionsToObject(this.driver.commands(), this.driver, this.self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,11 +54,12 @@ namespace("Cylon", function() {
|
||||||
//
|
//
|
||||||
// Returns result of supplied callback
|
// Returns result of supplied callback
|
||||||
Device.prototype.start = function(callback) {
|
Device.prototype.start = function(callback) {
|
||||||
var msg;
|
var msg = "Starting device " + this.name;
|
||||||
msg = "Starting device '" + this.name + "'";
|
|
||||||
if (this.pin != null) {
|
if (this.pin != null) {
|
||||||
msg += " on pin " + this.pin;
|
msg += " on pin " + this.pin;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.info(msg);
|
Logger.info(msg);
|
||||||
return this.driver.start(callback);
|
return this.driver.start(callback);
|
||||||
};
|
};
|
||||||
|
@ -65,7 +68,7 @@ namespace("Cylon", function() {
|
||||||
//
|
//
|
||||||
// Returns result of supplied callback
|
// Returns result of supplied callback
|
||||||
Device.prototype.stop = function() {
|
Device.prototype.stop = function() {
|
||||||
Logger.info("Stopping device '" + this.name + "'");
|
Logger.info("Stopping device " + this.name);
|
||||||
return this.driver.stop();
|
return this.driver.stop();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -88,22 +91,20 @@ namespace("Cylon", function() {
|
||||||
//
|
//
|
||||||
// Returns a Connection instance
|
// Returns a Connection instance
|
||||||
Device.prototype.determineConnection = function(c) {
|
Device.prototype.determineConnection = function(c) {
|
||||||
if (c) {
|
if (c) { return this.robot.connections[c]; }
|
||||||
return this.robot.connections[c];
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Returns a default Connection to use
|
// Public: Returns a default Connection to use
|
||||||
//
|
//
|
||||||
// Returns a Connection instance
|
// Returns a Connection instance
|
||||||
Device.prototype.defaultConnection = function() {
|
Device.prototype.defaultConnection = function() {
|
||||||
var first, k, v, _ref;
|
var first = 0;
|
||||||
first = 0;
|
|
||||||
_ref = this.robot.connections;
|
for (var c in this.robot.connections) {
|
||||||
for (k in _ref) {
|
var connection = this.robot.connections[c];
|
||||||
v = _ref[k];
|
first || (first = connection);
|
||||||
first || (first = v);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return first;
|
return first;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -114,9 +115,7 @@ namespace("Cylon", function() {
|
||||||
//
|
//
|
||||||
// Returns the set-up driver
|
// Returns the set-up driver
|
||||||
Device.prototype.initDriver = function(opts) {
|
Device.prototype.initDriver = function(opts) {
|
||||||
if (opts == null) {
|
if (opts == null) { opts = {}; }
|
||||||
opts = {};
|
|
||||||
}
|
|
||||||
Logger.debug("Loading driver '" + opts.driver + "'");
|
Logger.debug("Loading driver '" + opts.driver + "'");
|
||||||
return this.robot.initDriver(opts.driver, this.self, opts);
|
return this.robot.initDriver(opts.driver, this.self, opts);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue