Refactor cylon.js
This commit is contained in:
parent
bbf4e413d8
commit
df82352a96
353
lib/cylon.js
353
lib/cylon.js
|
@ -6,199 +6,222 @@
|
||||||
* Licensed under the Apache 2.0 license.
|
* Licensed under the Apache 2.0 license.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
(function() {
|
|
||||||
'use strict';
|
|
||||||
var Cylon,
|
|
||||||
__slice = [].slice,
|
|
||||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
|
||||||
|
|
||||||
require('./config');
|
require('./config');
|
||||||
|
require('./utils');
|
||||||
|
require('./logger');
|
||||||
|
require('./driver');
|
||||||
|
require('./adaptor');
|
||||||
|
|
||||||
require('./utils');
|
Logger.setup();
|
||||||
|
|
||||||
require('./logger');
|
// Cylon is the global namespace for the project, and also in charge of
|
||||||
|
// maintaining the Master singleton that controls all the robots.
|
||||||
|
var Cylon = (function() {
|
||||||
|
function Cylon() {}
|
||||||
|
|
||||||
require('./driver');
|
var instance = null;
|
||||||
|
|
||||||
require('./adaptor');
|
|
||||||
|
|
||||||
Logger.setup();
|
// Public: Fetches singleton instance of Master, or creates a new one if it
|
||||||
|
// doesn't already exist
|
||||||
|
//
|
||||||
|
// Returns a Master instance
|
||||||
|
Cylon.getInstance = function() {
|
||||||
|
return new Master();
|
||||||
|
};
|
||||||
|
|
||||||
Cylon = (function() {
|
var bind = function(fn, me) {
|
||||||
var Master, instance;
|
return function() { return fn.apply(me, arguments); }
|
||||||
|
}
|
||||||
|
|
||||||
function Cylon() {}
|
// The Master class is our puppeteer that manages all the robots, as well as
|
||||||
|
// starting them and the API.
|
||||||
|
var Master = (function() {
|
||||||
|
var robots = [],
|
||||||
|
api = null,
|
||||||
|
api_config = { host: '127.0.0.1', port: '3000' };
|
||||||
|
|
||||||
instance = null;
|
// Public: Creates a new Master
|
||||||
|
//
|
||||||
|
// Returns a Master instance
|
||||||
|
function Master() {
|
||||||
|
this.robot = bind(this.robot, this);
|
||||||
|
|
||||||
Cylon.getInstance = function() {
|
this.self = this;
|
||||||
var args;
|
|
||||||
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
|
||||||
return instance != null ? instance : instance = (function(func, args, ctor) {
|
|
||||||
ctor.prototype = func.prototype;
|
|
||||||
var child = new ctor, result = func.apply(child, args);
|
|
||||||
return Object(result) === result ? result : child;
|
|
||||||
})(Master, args, function(){});
|
|
||||||
};
|
|
||||||
|
|
||||||
Master = (function() {
|
if (process.platform === "win32") {
|
||||||
var api, api_config, robots;
|
var readline = require("readline");
|
||||||
|
|
||||||
robots = [];
|
var rl = readline.createInterface({
|
||||||
|
input: process.stdin,
|
||||||
api = null;
|
output: process.stdout
|
||||||
|
|
||||||
api_config = {
|
|
||||||
host: '127.0.0.1',
|
|
||||||
port: '3000'
|
|
||||||
};
|
|
||||||
|
|
||||||
function Master() {
|
|
||||||
this.robot = __bind(this.robot, this);
|
|
||||||
var readLine, rl;
|
|
||||||
this.self = this;
|
|
||||||
if (process.platform === "win32") {
|
|
||||||
readLine = require("readline");
|
|
||||||
rl = readLine.createInterface({
|
|
||||||
input: process.stdin,
|
|
||||||
output: process.stdout
|
|
||||||
});
|
|
||||||
rl.on("SIGINT", function() {
|
|
||||||
return process.emit("SIGINT");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
process.on("SIGINT", function() {
|
|
||||||
Cylon.getInstance().stop();
|
|
||||||
return process.kill();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
rl.on("SIGINT", function() { return process.emit("SIGINT"); });
|
||||||
}
|
}
|
||||||
|
|
||||||
Master.prototype.robot = function(opts) {
|
process.on("SIGINT", function() {
|
||||||
var Robot, robot;
|
Cylon.getInstance().stop();
|
||||||
Robot = require("./robot");
|
process.kill();
|
||||||
opts.master = this;
|
});
|
||||||
robot = new Robot(opts);
|
}
|
||||||
robots.push(robot);
|
|
||||||
return robot;
|
|
||||||
};
|
|
||||||
|
|
||||||
Master.prototype.robots = function() {
|
// Public: Creates a new Robot
|
||||||
return robots;
|
//
|
||||||
};
|
// opts - hash of Robot attributes
|
||||||
|
//
|
||||||
|
// Returns a shiny new Robot
|
||||||
|
// Examples:
|
||||||
|
// Cylon.robot
|
||||||
|
// connection: { name: 'arduino', adaptor: 'firmata' }
|
||||||
|
// device: { name: 'led', driver: 'led', pin: 13 }
|
||||||
|
//
|
||||||
|
// work: (me) ->
|
||||||
|
// me.led.toggle()
|
||||||
|
Master.prototype.robot = function(opts) {
|
||||||
|
var Robot = require("./robot");
|
||||||
|
opts.master = this;
|
||||||
|
|
||||||
Master.prototype.api = function(opts) {
|
var robot = new Robot(opts);
|
||||||
if (opts == null) {
|
|
||||||
opts = {};
|
robots.push(robot);
|
||||||
|
return robot;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Public: Returns all Robots the Master knows about
|
||||||
|
//
|
||||||
|
// Returns an array of all Robot instances
|
||||||
|
Master.prototype.robots = function() {
|
||||||
|
return robots;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Public: Configures the API host and port based on passed options
|
||||||
|
//
|
||||||
|
// opts - object containing API options
|
||||||
|
// host - host address API should serve from
|
||||||
|
// port - port API should listen for requests on
|
||||||
|
//
|
||||||
|
// Returns the API configuration
|
||||||
|
Master.prototype.api = function(opts) {
|
||||||
|
if (opts == null) { opts = {}; }
|
||||||
|
|
||||||
|
api_config.host = opts.host || "127.0.0.1";
|
||||||
|
api_config.port = opts.port || "3000";
|
||||||
|
|
||||||
|
return api_config;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Public: Finds a particular robot by name
|
||||||
|
//
|
||||||
|
// name - name of the robot to find
|
||||||
|
// callback - optional callback to be executed
|
||||||
|
//
|
||||||
|
// Returns the found Robot or result of the callback if it's supplied
|
||||||
|
Master.prototype.findRobot = function(name, callback) {
|
||||||
|
var error,
|
||||||
|
robot = null;
|
||||||
|
|
||||||
|
for (var i = 0; i < robots.length; i++) {
|
||||||
|
var bot = robots[i];
|
||||||
|
if (bot.name === name) { robot = bot; }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (robot == null) {
|
||||||
|
error = { error: "No Robot found with the name " + name };
|
||||||
|
}
|
||||||
|
|
||||||
|
return callback ? callback(error, robot) : robot;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Public: Finds a particular Robot's device by name
|
||||||
|
//
|
||||||
|
// robotid - name of the robot to find
|
||||||
|
// deviceid - name of the device to find
|
||||||
|
// callback - optional callback to be executed
|
||||||
|
//
|
||||||
|
// Returns the found Device or result of the callback if it's supplied
|
||||||
|
Master.prototype.findRobotDevice = function(robotid, deviceid, callback) {
|
||||||
|
return this.findRobot(robotid, function(err, robot) {
|
||||||
|
var error,
|
||||||
|
device = null;
|
||||||
|
|
||||||
|
if (err) { return callback(err, robot); }
|
||||||
|
|
||||||
|
if (robot.devices[deviceid]) { device = robot.devices[deviceid]; }
|
||||||
|
|
||||||
|
if (device == null) {
|
||||||
|
error = { error: "No device found with the name " + deviceid + "." };
|
||||||
}
|
}
|
||||||
api_config.host = opts.host || "127.0.0.1";
|
|
||||||
api_config.port = opts.port || "3000";
|
|
||||||
return api_config;
|
|
||||||
};
|
|
||||||
|
|
||||||
Master.prototype.findRobot = function(name, callback) {
|
return callback ? callback(error, device) : device;
|
||||||
var bot, error, robot, _i, _len;
|
});
|
||||||
robot = null;
|
};
|
||||||
for (_i = 0, _len = robots.length; _i < _len; _i++) {
|
|
||||||
bot = robots[_i];
|
// Public: Finds a particular Robot's connection by name
|
||||||
if (bot.name === name) {
|
//
|
||||||
robot = bot;
|
// robotid - name of the robot to find
|
||||||
}
|
// connid - name of the device to find
|
||||||
|
// callback - optional callback to be executed
|
||||||
|
//
|
||||||
|
// Returns the found Connection or result of the callback if it's supplied
|
||||||
|
Master.prototype.findRobotConnection = function(robotid, connid, callback) {
|
||||||
|
return this.findRobot(robotid, function(err, robot) {
|
||||||
|
var error,
|
||||||
|
connection = null;
|
||||||
|
|
||||||
|
if (err) { return callback(err, robot); }
|
||||||
|
|
||||||
|
if (robot.connections[connid]) { connection = robot.connections[connid]; }
|
||||||
|
|
||||||
|
if (connection == null) {
|
||||||
|
error = { error: "No connection found with the name " + connid + "." };
|
||||||
}
|
}
|
||||||
if (robot == null) {
|
|
||||||
error = {
|
|
||||||
error: "No Robot found with the name " + name
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (callback) {
|
|
||||||
return callback(error, robot);
|
|
||||||
} else {
|
|
||||||
return robot;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Master.prototype.findRobotDevice = function(robotid, deviceid, callback) {
|
return callback ? callback(error, connection) : connection;
|
||||||
return this.findRobot(robotid, function(err, robot) {
|
});
|
||||||
var device, error;
|
};
|
||||||
if (err) {
|
|
||||||
callback(err, robot);
|
|
||||||
}
|
|
||||||
device = null;
|
|
||||||
if (robot.devices[deviceid]) {
|
|
||||||
device = robot.devices[deviceid];
|
|
||||||
}
|
|
||||||
if (device == null) {
|
|
||||||
error = {
|
|
||||||
error: "No device found with the name " + deviceid + "."
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (callback) {
|
|
||||||
return callback(error, device);
|
|
||||||
} else {
|
|
||||||
return device;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Master.prototype.findRobotConnection = function(robotid, connid, callback) {
|
// Public: Starts up the API and the robots
|
||||||
return this.findRobot(robotid, function(err, robot) {
|
//
|
||||||
var connection, error;
|
// Returns nothing
|
||||||
if (err) {
|
Master.prototype.start = function() {
|
||||||
callback(err, robot);
|
this.startAPI();
|
||||||
}
|
|
||||||
connection = null;
|
|
||||||
if (robot.connections[connid]) {
|
|
||||||
connection = robot.connections[connid];
|
|
||||||
}
|
|
||||||
if (connection == null) {
|
|
||||||
error = {
|
|
||||||
error: "No connection found with the name " + connid + "."
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (callback) {
|
|
||||||
return callback(error, connection);
|
|
||||||
} else {
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Master.prototype.start = function() {
|
for (var i = 0; i < robots.length; i++) {
|
||||||
var robot, _i, _len, _results;
|
var robot = robots[i];
|
||||||
this.startAPI();
|
robot.start();
|
||||||
_results = [];
|
}
|
||||||
for (_i = 0, _len = robots.length; _i < _len; _i++) {
|
};
|
||||||
robot = robots[_i];
|
|
||||||
_results.push(robot.start());
|
|
||||||
}
|
|
||||||
return _results;
|
|
||||||
};
|
|
||||||
|
|
||||||
Master.prototype.stop = function() {
|
// Public: Stops the API and the robots
|
||||||
var robot, _i, _len, _results;
|
//
|
||||||
_results = [];
|
// Returns nothing
|
||||||
for (_i = 0, _len = robots.length; _i < _len; _i++) {
|
Master.prototype.stop = function() {
|
||||||
robot = robots[_i];
|
for (var i = 0; i < robots.length; i++) {
|
||||||
_results.push(robot.stop());
|
var robot = robots[i];
|
||||||
}
|
robot.stop();
|
||||||
return _results;
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Master.prototype.startAPI = function() {
|
// Creates a new instance of the Cylon API server, or returns the
|
||||||
var server;
|
// already-existing one.
|
||||||
server = require('./api');
|
//
|
||||||
api_config.master = this.self;
|
// Returns an Cylon.ApiServer instance
|
||||||
return api != null ? api : api = new server(api_config);
|
Master.prototype.startAPI = function() {
|
||||||
};
|
var Server = require('./api');
|
||||||
|
api_config.master = this.self;
|
||||||
|
return api != null ? api : api = new Server(api_config);
|
||||||
|
};
|
||||||
|
|
||||||
return Master;
|
return Master;
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
return Cylon;
|
return Cylon;
|
||||||
|
|
||||||
}).call(this);
|
|
||||||
|
|
||||||
module.exports = Cylon.getInstance();
|
|
||||||
|
|
||||||
}).call(this);
|
}).call(this);
|
||||||
|
|
||||||
|
module.exports = Cylon.getInstance();
|
||||||
|
|
Loading…
Reference in New Issue