Refactor api, basestar & config to pure JS style
This commit is contained in:
parent
30228c7f3c
commit
c28baa8eeb
306
lib/api.js
306
lib/api.js
|
@ -2,174 +2,180 @@
|
||||||
* 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() {
|
||||||
|
var master;
|
||||||
|
|
||||||
namespace = require('node-namespace');
|
master = null;
|
||||||
|
|
||||||
namespace('Cylon', function() {
|
function ApiServer(opts) {
|
||||||
return this.ApiServer = (function() {
|
var _this = this;
|
||||||
var master;
|
if (opts == null) {
|
||||||
|
opts = {};
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
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) {
|
ApiServer.prototype.parseCommandParams = function(req) {
|
||||||
var param_container, params, v, _;
|
var param_container, params, v, _;
|
||||||
params = [];
|
params = [];
|
||||||
param_container = {};
|
param_container = {};
|
||||||
if (req.method === 'GET' || Object.keys(req.query).length > 0) {
|
if (req.method === 'GET' || Object.keys(req.query).length > 0) {
|
||||||
param_container = req.query;
|
param_container = req.query;
|
||||||
} else if (typeof req.body === 'object') {
|
} else if (typeof req.body === 'object') {
|
||||||
param_container = req.body;
|
param_container = req.body;
|
||||||
}
|
}
|
||||||
for (_ in param_container) {
|
for (_ in param_container) {
|
||||||
v = param_container[_];
|
v = param_container[_];
|
||||||
params.push(v);
|
params.push(v);
|
||||||
}
|
}
|
||||||
return params;
|
return params;
|
||||||
};
|
};
|
||||||
|
|
||||||
ApiServer.prototype.configureRoutes = function() {
|
ApiServer.prototype.configureRoutes = function() {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
this.server.get("/robots", function(req, res) {
|
|
||||||
var robot;
|
this.server.get("/robots", function(req, res) {
|
||||||
return res.json((function() {
|
var robot;
|
||||||
var _i, _len, _ref, _results;
|
return res.json((function() {
|
||||||
_ref = master.robots();
|
var _i, _len, _ref, _results;
|
||||||
_results = [];
|
_ref = master.robots();
|
||||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
_results = [];
|
||||||
robot = _ref[_i];
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||||
_results.push(robot.data());
|
robot = _ref[_i];
|
||||||
}
|
_results.push(robot.data());
|
||||||
return _results;
|
}
|
||||||
})());
|
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);
|
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) {
|
||||||
var result;
|
return res.json(err ? err : robot.data().connections);
|
||||||
if (err) {
|
});
|
||||||
return res.json(err);
|
});
|
||||||
}
|
|
||||||
result = robot[req.params.commandname].apply(robot, params);
|
this.server.get("/robots/:robot/connections/:connection", function(req, res) {
|
||||||
return res.json({
|
var connectionname, robotname, _ref;
|
||||||
result: result
|
_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;
|
module.exports = Cylon.ApiServer;
|
||||||
|
|
||||||
}).call(this);
|
|
||||||
|
|
108
lib/basestar.js
108
lib/basestar.js
|
@ -2,76 +2,66 @@
|
||||||
* 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() {
|
require('./utils');
|
||||||
'use strict';
|
var namespace = require('node-namespace');
|
||||||
var EventEmitter, namespace,
|
var EventEmitter = require('events').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; },
|
|
||||||
__slice = [].slice;
|
|
||||||
|
|
||||||
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;
|
Basestar.prototype.proxyMethods = function(methods, target, source, force) {
|
||||||
|
if (force == null) {
|
||||||
namespace('Cylon', function() {
|
force = false;
|
||||||
return this.Basestar = (function(_super) {
|
|
||||||
__extends(Basestar, _super);
|
|
||||||
|
|
||||||
function Basestar(opts) {
|
|
||||||
this.self = this;
|
|
||||||
}
|
}
|
||||||
|
return proxyFunctionsToObject(methods, target, source, force);
|
||||||
|
};
|
||||||
|
|
||||||
Basestar.prototype.proxyMethods = function(methods, target, source, force) {
|
Basestar.prototype.defineEvent = function(opts) {
|
||||||
if (force == null) {
|
var sendUpdate, targetEventName,
|
||||||
force = false;
|
_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) {
|
Basestar.prototype.defineAdaptorEvent = function(opts) {
|
||||||
var sendUpdate, targetEventName,
|
opts['source'] = this.connector;
|
||||||
_this = this;
|
opts['target'] = this.connection;
|
||||||
targetEventName = opts.targetEventName || opts.eventName;
|
if (opts['sendUpdate'] == null) {
|
||||||
sendUpdate = opts.sendUpdate || false;
|
opts['sendUpdate'] = false;
|
||||||
opts.source.on(opts.eventName, function() {
|
}
|
||||||
var args, _ref, _ref1;
|
return this.defineEvent(opts);
|
||||||
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) {
|
Basestar.prototype.defineDriverEvent = function(opts) {
|
||||||
opts['source'] = this.connector;
|
opts['source'] = this.connection;
|
||||||
opts['target'] = this.connection;
|
opts['target'] = this.device;
|
||||||
if (opts['sendUpdate'] == null) {
|
if (opts['sendUpdate'] == null) {
|
||||||
opts['sendUpdate'] = false;
|
opts['sendUpdate'] = true;
|
||||||
}
|
}
|
||||||
return this.defineEvent(opts);
|
return this.defineEvent(opts);
|
||||||
};
|
};
|
||||||
|
|
||||||
Basestar.prototype.defineDriverEvent = function(opts) {
|
return Basestar;
|
||||||
opts['source'] = this.connection;
|
|
||||||
opts['target'] = this.device;
|
|
||||||
if (opts['sendUpdate'] == null) {
|
|
||||||
opts['sendUpdate'] = true;
|
|
||||||
}
|
|
||||||
return this.defineEvent(opts);
|
|
||||||
};
|
|
||||||
|
|
||||||
return Basestar;
|
})(EventEmitter);
|
||||||
|
});
|
||||||
})(EventEmitter);
|
|
||||||
});
|
|
||||||
|
|
||||||
}).call(this);
|
|
||||||
|
|
|
@ -2,31 +2,27 @@
|
||||||
* 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) {
|
||||||
|
if (defaultValue == null) {
|
||||||
|
defaultValue = false;
|
||||||
|
}
|
||||||
|
if (process.env[variable] != null) {
|
||||||
|
return process.env[variable];
|
||||||
|
} else {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
fetch = function(variable, defaultValue) {
|
namespace('CylonConfig', function() {
|
||||||
if (defaultValue == null) {
|
return this.testing_mode = fetch("CYLON_TEST", false);
|
||||||
defaultValue = false;
|
});
|
||||||
}
|
|
||||||
if (process.env[variable] != null) {
|
|
||||||
return process.env[variable];
|
|
||||||
} else {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace('CylonConfig', function() {
|
module.exports = CylonConfig;
|
||||||
return this.testing_mode = fetch("CYLON_TEST", false);
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = CylonConfig;
|
|
||||||
|
|
||||||
}).call(this);
|
|
||||||
|
|
Loading…
Reference in New Issue