Merge pull request #120 from hybridgroup/pure-js-core

Refactoring core intro pure JS
This commit is contained in:
Andrew Stewart 2014-02-27 12:12:49 -08:00
commit f916b38b09
12 changed files with 985 additions and 1046 deletions

View File

@ -2,52 +2,66 @@
* adaptor
* 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 namespace,
__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('./basestar');
var namespace = require('node-namespace');
namespace = require('node-namespace');
// The Adaptor class is a base class for Adaptor classes in external Cylon
// modules to use. It offers basic functions for connecting/disconnecting that
// descendant classes can use.
namespace("Cylon", function() {
this.Adaptor = (function(klass) {
subclass(Adaptor, klass);
require('./basestar');
namespace('Cylon', function() {
return this.Adaptor = (function(_super) {
__extends(Adaptor, _super);
function Adaptor(opts) {
if (opts == null) {
opts = {};
}
this.self = this;
this.name = opts.name;
this.connection = opts.connection;
this.commandList = [];
// Public: Creates a new Adaptor
//
// opts - hash of acceptable params
// name - name of the Adaptor, used when printing to console
// connection - Connection the adaptor will use to proxy commands/events
//
// Returns a new Adaptor
function Adaptor(opts) {
if (opts == null) {
opts = {};
}
this.self = this;
this.name = opts.name;
this.connection = opts.connection;
this.commandList = [];
}
Adaptor.prototype.commands = function() {
return this.commandList;
};
// Public: Exposes all commands the adaptor will respond to/proxy
//
// Returns an array of string method names
Adaptor.prototype.commands = function() {
return this.commandList;
};
Adaptor.prototype.connect = function(callback) {
Logger.info("Connecting to adaptor '" + this.name + "'...");
callback(null);
return this.connection.emit('connect');
};
// Public: Connects to the adaptor, and emits 'connect' from the @connection
// when done.
//
// callback - function to run when the adaptor is connected
//
// Returns nothing
Adaptor.prototype.connect = function(callback) {
Logger.info("Connecting to adaptor '" + this.name + "'...");
callback(null);
return this.connection.emit('connect');
};
Adaptor.prototype.disconnect = function() {
return Logger.info("Disconnecting from adaptor '" + this.name + "'...");
};
// Public: Disconnects from the adaptor
//
// Returns nothing
Adaptor.prototype.disconnect = function() {
return Logger.info("Disconnecting from adaptor '" + this.name + "'...");
};
return Adaptor;
return Adaptor;
})(Cylon.Basestar);
});
}).call(this);
})(Cylon.Basestar);
});

View File

