Refactor api, basestar & config to pure JS style

This commit is contained in:
deadprogram 2014-02-27 10:58:50 -08:00
parent 30228c7f3c
commit c28baa8eeb
3 changed files with 222 additions and 230 deletions

View File

@ -2,21 +2,17 @@
* api * api
* cylonjs.com * cylonjs.com
* *
* Copyright (c) 2013 The Hybrid Group * Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license. * Licensed under the Apache 2.0 license.
*/ */
"use strict";
(function() { var express = require('express.io');
'use strict'; var namespace = require('node-namespace');
var express, namespace;
express = require('express.io'); namespace("Cylon", function() {
this.ApiServer = (function() {
namespace = require('node-namespace');
namespace('Cylon', function() {
return this.ApiServer = (function() {
var master; var master;
master = null; master = null;
@ -62,6 +58,7 @@
ApiServer.prototype.configureRoutes = function() { ApiServer.prototype.configureRoutes = function() {
var _this = this; var _this = this;
this.server.get("/robots", function(req, res) { this.server.get("/robots", function(req, res) {
var robot; var robot;
return res.json((function() { return res.json((function() {
@ -75,16 +72,19 @@
return _results; return _results;
})()); })());
}); });
this.server.get("/robots/:robotname", function(req, res) { this.server.get("/robots/:robotname", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) { return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data()); return res.json(err ? err : robot.data());
}); });
}); });
this.server.get("/robots/:robotname/commands", function(req, res) { this.server.get("/robots/:robotname/commands", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) { return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().commands); return res.json(err ? err : robot.data().commands);
}); });
}); });
this.server.all("/robots/:robotname/commands/:commandname", function(req, res) { this.server.all("/robots/:robotname/commands/:commandname", function(req, res) {
var params; var params;
params = _this.parseCommandParams(req); params = _this.parseCommandParams(req);
@ -99,11 +99,13 @@
}); });
}); });
}); });
this.server.get("/robots/:robotname/devices", function(req, res) { this.server.get("/robots/:robotname/devices", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) { return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().devices); return res.json(err ? err : robot.data().devices);
}); });
}); });
this.server.get("/robots/:robotname/devices/:devicename", function(req, res) { this.server.get("/robots/:robotname/devices/:devicename", function(req, res) {
var devicename, robotname, _ref; var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1]; _ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];
@ -111,6 +113,7 @@
return res.json(err ? err : device.data()); return res.json(err ? err : device.data());
}); });
}); });
this.server.get("/robots/:robotname/devices/:devicename/commands", function(req, res) { this.server.get("/robots/:robotname/devices/:devicename/commands", function(req, res) {
var devicename, robotname, _ref; var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1]; _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); return res.json(err ? err : device.data().commands);
}); });
}); });
this.server.all("/robots/:robot/devices/:device/commands/:commandname", function(req, res) { this.server.all("/robots/:robot/devices/:device/commands/:commandname", function(req, res) {
var commandname, devicename, params, robotname; var commandname, devicename, params, robotname;
params = [req.params.robot, req.params.device, req.params.commandname]; params = [req.params.robot, req.params.device, req.params.commandname];
@ -134,11 +138,13 @@
}); });
}); });
}); });
this.server.get("/robots/:robotname/connections", function(req, res) { this.server.get("/robots/:robotname/connections", function(req, res) {
return master.findRobot(req.params.robotname, function(err, robot) { return master.findRobot(req.params.robotname, function(err, robot) {
return res.json(err ? err : robot.data().connections); return res.json(err ? err : robot.data().connections);
}); });
}); });
this.server.get("/robots/:robot/connections/:connection", function(req, res) { this.server.get("/robots/:robot/connections/:connection", function(req, res) {
var connectionname, robotname, _ref; var connectionname, robotname, _ref;
_ref = [req.params.robot, req.params.connection], robotname = _ref[0], connectionname = _ref[1]; _ref = [req.params.robot, req.params.connection], robotname = _ref[0], connectionname = _ref[1];
@ -146,9 +152,11 @@
return res.json(err ? err : connection.data()); return res.json(err ? err : connection.data());
}); });
}); });
this.server.get("/robots/:robotname/devices/:devicename/events", function(req, res) { this.server.get("/robots/:robotname/devices/:devicename/events", function(req, res) {
return req.io.route('events'); return req.io.route('events');
}); });
return this.server.io.route('events', function(req) { return this.server.io.route('events', function(req) {
var devicename, robotname, _ref; var devicename, robotname, _ref;
_ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1]; _ref = [req.params.robotname, req.params.devicename], robotname = _ref[0], devicename = _ref[1];
@ -171,5 +179,3 @@
}); });
module.exports = Cylon.ApiServer; module.exports = Cylon.ApiServer;
}).call(this);

View File

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

View File

@ -2,17 +2,15 @@
* cylon configuration loader * cylon configuration loader
* cylonjs.com * cylonjs.com
* *
* Copyright (c) 2013 The Hybrid Group * Copyright (c) 2013-2014 The Hybrid Group
* Licensed under the Apache 2.0 license. * Licensed under the Apache 2.0 license.
*/ */
'use strict';
(function() { var namespace = require('node-namespace');
var fetch, namespace;
namespace = require('node-namespace'); var fetch = function(variable, defaultValue) {
fetch = function(variable, defaultValue) {
if (defaultValue == null) { if (defaultValue == null) {
defaultValue = false; defaultValue = false;
} }
@ -28,5 +26,3 @@
}); });
module.exports = CylonConfig; module.exports = CylonConfig;
}).call(this);