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,25 +2,29 @@
* adaptor
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(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; };
namespace = require('node-namespace');
"use strict";
require('./basestar');
var namespace = require('node-namespace');
namespace('Cylon', function() {
return this.Adaptor = (function(_super) {
__extends(Adaptor, _super);
// 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);
// 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 = {};
@ -31,16 +35,28 @@
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;
};
// 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');
};
// Public: Disconnects from the adaptor
//
// Returns nothing
Adaptor.prototype.disconnect = function() {
return Logger.info("Disconnecting from adaptor '" + this.name + "'...");
};
@ -49,5 +65,3 @@
})(Cylon.Basestar);
});
}).call(this);

View File

@ -2,21 +2,17 @@
* 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 = require('node-namespace');
namespace('Cylon', function() {
return this.ApiServer = (function() {
namespace("Cylon", function() {
this.ApiServer = (function() {
var master;
master = null;
@ -62,6 +58,7 @@
ApiServer.prototype.configureRoutes = function() {
var _this = this;
this.server.get("/robots", function(req, res) {
var robot;
return res.json((function() {
@ -75,16 +72,19 @@
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/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);
@ -99,11 +99,13 @@
});
});
});
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];
@ -111,6 +113,7 @@
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];
@ -118,6 +121,7 @@
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];
@ -134,11 +138,13 @@
});
});
});
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];
@ -146,9 +152,11 @@
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];
@ -171,5 +179,3 @@
});
module.exports = Cylon.ApiServer;
}).call(this);

View File

@ -2,27 +2,19 @@
* basestar
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(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;
"use strict";
require('./utils');
var namespace = require('node-namespace');
var EventEmitter = require('events').EventEmitter;
namespace = require('node-namespace');
EventEmitter = require('events').EventEmitter;
namespace('Cylon', function() {
return this.Basestar = (function(_super) {
__extends(Basestar, _super);
namespace("Cylon", function() {
this.Basestar = (function(klass) {
subclass(Basestar, klass);
function Basestar(opts) {
this.self = this;
@ -73,5 +65,3 @@
})(EventEmitter);
});
}).call(this);

View File

@ -2,17 +2,15 @@
* 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');
fetch = function(variable, defaultValue) {
var fetch = function(variable, defaultValue) {
if (defaultValue == null) {
defaultValue = false;
}
@ -28,5 +26,3 @@
});
module.exports = CylonConfig;
}).call(this);

View File

@ -2,31 +2,22 @@
* connection
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(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); }; }
namespace = require('node-namespace');
EventEmitter = require('events').EventEmitter;
namespace('Cylon', function() {
return this.Connection = (function(_super) {
__extends(Connection, _super);
namespace("Cylon", function() {
this.Connection = (function(klass) {
subclass(Connection, klass);
function Connection(opts) {
if (opts == null) {
@ -85,5 +76,3 @@
});
module.exports = Cylon.Connection;
}).call(this);

View File

@ -2,29 +2,21 @@
* device
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(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); }; }
namespace = require('node-namespace');
EventEmitter = require('events').EventEmitter;
namespace('Cylon', function() {
return this.Device = (function(_super) {
__extends(Device, _super);
namespace("Cylon", function() {
this.Device = (function(klass) {
subclass(Device, klass);
function Device(opts) {
if (opts == null) {
@ -97,5 +89,3 @@
});
module.exports = Cylon.Device;
}).call(this);

View File

@ -2,38 +2,25 @@
* 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.
*/
(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; };
FS = require('fs');
var FS = require('fs');
var EventEmitter = require('events').EventEmitter;
var namespace = require('node-namespace');
EventEmitter = require('events').EventEmitter;
namespace("Cylon.IO", function() {
this.DigitalPin = (function(klass) {
subclass(DigitalPin, klass);
namespace = require('node-namespace');
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;
var GPIO_PATH = "/sys/class/gpio";
var GPIO_READ = "in";
var GPIO_WRITE = "out";
var HIGH = 1;
var LOW = 0;
function DigitalPin(opts) {
this.pinNum = opts.pin;
@ -198,5 +185,3 @@
})(EventEmitter);
});
}).call(this);

View File

@ -2,24 +2,18 @@
* driver
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(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; };
namespace = require('node-namespace');
require('./basestar');
var namespace = require('node-namespace');
namespace('Cylon', function() {
return this.Driver = (function(_super) {
__extends(Driver, _super);
namespace("Cylon", function() {
this.Driver = (function(klass) {
subclass(Driver, klass);
function Driver(opts) {
if (opts == null) {
@ -51,5 +45,3 @@
})(Cylon.Basestar);
});
}).call(this);

View File

@ -2,18 +2,15 @@
* port
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(function() {
'use strict';
var namespace;
namespace = require('node-namespace');
var namespace = require('node-namespace');
namespace('Cylon', function() {
namespace("Cylon", function() {
return this.Port = (function() {
function Port(data) {
this.self = this;
@ -57,5 +54,3 @@
});
module.exports = Cylon.Port;
}).call(this);

View File

@ -2,46 +2,30 @@
* robot
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(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('./basestar');
require("./connection");
require("./adaptor");
require("./device");
require("./driver");
require('./digital-pin');
var Async = require("async");
var EventEmitter = require('events').EventEmitter;
namespace = require('node-namespace');
var 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;
namespace("Cylon", function() {
this.Robot = (function(klass) {
subclass(Robot, klass);
function Robot(opts) {
var func, n, reserved;
@ -287,5 +271,3 @@
});
module.exports = Cylon.Robot;
}).call(this);

View File

@ -1,10 +1,7 @@
(function() {
'use strict';
var EventEmitter;
source("adaptor");
EventEmitter = require('events').EventEmitter;
var EventEmitter = require('events').EventEmitter;
describe("Adaptor", function() {
var adaptor, conn;
@ -13,6 +10,7 @@
name: 'TestAdaptor',
connection: conn
});
it("provides a 'connect' method that accepts a callback", function() {
var spy;
expect(adaptor.connect).to.be.a('function');
@ -20,8 +18,9 @@
adaptor.connect(function() {
return spy();
});
return spy.should.have.been.called;
spy.should.have.been.called;
});
it("tells the connection to emit the 'connect' event when connected", function() {
var spy;
spy = sinon.spy();
@ -29,23 +28,26 @@
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);
});
spy.should.have.been.called;
});
}).call(this);
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,12 +1,6 @@
(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');
EventEmitter = require('events').EventEmitter;
var EventEmitter = require('events').EventEmitter;
describe('Basestar', function() {
describe('constructor', function() {
@ -16,9 +10,11 @@
return instance.self.should.be.eql(instance);
});
});
return describe('#proxyMethods', function() {
describe('#proxyMethods', function() {
var ProxyClass, TestClass, methods;
methods = ['asString', 'toString', 'returnString'];
ProxyClass = (function() {
function ProxyClass() {}
@ -37,8 +33,9 @@
return ProxyClass;
})();
TestClass = (function(_super) {
__extends(TestClass, _super);
subclass(TestClass, _super);
function TestClass() {
this.testInstance = new ProxyClass;
@ -48,25 +45,26 @@
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]");
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]");
testclass.toString().should.be.equal("[object ProxyClass]");
});
return it('can alias methods with arguments', function() {
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");
testclass.returnString("testString").should.be.equal("testString");
});
});
});
}).call(this);