@ -2,174 +2,180 @@
* api
* 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 express, namespace;
var express = require('express.io');
var namespace = require('node-namespace');
express = require('express.io');
namespace("Cylon", function() {
this.ApiServer = (function() {
var master;
namespace = require('node-namespace');
master = null;
namespace('Cylon', function() {
return this.ApiServer = (function() {
var master;
master = null;
function ApiServer(opts) {
var _this = this;
if (opts == null) {
opts = {};
}
this.host = opts.host || "127.0.0.1";
this.port = opts.port || "3000";
master = opts.master;
this.server = express().http().io();
this.server.set('title', 'Cylon API Server');
this.server.use(express.json());
this.server.use(express.urlencoded());
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/"));
this.server.get("/*", function(req, res, next) {
res.set('Content-Type', 'application/json');
return next();
});
this.configureRoutes();
this.server.listen(this.port, this.host, function() {
return Logger.info("" + (_this.server.get('title')) + " is listening on " + _this.host + ":" + _this.port);
});
function ApiServer(opts) {
var _this = this;
if (opts == null) {
opts = {};
}
this.host = opts.host || "127.0.0.1";
this.port = opts.port || "3000";
master = opts.master;
this.server = express().http().io();
this.server.set('title', 'Cylon API Server');
this.server.use(express.json());
this.server.use(express.urlencoded());
this.server.use(express["static"](__dirname + "/../node_modules/robeaux/"));
this.server.get("/*", function(req, res, next) {
res.set('Content-Type', 'application/json');
return next();
});
this.configureRoutes();
this.server.listen(this.port, this.host, function() {
return Logger.info("" + (_this.server.get('title')) + " is listening on " + _this.host + ":" + _this.port);
});
}
ApiServer.prototype.parseCommandParams = function(req) {
var param_container, params, v, _;
params = [];
param_container = {};
if (req.method === 'GET' || Object.keys(req.query).length > 0) {
param_container = req.query;
} else if (typeof req.body === 'object') {
param_container = req.body;
}
for (_ in param_container) {
v = param_container[_];
params.push(v);
}
return params;
};
ApiServer.prototype.parseCommandParams = function(req) {
var param_container, params, v, _;
params = [];
param_container = {};
if (req.method === 'GET' || Object.keys(req.query).length > 0) {
param_container = req.query;
} else if (typeof req.body === 'object') {
param_container = req.body;
}
for (_ in param_container) {
v = param_container[_];
params.push(v);
}
return params;
};
ApiServer.prototype.configureRoutes = function() {
var _this = this;
this.server.get("/robots", function(req, res) {
var robot;
return res.json((function() {
var _i, _len, _ref, _results;
_ref = master.robots();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
robot = _ref[_i];
_results.push(robot.data());
}
return _results;
})());
ApiServer.prototype.configureRoutes = function() {
var _this = this;
this.server.get("/robots", function(req, res) {
var robot;
return res.json((function() {
var _i, _len, _ref, _results;
_ref = master.robots();
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
robot = _ref[_i];
_results.push(robot.data());
}
return _results;
})());
});
this.server.get("/robots/:robotname", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data());
});
this.server.get("/robots/:robotname", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data());
});
this.server.get("/robots/:robotname/commands", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().commands);
});
});
this.server.all("/robots/:robotname/commands/:commandname", function(req, res) {
var params;
params = _this.parseCommandParams(req);
return master.findRobot(req.params.robotname, function(err, robot) {
var result;
if (err) {
return res.json(err);
}
result = robot[req.params.commandname].apply(robot, params);
return res.json({
result: result
});
});
this.server.get("/robots/:robotname/commands", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().commands);
});
this.server.get("/robots/:robotname/devices", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().devices);
});
});
this.server.get("/robots/:robotname/devices/:devicename", function(req, res) {
var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];
return master.findRobotDevice(robotname, devicename, function(err, device) {
return res.json(err ? err : device.data());
});
});
this.server.get("/robots/:robotname/devices/:devicename/commands", function(req, res) {
var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];
return master.findRobotDevice(robotname, devicename, function(err, device) {
return res.json(err ? err : device.data().commands);
});
});
this.server.all("/robots/:robot/devices/:device/commands/:commandname", function(req, res) {
var commandname, devicename, params, robotname;
params = [req.params.robot, req.params.device, req.params.commandname];
robotname = params[0], devicename = params[1], commandname = params[2];
params = _this.parseCommandParams(req);
return master.findRobotDevice(robotname, devicename, function(err, device) {
var result;
if (err) {
return res.json(err);
}
result = device[commandname].apply(device, params);
return res.json({
result: result
});
});
this.server.all("/robots/:robotname/commands/:commandname", function(req, res) {
var params;
params = _this.parseCommandParams(req);
return master.findRobot(req.params.robotname, function(err, robot) {
var result;
if (err) {
return res.json(err);
}
result = robot[req.params.commandname].apply(robot, params);
return res.json({
result: result
});
this.server.get("/robots/:robotname/connections", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().connections);
});
});
this.server.get("/robots/:robot/connections/:connection", function(req, res) {
var connectionname, robotname, _ref;
_ref = [req.params.robot, req.params.connection], robotname = _ref[0], connectionname = _ref[1];
return master.findRobotConnection(robotname, connectionname, function(err, connection) {
return res.json(err ? err : connection.data());
});
});
this.server.get("/robots/:robotname/devices/:devicename/events", function(req, res) {
return req.io.route('events');
});
return this.server.io.route('events', function(req) {
var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];
return master.findRobotDevice(robotname, devicename, function(err, device) {
if (err) {
req.io.respond(err);
}
return device.on('update', function(data) {
return req.io.emit('update', {
data: data
});
});
});
this.server.get("/robots/:robotname/devices", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().devices);
});
});
this.server.get("/robots/:robotname/devices/:devicename", function(req, res) {
var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];
return master.findRobotDevice(robotname, devicename, function(err, device) {
return res.json(err ? err : device.data());
});
});
this.server.get("/robots/:robotname/devices/:devicename/commands", function(req, res) {
var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];
return master.findRobotDevice(robotname, devicename, function(err, device) {
return res.json(err ? err : device.data().commands);
});
});
this.server.all("/robots/:robot/devices/:device/commands/:commandname", function(req, res) {
var commandname, devicename, params, robotname;
params = [req.params.robot, req.params.device, req.params.commandname];
robotname = params[0], devicename = params[1], commandname = params[2];
params = _this.parseCommandParams(req);
return master.findRobotDevice(robotname, devicename, function(err, device) {
var result;
if (err) {
return res.json(err);
}
result = device[commandname].apply(device, params);
return res.json({
result: result
});
});
});
this.server.get("/robots/:robotname/connections", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().connections);
});
});
this.server.get("/robots/:robot/connections/:connection", function(req, res) {
var connectionname, robotname, _ref;
_ref = [req.params.robot, req.params.connection], robotname = _ref[0], connectionname = _ref[1];
return master.findRobotConnection(robotname, connectionname, function(err, connection) {
return res.json(err ? err : connection.data());
});
});
this.server.get("/robots/:robotname/devices/:devicename/events", function(req, res) {
return req.io.route('events');
});
return this.server.io.route('events', function(req) {
var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];
return master.findRobotDevice(robotname, devicename, function(err, device) {
if (err) {
req.io.respond(err);
}
return device.on('update', function(data) {
return req.io.emit('update', {
data: data
});
});
});
});
};
});
};
return ApiServer;
return ApiServer;
})();
});
})();
});
module.exports = Cylon.ApiServer;
}).call(this);
module.exports = Cylon.ApiServer;

View File

