cylon/lib/cylon.js

205 lines
5.0 KiB
JavaScript
Raw Normal View History

2013-10-25 05:25:42 +08:00
/*
* cylon
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(function() {
'use strict';
var Cylon,
2013-10-25 05:25:42 +08:00
__slice = [].slice,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
require('./config');
2013-10-25 05:25:42 +08:00
require('./utils');
require('./logger');
require('./driver');
require('./adaptor');
2013-10-25 05:25:42 +08:00
Logger.setup();
Cylon = (function() {
var Master, instance;
function Cylon() {}
instance = null;
Cylon.getInstance = function() {
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() {
2013-11-07 08:13:35 +08:00
var api, api_config, robots;
robots = [];
api = null;
2013-10-25 05:25:42 +08:00
2013-11-07 08:13:35 +08:00
api_config = {
host: '127.0.0.1',
port: '3000'
};
2013-10-25 05:25:42 +08:00
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();
});
2013-10-25 05:25:42 +08:00
}
Master.prototype.robot = function(opts) {
var Robot, robot;
Robot = require("./robot");
2013-10-25 05:25:42 +08:00
opts.master = this;
robot = new Robot(opts);
robots.push(robot);
return robot;
};
Master.prototype.robots = function() {
return robots;
};
2013-11-07 08:13:35 +08:00
Master.prototype.api = function(opts) {
2013-11-09 01:28:48 +08:00
if (opts == null) {
opts = {};
}
2013-11-07 08:13:35 +08:00
api_config.host = opts.host || "127.0.0.1";
api_config.port = opts.port || "3000";
return api_config;
};
2013-11-01 04:36:28 +08:00
Master.prototype.findRobot = function(name, callback) {
var bot, error, robot, _i, _len;
robot = null;
for (_i = 0, _len = robots.length; _i < _len; _i++) {
2013-11-01 04:36:28 +08:00
bot = robots[_i];
if (bot.name === name) {
robot = bot;
}
}
2013-11-01 04:36:28 +08:00
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 this.findRobot(robotid, function(err, robot) {
var device, error;
if (err) {
callback(err, robot);
}
2013-11-09 08:16:17 +08:00
device = null;
2013-11-01 04:36:28 +08:00
if (robot.devices[deviceid]) {
device = robot.devices[deviceid];
}
if (device == null) {
error = {
2013-11-09 08:16:17 +08:00
error: "No device found with the name " + deviceid + "."
2013-11-01 04:36:28 +08:00
};
}
if (callback) {
return callback(error, device);
} else {
return device;
}
});
};
Master.prototype.findRobotConnection = function(robotid, connid, callback) {
return this.findRobot(robotid, function(err, robot) {
var connection, error;
if (err) {
callback(err, robot);
}
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;
}
});
};
2013-10-25 05:25:42 +08:00
Master.prototype.start = function() {
var robot, _i, _len, _results;
this.startAPI();
2013-10-25 05:25:42 +08:00
_results = [];
for (_i = 0, _len = robots.length; _i < _len; _i++) {
robot = robots[_i];
_results.push(robot.start());
}
return _results;
};
Master.prototype.stop = function() {
var robot, _i, _len, _results;
_results = [];
for (_i = 0, _len = robots.length; _i < _len; _i++) {
robot = robots[_i];
_results.push(robot.stop());
}
return _results;
};
Master.prototype.startAPI = function() {
2014-01-11 03:48:39 +08:00
var server;
server = require('./api');
2013-11-07 08:13:35 +08:00
api_config.master = this.self;
2014-01-11 03:48:39 +08:00
return api != null ? api : api = new server(api_config);
};
2013-10-25 05:25:42 +08:00
return Master;
})();
return Cylon;
}).call(this);
module.exports = Cylon.getInstance();
}).call(this);