2013-10-25 05:25:42 +08:00
|
|
|
/*
|
|
|
|
* cylon
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013 The Hybrid Group
|
|
|
|
* Licensed under the Apache 2.0 license.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 05:31:54 +08:00
|
|
|
"use strict";
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-06-13 06:31:49 +08:00
|
|
|
var Async = require('async');
|
|
|
|
|
2014-05-14 10:44:40 +08:00
|
|
|
var Logger = require('./logger'),
|
|
|
|
Robot = require('./robot'),
|
2014-09-04 06:04:36 +08:00
|
|
|
Config = require('./config'),
|
2014-05-14 10:20:54 +08:00
|
|
|
Utils = require('./utils');
|
|
|
|
|
|
|
|
var Cylon = module.exports = {
|
2014-05-14 10:44:40 +08:00
|
|
|
Logger: Logger,
|
2014-05-14 10:20:54 +08:00
|
|
|
Driver: require('./driver'),
|
|
|
|
Adaptor: require('./adaptor'),
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils: Utils,
|
2014-05-14 10:20:54 +08:00
|
|
|
|
|
|
|
IO: {
|
2014-06-03 04:45:42 +08:00
|
|
|
DigitalPin: require('./io/digital-pin'),
|
2014-06-03 07:02:02 +08:00
|
|
|
Utils: require('./io/utils')
|
2014-05-14 10:20:54 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
api_instance: null,
|
|
|
|
|
2014-07-16 03:27:16 +08:00
|
|
|
robots: {},
|
|
|
|
commands: {}
|
2014-05-14 10:20:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Creates a new Robot
|
|
|
|
//
|
|
|
|
// 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()
|
|
|
|
Cylon.robot = function robot(opts) {
|
2014-09-05 05:51:44 +08:00
|
|
|
// check if a robot with the same name exists already
|
|
|
|
if (opts.name && this.robots[opts.name]) {
|
|
|
|
var original = opts.name;
|
|
|
|
opts.name = Utils.makeUnique(original, Object.keys(this.robots));
|
|
|
|
Logger.warn("Robot names must be unique. Renaming '" + original + "' to '" + opts.name + "'");
|
|
|
|
}
|
|
|
|
|
2014-05-14 10:20:54 +08:00
|
|
|
var robot = new Robot(opts);
|
2014-06-07 02:36:22 +08:00
|
|
|
this.robots[robot.name] = robot;
|
2014-05-14 10:20:54 +08:00
|
|
|
return robot;
|
|
|
|
};
|
|
|
|
|
2014-06-05 02:37:15 +08:00
|
|
|
// Public: Creates a new API based on passed options
|
2014-05-14 10:20:54 +08:00
|
|
|
//
|
2014-06-05 02:37:15 +08:00
|
|
|
// Returns nothing
|
2014-09-20 02:59:11 +08:00
|
|
|
Cylon.api = function api() {
|
2014-06-19 05:44:46 +08:00
|
|
|
var API = require('./api');
|
2014-02-28 05:31:54 +08:00
|
|
|
|
2014-09-20 02:59:11 +08:00
|
|
|
var config = Utils.fetch(Config, 'api', {});
|
|
|
|
|
|
|
|
this.api_instance = new API(config);
|
2014-06-05 02:37:15 +08:00
|
|
|
this.api_instance.listen();
|
2014-05-14 10:20:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Starts up the API and the robots
|
|
|
|
//
|
|
|
|
// Returns nothing
|
|
|
|
Cylon.start = function start() {
|
2014-10-04 15:56:08 +08:00
|
|
|
var starters = [];
|
2014-06-07 02:36:22 +08:00
|
|
|
for (var bot in this.robots) {
|
2014-10-04 15:56:08 +08:00
|
|
|
starters.push(this.robots[bot].start);
|
2014-10-04 14:07:07 +08:00
|
|
|
}
|
2014-10-04 15:56:08 +08:00
|
|
|
Async.parallel(starters, function(err, results) {
|
|
|
|
var mode = Utils.fetch(Config, 'workMode', 'async');
|
|
|
|
if (mode === 'sync') {
|
|
|
|
for (var bot in this.robots) {
|
|
|
|
this.robots[bot].startWork();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2014-05-14 10:20:54 +08:00
|
|
|
};
|
|
|
|
|
2014-09-04 06:04:36 +08:00
|
|
|
// Public: Sets the internal configuration, based on passed options
|
|
|
|
//
|
|
|
|
// opts - object containing configuration key/value pairs
|
|
|
|
//
|
2014-09-16 05:46:24 +08:00
|
|
|
// Returns the current config
|
|
|
|
Cylon.config = function(opts) {
|
2014-10-29 06:53:03 +08:00
|
|
|
var loggingChanged = (opts.logging && Config.logging !== opts.logging);
|
|
|
|
|
2014-09-16 05:46:24 +08:00
|
|
|
if (opts && typeof(opts) === 'object' && !Array.isArray(opts)) {
|
|
|
|
for (var o in opts) {
|
|
|
|
Config[o] = opts[o];
|
|
|
|
}
|
2014-09-04 06:04:36 +08:00
|
|
|
}
|
|
|
|
|
2014-10-29 06:53:03 +08:00
|
|
|
if (loggingChanged) {
|
|
|
|
Logger.setup();
|
|
|
|
}
|
|
|
|
|
2014-09-04 06:04:36 +08:00
|
|
|
return Config;
|
|
|
|
};
|
|
|
|
|
2014-05-14 10:20:54 +08:00
|
|
|
// Public: Halts the API and the robots
|
|
|
|
//
|
2014-06-13 06:31:49 +08:00
|
|
|
// callback - callback to be triggered when Cylon is ready to shutdown
|
|
|
|
//
|
2014-05-14 10:20:54 +08:00
|
|
|
// Returns nothing
|
2014-06-13 06:31:49 +08:00
|
|
|
Cylon.halt = function halt(callback) {
|
2014-09-05 02:14:47 +08:00
|
|
|
callback = callback || function() {}
|
2014-09-04 06:39:54 +08:00
|
|
|
// if robots can't shut down quickly enough, forcefully self-terminate
|
2014-10-01 02:53:53 +08:00
|
|
|
var timeout = Config.haltTimeout || 3000
|
2014-09-04 06:39:54 +08:00
|
|
|
Utils.after(timeout, callback);
|
2014-05-14 10:20:54 +08:00
|
|
|
|
2014-06-13 06:31:49 +08:00
|
|
|
var fns = [];
|
2014-05-14 10:20:54 +08:00
|
|
|
|
2014-06-07 02:36:22 +08:00
|
|
|
for (var bot in this.robots) {
|
2014-06-13 06:31:49 +08:00
|
|
|
var robot = this.robots[bot];
|
|
|
|
fns.push(robot.halt.bind(robot));
|
2014-05-14 10:20:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 06:31:49 +08:00
|
|
|
Async.parallel(fns, callback);
|
2014-05-14 10:20:54 +08:00
|
|
|
};
|
|
|
|
|
2014-07-16 03:27:16 +08:00
|
|
|
Cylon.toJSON = function() {
|
|
|
|
var robots = [];
|
|
|
|
|
|
|
|
for (var bot in this.robots) {
|
|
|
|
robots.push(this.robots[bot]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
robots: robots,
|
|
|
|
commands: Object.keys(this.commands)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-14 10:20:54 +08:00
|
|
|
if (process.platform === "win32") {
|
|
|
|
var readline = require("readline"),
|
|
|
|
io = { input: process.stdin, output: process.stdout };
|
|
|
|
|
|
|
|
readline.createInterface(io).on("SIGINT", function() {
|
|
|
|
process.emit("SIGINT");
|
|
|
|
});
|
2014-06-17 04:09:13 +08:00
|
|
|
}
|
2014-05-14 10:20:54 +08:00
|
|
|
|
|
|
|
process.on("SIGINT", function() {
|
2014-06-13 06:31:49 +08:00
|
|
|
Cylon.halt(function() {
|
|
|
|
process.kill(process.pid);
|
|
|
|
});
|
2014-05-14 10:20:54 +08:00
|
|
|
});
|