@ -2,76 +2,66 @@
* basestar
* 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,
__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; },
__slice = [].slice;
require('./utils');
var namespace = require('node-namespace');
var EventEmitter = require('events').EventEmitter;
require('./utils');
namespace("Cylon", function() {
this.Basestar = (function(klass) {
subclass(Basestar, klass);
namespace = require('node-namespace');
function Basestar(opts) {
this.self = this;
}
EventEmitter = require('events').EventEmitter;
namespace('Cylon', function() {
return this.Basestar = (function(_super) {
__extends(Basestar, _super);
function Basestar(opts) {
this.self = this;
Basestar.prototype.proxyMethods = function(methods, target, source, force) {
if (force == null) {
force = false;
}
return proxyFunctionsToObject(methods, target, source, force);
};
Basestar.prototype.proxyMethods = function(methods, target, source, force) {
if (force == null) {
force = false;
Basestar.prototype.defineEvent = function(opts) {
var sendUpdate, targetEventName,
_this = this;
targetEventName = opts.targetEventName || opts.eventName;
sendUpdate = opts.sendUpdate || false;
opts.source.on(opts.eventName, function() {
var args, _ref, _ref1;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
(_ref = opts.target).emit.apply(_ref, [targetEventName].concat(__slice.call(args)));
if (sendUpdate) {
return (_ref1 = opts.target).emit.apply(_ref1, ['update', targetEventName].concat(__slice.call(args)));
}
return proxyFunctionsToObject(methods, target, source, force);
};
});
return opts.source;
};
Basestar.prototype.defineEvent = function(opts) {
var sendUpdate, targetEventName,
_this = this;
targetEventName = opts.targetEventName || opts.eventName;
sendUpdate = opts.sendUpdate || false;
opts.source.on(opts.eventName, function() {
var args, _ref, _ref1;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
(_ref = opts.target).emit.apply(_ref, [targetEventName].concat(__slice.call(args)));
if (sendUpdate) {
return (_ref1 = opts.target).emit.apply(_ref1, ['update', targetEventName].concat(__slice.call(args)));
}
});
return opts.source;
};
Basestar.prototype.defineAdaptorEvent = function(opts) {
opts['source'] = this.connector;
opts['target'] = this.connection;
if (opts['sendUpdate'] == null) {
opts['sendUpdate'] = false;
}
return this.defineEvent(opts);
};
Basestar.prototype.defineAdaptorEvent = function(opts) {
opts['source'] = this.connector;
opts['target'] = this.connection;
if (opts['sendUpdate'] == null) {
opts['sendUpdate'] = false;
}
return this.defineEvent(opts);
};
Basestar.prototype.defineDriverEvent = function(opts) {
opts['source'] = this.connection;
opts['target'] = this.device;
if (opts['sendUpdate'] == null) {
opts['sendUpdate'] = true;
}
return this.defineEvent(opts);
};
Basestar.prototype.defineDriverEvent = function(opts) {
opts['source'] = this.connection;
opts['target'] = this.device;
if (opts['sendUpdate'] == null) {
opts['sendUpdate'] = true;
}
return this.defineEvent(opts);
};
return Basestar;
return Basestar;
})(EventEmitter);
});
}).call(this);
})(EventEmitter);
});

View File

@ -2,31 +2,27 @@
* cylon configuration loader
* 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() {
var fetch, namespace;
var namespace = require('node-namespace');
namespace = require('node-namespace');
var fetch = function(variable, defaultValue) {
if (defaultValue == null) {
defaultValue = false;
}
if (process.env[variable] != null) {
return process.env[variable];
} else {
return defaultValue;
}
};
fetch = function(variable, defaultValue) {
if (defaultValue == null) {
defaultValue = false;
}
if (process.env[variable] != null) {
return process.env[variable];
} else {
return defaultValue;
}
};
namespace('CylonConfig', function() {
return this.testing_mode = fetch("CYLON_TEST", false);
});
namespace('CylonConfig', function() {
return this.testing_mode = fetch("CYLON_TEST", false);
});
module.exports = CylonConfig;
}).call(this);
module.exports = CylonConfig;

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;

View File

@ -2,201 +2,186 @@
* Linux IO DigitalPin
* 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, FS, namespace,
__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; };
var FS = require('fs');
var EventEmitter = require('events').EventEmitter;
var namespace = require('node-namespace');
FS = require('fs');
namespace("Cylon.IO", function() {
this.DigitalPin = (function(klass) {
subclass(DigitalPin, klass);
EventEmitter = require('events').EventEmitter;
var GPIO_PATH = "/sys/class/gpio";
var GPIO_READ = "in";
var GPIO_WRITE = "out";
var HIGH = 1;
var LOW = 0;
namespace = require('node-namespace');
function DigitalPin(opts) {
this.pinNum = opts.pin;
this.status = 'low';
this.ready = false;
this.mode = opts.mode;
}
namespace('Cylon.IO', function() {
return this.DigitalPin = (function(_super) {
var GPIO_PATH, GPIO_READ, GPIO_WRITE, HIGH, LOW;
__extends(DigitalPin, _super);
GPIO_PATH = "/sys/class/gpio";
GPIO_READ = "in";
GPIO_WRITE = "out";
HIGH = 1;
LOW = 0;
function DigitalPin(opts) {
this.pinNum = opts.pin;
this.status = 'low';
this.ready = false;
this.mode = opts.mode;
DigitalPin.prototype.connect = function(mode) {
var _this = this;
if (mode == null) {
mode = null;
}
DigitalPin.prototype.connect = function(mode) {
var _this = this;
if (mode == null) {
mode = null;
}
if (this.mode == null) {
this.mode = mode;
}
return FS.exists(this._pinPath(), function(exists) {
if (exists) {
return _this._openPin();
} else {
return _this._createGPIOPin();
}
});
};
DigitalPin.prototype.close = function() {
var _this = this;
return FS.writeFile(this._unexportPath(), "" + this.pinNum, function(err) {
return _this._closeCallback(err);
});
};
DigitalPin.prototype.closeSync = function() {
FS.writeFileSync(this._unexportPath(), "" + this.pinNum);
return this._closeCallback(false);
};
DigitalPin.prototype.digitalWrite = function(value) {
var _this = this;
if (this.mode !== 'w') {
this._setMode('w');
}
this.status = value === 1 ? 'high' : 'low';
FS.writeFile(this._valuePath(), value, function(err) {
if (err) {
return _this.emit('error', "Error occurred while writing value " + value + " to pin " + _this.pinNum);
} else {
return _this.emit('digitalWrite', value);
}
});
return value;
};
DigitalPin.prototype.digitalRead = function(interval) {
var readData,
_this = this;
if (this.mode !== 'r') {
this._setMode('r');
}
readData = null;
return every(interval, function() {
return FS.readFile(_this._valuePath(), function(err, data) {
if (err) {
return _this.emit('error', "Error occurred while reading from pin " + _this.pinNum);
} else {
readData = parseInt(data.toString());
return _this.emit('digitalRead', readData);
}
});
});
};
DigitalPin.prototype.setHigh = function() {
return this.digitalWrite(1);
};
DigitalPin.prototype.setLow = function() {
return this.digitalWrite(0);
};
DigitalPin.prototype.toggle = function() {
if (this.status === 'low') {
return this.setHigh();
} else {
return this.setLow();
}
};
DigitalPin.prototype._createGPIOPin = function() {
var _this = this;
return FS.writeFile(this._exportPath(), "" + this.pinNum, function(err) {
if (err) {
return _this.emit('error', 'Error while creating pin files');
} else {
return _this._openPin();
}
});
};
DigitalPin.prototype._openPin = function() {
this._setMode(this.mode, true);
return this.emit('open');
};
DigitalPin.prototype._closeCallback = function(err) {
if (err) {
return this.emit('error', 'Error while closing pin files');
} else {
return this.emit('close', this.pinNum);
}
};
DigitalPin.prototype._setMode = function(mode, emitConnect) {
var _this = this;
if (emitConnect == null) {
emitConnect = false;
}
if (this.mode == null) {
this.mode = mode;
if (mode === 'w') {
return FS.writeFile(this._directionPath(), GPIO_WRITE, function(err) {
return _this._setModeCallback(err, emitConnect);
});
} else if (mode === 'r') {
return FS.writeFile(this._directionPath(), GPIO_READ, function(err) {
return _this._setModeCallback(err, emitConnect);
});
}
};
DigitalPin.prototype._setModeCallback = function(err, emitConnect) {
if (err) {
return this.emit('error', "Setting up pin direction failed");
}
return FS.exists(this._pinPath(), function(exists) {
if (exists) {
return _this._openPin();
} else {
this.ready = true;
if (emitConnect) {
return this.emit('connect', this.mode);
}
return _this._createGPIOPin();
}
};
});
};
DigitalPin.prototype._directionPath = function() {
return "" + (this._pinPath()) + "/direction";
};
DigitalPin.prototype.close = function() {
var _this = this;
return FS.writeFile(this._unexportPath(), "" + this.pinNum, function(err) {
return _this._closeCallback(err);
});
};
DigitalPin.prototype._valuePath = function() {
return "" + (this._pinPath()) + "/value";
};
DigitalPin.prototype.closeSync = function() {
FS.writeFileSync(this._unexportPath(), "" + this.pinNum);
return this._closeCallback(false);
};
DigitalPin.prototype._pinPath = function() {
return "" + GPIO_PATH + "/gpio" + this.pinNum;
};
DigitalPin.prototype.digitalWrite = function(value) {
var _this = this;
if (this.mode !== 'w') {
this._setMode('w');
}
this.status = value === 1 ? 'high' : 'low';
FS.writeFile(this._valuePath(), value, function(err) {
if (err) {
return _this.emit('error', "Error occurred while writing value " + value + " to pin " + _this.pinNum);
} else {
return _this.emit('digitalWrite', value);
}
});
return value;
};
DigitalPin.prototype._exportPath = function() {
return "" + GPIO_PATH + "/export";
};
DigitalPin.prototype.digitalRead = function(interval) {
var readData,
_this = this;
if (this.mode !== 'r') {
this._setMode('r');
}
readData = null;
return every(interval, function() {
return FS.readFile(_this._valuePath(), function(err, data) {
if (err) {
return _this.emit('error', "Error occurred while reading from pin " + _this.pinNum);
} else {
readData = parseInt(data.toString());
return _this.emit('digitalRead', readData);
}
});
});
};
DigitalPin.prototype._unexportPath = function() {
return "" + GPIO_PATH + "/unexport";
};
DigitalPin.prototype.setHigh = function() {
return this.digitalWrite(1);
};
return DigitalPin;
DigitalPin.prototype.setLow = function() {
return this.digitalWrite(0);
};
})(EventEmitter);
});
DigitalPin.prototype.toggle = function() {
if (this.status === 'low') {
return this.setHigh();
} else {
return this.setLow();
}
};
}).call(this);
DigitalPin.prototype._createGPIOPin = function() {
var _this = this;
return FS.writeFile(this._exportPath(), "" + this.pinNum, function(err) {
if (err) {
return _this.emit('error', 'Error while creating pin files');
} else {
return _this._openPin();
}
});
};
DigitalPin.prototype._openPin = function() {
this._setMode(this.mode, true);
return this.emit('open');
};
DigitalPin.prototype._closeCallback = function(err) {
if (err) {
return this.emit('error', 'Error while closing pin files');
} else {
return this.emit('close', this.pinNum);
}
};
DigitalPin.prototype._setMode = function(mode, emitConnect) {
var _this = this;
if (emitConnect == null) {
emitConnect = false;
}
this.mode = mode;
if (mode === 'w') {
return FS.writeFile(this._directionPath(), GPIO_WRITE, function(err) {
return _this._setModeCallback(err, emitConnect);
});
} else if (mode === 'r') {
return FS.writeFile(this._directionPath(), GPIO_READ, function(err) {
return _this._setModeCallback(err, emitConnect);
});
}
};
DigitalPin.prototype._setModeCallback = function(err, emitConnect) {
if (err) {
return this.emit('error', "Setting up pin direction failed");
} else {
this.ready = true;
if (emitConnect) {
return this.emit('connect', this.mode);
}
}
};
DigitalPin.prototype._directionPath = function() {
return "" + (this._pinPath()) + "/direction";
};
DigitalPin.prototype._valuePath = function() {
return "" + (this._pinPath()) + "/value";
};
DigitalPin.prototype._pinPath = function() {
return "" + GPIO_PATH + "/gpio" + this.pinNum;
};
DigitalPin.prototype._exportPath = function() {
return "" + GPIO_PATH + "/export";
};
DigitalPin.prototype._unexportPath = function() {
return "" + GPIO_PATH + "/unexport";
};
return DigitalPin;
})(EventEmitter);
});

View File

@ -2,54 +2,46 @@
* driver
* 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 namespace,
__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('./basestar');
var namespace = require('node-namespace');
namespace = require('node-namespace');
namespace("Cylon", function() {
this.Driver = (function(klass) {
subclass(Driver, klass);
require('./basestar');
namespace('Cylon', function() {
return this.Driver = (function(_super) {
__extends(Driver, _super);
function Driver(opts) {
if (opts == null) {
opts = {};
}
this.self = this;
this.name = opts.name;
this.device = opts.device;
this.connection = this.device.connection;
this.commandList = [];
function Driver(opts) {
if (opts == null) {
opts = {};
}
this.self = this;
this.name = opts.name;
this.device = opts.device;
this.connection = this.device.connection;
this.commandList = [];
}
Driver.prototype.commands = function() {
return this.commandList;
};
Driver.prototype.commands = function() {
return this.commandList;
};
Driver.prototype.start = function(callback) {
Logger.info("Driver " + this.name + " started");
callback(null);
this.device.emit('start');
return true;
};
Driver.prototype.start = function(callback) {
Logger.info("Driver " + this.name + " started");
callback(null);
this.device.emit('start');
return true;
};
Driver.prototype.stop = function() {
return Logger.info("Driver " + this.name + " stopped");
};
Driver.prototype.stop = function() {
return Logger.info("Driver " + this.name + " stopped");
};
return Driver;
return Driver;
})(Cylon.Basestar);
});
}).call(this);
})(Cylon.Basestar);
});

View File

@ -2,60 +2,55 @@
* port
* 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 namespace;
var namespace = require('node-namespace');
namespace = require('node-namespace');
namespace("Cylon", function() {
return this.Port = (function() {
function Port(data) {
this.self = this;
this.isTcp = this.isSerial = this.isPortless = false;
this.parse(data);
}
namespace('Cylon', function() {
return this.Port = (function() {
function Port(data) {
this.self = this;
this.isTcp = this.isSerial = this.isPortless = false;
this.parse(data);
Port.prototype.parse = function(data) {
var match;
if (data === void 0) {
this.port = void 0;
return this.isPortless = true;
} else if (match = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})/.exec(data)) {
this.port = match[2];
this.host = match[1];
return this.isTcp = true;
} else if (/^[0-9]{1,5}$/.exec(data)) {
this.port = data;
this.host = "localhost";
return this.isTcp = true;
} else {
this.port = data;
this.host = void 0;
return this.isSerial = true;
}
};
Port.prototype.parse = function(data) {
var match;
if (data === void 0) {
this.port = void 0;
return this.isPortless = true;
} else if (match = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})/.exec(data)) {
this.port = match[2];
this.host = match[1];
return this.isTcp = true;
} else if (/^[0-9]{1,5}$/.exec(data)) {
this.port = data;
this.host = "localhost";
return this.isTcp = true;
} else {
this.port = data;
this.host = void 0;
return this.isSerial = true;
}
};
Port.prototype.toString = function() {
if (this.isPortless) {
return "none";
} else if (this.isSerial) {
return this.port;
} else {
return "" + this.host + ":" + this.port;
}
};
Port.prototype.toString = function() {
if (this.isPortless) {
return "none";
} else if (this.isSerial) {
return this.port;
} else {
return "" + this.host + ":" + this.port;
}
};
return Port;
return Port;
})();
});
})();
});
module.exports = Cylon.Port;
}).call(this);
module.exports = Cylon.Port;

View File

@ -2,290 +2,272 @@
* robot
* 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 Async, 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; },
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
require('./cylon');
require('./cylon');
require('./basestar');
require("./connection");
require("./adaptor");
require("./device");
require("./driver");
require('./digital-pin');
var Async = require("async");
var EventEmitter = require('events').EventEmitter;
require('./basestar');
var namespace = require('node-namespace');
require("./connection");
namespace("Cylon", function() {
this.Robot = (function(klass) {
subclass(Robot, klass);
require("./adaptor");
require("./device");
require("./driver");
require('./digital-pin');
namespace = require('node-namespace');
Async = require("async");
EventEmitter = require('events').EventEmitter;
namespace('Cylon', function() {
return this.Robot = (function(_super) {
var klass;
__extends(Robot, _super);
klass = Robot;
function Robot(opts) {
var func, n, reserved;
if (opts == null) {
opts = {};
}
this.toString = __bind(this.toString, this);
this.registerDriver = __bind(this.registerDriver, this);
this.requireDriver = __bind(this.requireDriver, this);
this.registerAdaptor = __bind(this.registerAdaptor, this);
this.requireAdaptor = __bind(this.requireAdaptor, this);
this.stop = __bind(this.stop, this);
this.startDevices = __bind(this.startDevices, this);
this.startConnections = __bind(this.startConnections, this);
this.start = __bind(this.start, this);
this.initDevices = __bind(this.initDevices, this);
this.initConnections = __bind(this.initConnections, this);
this.robot = this;
this.name = opts.name || this.constructor.randomName();
this.master = opts.master;
this.connections = {};
this.devices = {};
this.adaptors = {};
this.drivers = {};
this.commands = [];
this.running = false;
this.registerAdaptor("./test/loopback", "loopback");
this.registerAdaptor("./test/test-adaptor", "test");
this.registerDriver("./test/ping", "ping");
this.registerDriver("./test/test-driver", "test");
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
this.work = opts.work || function() {
return Logger.info("No work yet");
};
for (n in opts) {
func = opts[n];
reserved = ['connection', 'connections', 'device', 'devices', 'work'];
if (__indexOf.call(reserved, n) < 0) {
this.robot[n] = func;
}
function Robot(opts) {
var func, n, reserved;
if (opts == null) {
opts = {};
}
this.toString = __bind(this.toString, this);
this.registerDriver = __bind(this.registerDriver, this);
this.requireDriver = __bind(this.requireDriver, this);
this.registerAdaptor = __bind(this.registerAdaptor, this);
this.requireAdaptor = __bind(this.requireAdaptor, this);
this.stop = __bind(this.stop, this);
this.startDevices = __bind(this.startDevices, this);
this.startConnections = __bind(this.startConnections, this);
this.start = __bind(this.start, this);
this.initDevices = __bind(this.initDevices, this);
this.initConnections = __bind(this.initConnections, this);
this.robot = this;
this.name = opts.name || this.constructor.randomName();
this.master = opts.master;
this.connections = {};
this.devices = {};
this.adaptors = {};
this.drivers = {};
this.commands = [];
this.running = false;
this.registerAdaptor("./test/loopback", "loopback");
this.registerAdaptor("./test/test-adaptor", "test");
this.registerDriver("./test/ping", "ping");
this.registerDriver("./test/test-driver", "test");
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
this.work = opts.work || function() {
return Logger.info("No work yet");
};
for (n in opts) {
func = opts[n];
reserved = ['connection', 'connections', 'device', 'devices', 'work'];
if (__indexOf.call(reserved, n) < 0) {
this.robot[n] = func;
}
}
}
Robot.randomName = function() {
return "Robot " + (Math.floor(Math.random() * 100000));
Robot.randomName = function() {
return "Robot " + (Math.floor(Math.random() * 100000));
};
Robot.prototype.data = function() {
var connection, device, n;
return {
name: this.name,
connections: (function() {
var _ref, _results;
_ref = this.connections;
_results = [];
for (n in _ref) {
connection = _ref[n];
_results.push(connection.data());
}
return _results;
}).call(this),
devices: (function() {
var _ref, _results;
_ref = this.devices;
_results = [];
for (n in _ref) {
device = _ref[n];
_results.push(device.data());
}
return _results;
}).call(this),
commands: this.commands
};
};
Robot.prototype.data = function() {
var connection, device, n;
return {
name: this.name,
connections: (function() {
var _ref, _results;
_ref = this.connections;
_results = [];
for (n in _ref) {
connection = _ref[n];
_results.push(connection.data());
}
return _results;
}).call(this),
devices: (function() {
var _ref, _results;
_ref = this.devices;
_results = [];
for (n in _ref) {
device = _ref[n];
_results.push(device.data());
}
return _results;
}).call(this),
commands: this.commands
};
};
Robot.prototype.initConnections = function(connections) {
var connection, _i, _len;
Logger.info("Initializing connections...");
if (connections == null) {
return;
}
connections = [].concat(connections);
for (_i = 0, _len = connections.length; _i < _len; _i++) {
connection = connections[_i];
Logger.info("Initializing connection '" + connection.name + "'...");
connection['robot'] = this;
this.connections[connection.name] = new Cylon.Connection(connection);
}
return this.connections;
};
Robot.prototype.initConnections = function(connections) {
var connection, _i, _len;
Logger.info("Initializing connections...");
if (connections == null) {
return;
}
connections = [].concat(connections);
for (_i = 0, _len = connections.length; _i < _len; _i++) {
connection = connections[_i];
Logger.info("Initializing connection '" + connection.name + "'...");
connection['robot'] = this;
this.connections[connection.name] = new Cylon.Connection(connection);
}
return this.connections;
};
Robot.prototype.initDevices = function(devices) {
var device, _i, _len, _results;
Logger.info("Initializing devices...");
if (devices == null) {
return;
}
devices = [].concat(devices);
_results = [];
for (_i = 0, _len = devices.length; _i < _len; _i++) {
device = devices[_i];
Logger.info("Initializing device '" + device.name + "'...");
device['robot'] = this;
_results.push(this.devices[device.name] = new Cylon.Device(device));
}
return _results;
};
Robot.prototype.initDevices = function(devices) {
var device, _i, _len, _results;
Logger.info("Initializing devices...");
if (devices == null) {
return;
}
devices = [].concat(devices);
_results = [];
for (_i = 0, _len = devices.length; _i < _len; _i++) {
device = devices[_i];
Logger.info("Initializing device '" + device.name + "'...");
device['robot'] = this;
_results.push(this.devices[device.name] = new Cylon.Device(device));
}
return _results;
};
Robot.prototype.start = function() {
var _this = this;
return this.startConnections(function() {
return _this.robot.startDevices(function() {
_this.robot.work.call(_this.robot, _this.robot);
_this.running = true;
Logger.info("Working...");
return _this.robot.emit('working');
});
Robot.prototype.start = function() {
var _this = this;
return this.startConnections(function() {
return _this.robot.startDevices(function() {
_this.robot.work.call(_this.robot, _this.robot);
_this.running = true;
Logger.info("Working...");
return _this.robot.emit('working');
});
};
});
};
Robot.prototype.startConnections = function(callback) {
var connection, n, starters, _ref;
Logger.info("Starting connections...");
starters = {};
_ref = this.connections;
for (n in _ref) {
connection = _ref[n];
this.robot[n] = connection;
starters[n] = connection.connect;
}
return Async.parallel(starters, callback);
};
Robot.prototype.startConnections = function(callback) {
var connection, n, starters, _ref;
Logger.info("Starting connections...");
starters = {};
_ref = this.connections;
for (n in _ref) {
connection = _ref[n];
this.robot[n] = connection;
starters[n] = connection.connect;
}
return Async.parallel(starters, callback);
};
Robot.prototype.startDevices = function(callback) {
var device, n, starters, _ref;
Logger.info("Starting devices...");
starters = {};
_ref = this.devices;
for (n in _ref) {
device = _ref[n];
this.robot[n] = device;
starters[n] = device.start;
}
return Async.parallel(starters, callback);
};
Robot.prototype.startDevices = function(callback) {
var device, n, starters, _ref;
Logger.info("Starting devices...");
starters = {};
_ref = this.devices;
for (n in _ref) {
device = _ref[n];
this.robot[n] = device;
starters[n] = device.start;
}
return Async.parallel(starters, callback);
};
Robot.prototype.stop = function() {
var connection, device, n, _ref, _ref1, _results;
_ref = this.devices;
for (n in _ref) {
device = _ref[n];
device.stop();
}
_ref1 = this.connections;
_results = [];
for (n in _ref1) {
connection = _ref1[n];
_results.push(connection.disconnect());
}
return _results;
};
Robot.prototype.stop = function() {
var connection, device, n, _ref, _ref1, _results;
_ref = this.devices;
for (n in _ref) {
device = _ref[n];
device.stop();
}
_ref1 = this.connections;
_results = [];
for (n in _ref1) {
connection = _ref1[n];
_results.push(connection.disconnect());
}
return _results;
};
Robot.prototype.initAdaptor = function(adaptorName, connection, opts) {
var realAdaptor, testAdaptor;
if (opts == null) {
opts = {};
}
realAdaptor = this.robot.requireAdaptor(adaptorName).adaptor({
Robot.prototype.initAdaptor = function(adaptorName, connection, opts) {
var realAdaptor, testAdaptor;
if (opts == null) {
opts = {};
}
realAdaptor = this.robot.requireAdaptor(adaptorName).adaptor({
name: adaptorName,
connection: connection,
extraParams: opts
});
if (CylonConfig.testing_mode) {
testAdaptor = this.robot.requireAdaptor('test').adaptor({
name: adaptorName,
connection: connection,
extraParams: opts
});
if (CylonConfig.testing_mode) {
testAdaptor = this.robot.requireAdaptor('test').adaptor({
name: adaptorName,
connection: connection,
extraParams: opts
});
return proxyTestStubs(realAdaptor.commands(), testAdaptor);
} else {
return realAdaptor;
}
};
return proxyTestStubs(realAdaptor.commands(), testAdaptor);
} else {
return realAdaptor;
}
};
Robot.prototype.requireAdaptor = function(adaptorName) {
if (this.robot.adaptors[adaptorName] == null) {
this.robot.registerAdaptor("cylon-" + adaptorName, adaptorName);
this.robot.adaptors[adaptorName].register(this);
}
return this.robot.adaptors[adaptorName];
};
Robot.prototype.requireAdaptor = function(adaptorName) {
if (this.robot.adaptors[adaptorName] == null) {
this.robot.registerAdaptor("cylon-" + adaptorName, adaptorName);
this.robot.adaptors[adaptorName].register(this);
}
return this.robot.adaptors[adaptorName];
};
Robot.prototype.registerAdaptor = function(moduleName, adaptorName) {
if (this.adaptors[adaptorName] == null) {
return this.adaptors[adaptorName] = require(moduleName);
}
};
Robot.prototype.registerAdaptor = function(moduleName, adaptorName) {
if (this.adaptors[adaptorName] == null) {
return this.adaptors[adaptorName] = require(moduleName);
}
};
Robot.prototype.initDriver = function(driverName, device, opts) {
var realDriver, testDriver;
if (opts == null) {
opts = {};
}
realDriver = this.robot.requireDriver(driverName).driver({
Robot.prototype.initDriver = function(driverName, device, opts) {
var realDriver, testDriver;
if (opts == null) {
opts = {};
}
realDriver = this.robot.requireDriver(driverName).driver({
name: driverName,
device: device,
extraParams: opts
});
if (CylonConfig.testing_mode) {
testDriver = this.robot.requireDriver('test').driver({
name: driverName,
device: device,
extraParams: opts
});
if (CylonConfig.testing_mode) {
testDriver = this.robot.requireDriver('test').driver({
name: driverName,
device: device,
extraParams: opts
});
return proxyTestStubs(realDriver.commands(), testDriver);
} else {
return realDriver;
}
};
return proxyTestStubs(realDriver.commands(), testDriver);
} else {
return realDriver;
}
};
Robot.prototype.requireDriver = function(driverName) {
if (this.robot.drivers[driverName] == null) {
this.robot.registerDriver("cylon-" + driverName, driverName);
this.robot.drivers[driverName].register(this);
}
return this.robot.drivers[driverName];
};
Robot.prototype.requireDriver = function(driverName) {
if (this.robot.drivers[driverName] == null) {
this.robot.registerDriver("cylon-" + driverName, driverName);
this.robot.drivers[driverName].register(this);
}
return this.robot.drivers[driverName];
};
Robot.prototype.registerDriver = function(moduleName, driverName) {
if (this.drivers[driverName] == null) {
return this.drivers[driverName] = require(moduleName);
}
};
Robot.prototype.registerDriver = function(moduleName, driverName) {
if (this.drivers[driverName] == null) {
return this.drivers[driverName] = require(moduleName);
}
};
Robot.prototype.toString = function() {
return "[Robot name='" + this.name + "']";
};
Robot.prototype.toString = function() {
return "[Robot name='" + this.name + "']";
};
return Robot;
return Robot;
})(EventEmitter);
});
})(EventEmitter);
});
module.exports = Cylon.Robot;
}).call(this);
module.exports = Cylon.Robot;

View File

@ -1,51 +1,53 @@
(function() {
'use strict';
var EventEmitter;
'use strict';
source("adaptor");
source("adaptor");
var EventEmitter = require('events').EventEmitter;
EventEmitter = require('events').EventEmitter;
describe("Adaptor", function() {
var adaptor, conn;
conn = new EventEmitter;
adaptor = new Cylon.Adaptor({
name: 'TestAdaptor',
connection: conn
});
it("provides a 'connect' method that accepts a callback", function() {
var spy;
expect(adaptor.connect).to.be.a('function');
spy = sinon.spy();
adaptor.connect(function() {
return spy();
});
return spy.should.have.been.called;
});
it("tells the connection to emit the 'connect' event when connected", function() {
var spy;
spy = sinon.spy();
adaptor.connection.on('connect', function() {
return spy();
});
adaptor.connect(function() {});
return spy.should.have.been.called;
});
it("provides a 'disconnect' method", function() {
return expect(adaptor.disconnect).to.be.a('function');
});
it("provides a default empty array of commands", function() {
return expect(adaptor.commands()).to.be.eql([]);
});
it("saves the provided name in the @name variable", function() {
return expect(adaptor.name).to.be.eql("TestAdaptor");
});
it("saves the provided connection in the @connection variable", function() {
return expect(adaptor.connection).to.be.eql(conn);
});
return it("contains a reference to itself in the @self variable", function() {
return expect(adaptor.self).to.be.eql(adaptor);
});
describe("Adaptor", function() {
var adaptor, conn;
conn = new EventEmitter;
adaptor = new Cylon.Adaptor({
name: 'TestAdaptor',
connection: conn
});
}).call(this);
it("provides a 'connect' method that accepts a callback", function() {
var spy;
expect(adaptor.connect).to.be.a('function');
spy = sinon.spy();
adaptor.connect(function() {
return spy();
});
spy.should.have.been.called;
});
it("tells the connection to emit the 'connect' event when connected", function() {
var spy;
spy = sinon.spy();
adaptor.connection.on('connect', function() {
return spy();
});
adaptor.connect(function() {});
spy.should.have.been.called;
});
it("provides a 'disconnect' method", function() {
expect(adaptor.disconnect).to.be.a('function');
});
it("provides a default empty array of commands", function() {
expect(adaptor.commands()).to.be.eql([]);
});
it("saves the provided name in the @name variable", function() {
expect(adaptor.name).to.be.eql("TestAdaptor");
});
it("saves the provided connection in the @connection variable", function() {
expect(adaptor.connection).to.be.eql(conn);
});
it("contains a reference to itself in the @self variable", function() {
expect(adaptor.self).to.be.eql(adaptor);
});
});

View File

@ -1,72 +1,70 @@
(function() {
'use strict';
var EventEmitter,
__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; };
source('basestar');
source('basestar');
var EventEmitter = require('events').EventEmitter;
EventEmitter = require('events').EventEmitter;
describe('Basestar', function() {
describe('constructor', function() {
return it('assigns @self to the instance of the Basestar class', function() {
var instance;
instance = new Cylon.Basestar;
return instance.self.should.be.eql(instance);
});
});
return describe('#proxyMethods', function() {
var ProxyClass, TestClass, methods;
methods = ['asString', 'toString', 'returnString'];
ProxyClass = (function() {
function ProxyClass() {}
ProxyClass.prototype.asString = function() {
return "[object ProxyClass]";
};
ProxyClass.prototype.toString = function() {
return "[object ProxyClass]";
};
ProxyClass.prototype.returnString = function(string) {
return string;
};
return ProxyClass;
})();
TestClass = (function(_super) {
__extends(TestClass, _super);
function TestClass() {
this.testInstance = new ProxyClass;
this.proxyMethods(methods, this.testInstance, this, true);
}
return TestClass;
})(Cylon.Basestar);
it('can alias methods', function() {
var testclass;
testclass = new TestClass;
assert(typeof testclass.asString === 'function');
return testclass.asString().should.be.equal("[object ProxyClass]");
});
it('can alias existing methods if forced to', function() {
var testclass;
testclass = new TestClass;
assert(typeof testclass.toString === 'function');
return testclass.toString().should.be.equal("[object ProxyClass]");
});
return it('can alias methods with arguments', function() {
var testclass;
testclass = new TestClass;
assert(typeof testclass.returnString === 'function');
return testclass.returnString("testString").should.be.equal("testString");
});
describe('Basestar', function() {
describe('constructor', function() {
return it('assigns @self to the instance of the Basestar class', function() {
var instance;
instance = new Cylon.Basestar;
return instance.self.should.be.eql(instance);
});
});
}).call(this);
describe('#proxyMethods', function() {
var ProxyClass, TestClass, methods;
methods = ['asString', 'toString', 'returnString'];
ProxyClass = (function() {
function ProxyClass() {}
ProxyClass.prototype.asString = function() {
return "[object ProxyClass]";
};
ProxyClass.prototype.toString = function() {
return "[object ProxyClass]";
};
ProxyClass.prototype.returnString = function(string) {
return string;
};
return ProxyClass;
})();
TestClass = (function(_super) {
subclass(TestClass, _super);
function TestClass() {
this.testInstance = new ProxyClass;
this.proxyMethods(methods, this.testInstance, this, true);
}
return TestClass;
})(Cylon.Basestar);
it('can alias methods', function() {
var testclass;
testclass = new TestClass;
assert(typeof testclass.asString === 'function');
testclass.asString().should.be.equal("[object ProxyClass]");
});
it('can alias existing methods if forced to', function() {
var testclass;
testclass = new TestClass;
assert(typeof testclass.toString === 'function');
testclass.toString().should.be.equal("[object ProxyClass]");
});
it('can alias methods with arguments', function() {
var testclass;
testclass = new TestClass;
assert(typeof testclass.returnString === 'function');
testclass.returnString("testString").should.be.equal("testString");
});
});
});