cylon/dist/robot.js

176 lines
5.2 KiB
JavaScript
Raw Normal View History

2013-10-25 05:25:42 +08:00
/*
* robot
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(function() {
'use strict';
var Connection, Device, Robot,
2013-10-25 08:25:46 +08:00
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
2013-10-25 05:25:42 +08:00
require('./cylon');
Connection = require("./connection");
Device = require("./device");
module.exports = Robot = (function() {
2013-10-25 08:25:46 +08:00
var self;
2013-10-25 05:25:42 +08:00
self = Robot;
function Robot(opts) {
if (opts == null) {
opts = {};
}
2013-10-25 08:25:46 +08:00
this.registerDriver = __bind(this.registerDriver, this);
2013-10-25 05:25:42 +08:00
this.startDevices = __bind(this.startDevices, this);
this.startConnections = __bind(this.startConnections, this);
this.start = __bind(this.start, this);
2013-10-25 05:25:42 +08:00
this.initDevices = __bind(this.initDevices, this);
this.initConnections = __bind(this.initConnections, this);
2013-10-25 08:25:46 +08:00
this.robot = this;
2013-10-25 05:25:42 +08:00
this.name = opts.name || this.constructor.randomName();
this.master = opts.master;
this.connections = {};
this.devices = {};
2013-10-25 08:25:46 +08:00
this.adaptors = {};
this.drivers = {};
2013-10-25 05:25:42 +08:00
this.registerAdaptor("./loopback", "loopback");
2013-10-26 04:11:09 +08:00
this.registerDriver("./ping", "ping");
2013-10-25 05:25:42 +08:00
this.initConnections(opts.connection || opts.connections);
this.initDevices(opts.device || opts.devices);
this.work = opts.work || function() {
return Logger.info("No work yet");
};
}
Robot.randomName = function() {
return "Robot " + (Math.floor(Math.random() * 100000));
};
Robot.prototype.initConnections = function(connections) {
var connection, _i, _len, _results;
Logger.info("Initializing connections...");
if (connections == null) {
return;
}
connections = [].concat(connections);
_results = [];
for (_i = 0, _len = connections.length; _i < _len; _i++) {
connection = connections[_i];
Logger.info("Initializing connection '" + connection.name + "'...");
connection['robot'] = this;
_results.push(this.connections[connection.name] = new Connection(connection));
}
return _results;
};
Robot.prototype.initDevices = function(devices) {
var device, _i, _len, _results;
Logger.info("Initializing devices...");
if (devices == null) {
return;
}
devices = [].concat(devices);
_results = [];
for (_i = 0, _len = devices.length; _i < _len; _i++) {
device = devices[_i];
Logger.info("Initializing device '" + device.name + "'...");
device['robot'] = this;
_results.push(this.devices[device.name] = new Device(device));
}
return _results;
};
Robot.prototype.start = function() {
this.startConnections();
this.startDevices();
2013-10-25 08:25:46 +08:00
return this.work.call(this.robot, this.robot);
2013-10-25 05:25:42 +08:00
};
Robot.prototype.startConnections = function() {
var connection, n, _ref, _results;
Logger.info("Starting connections...");
_ref = this.connections;
_results = [];
for (n in _ref) {
connection = _ref[n];
Logger.info("Starting connection '" + connection.name + "'...");
_results.push(connection.connect());
2013-10-25 05:25:42 +08:00
}
return _results;
};
Robot.prototype.startDevices = function() {
var device, n, _ref, _results;
Logger.info("Starting devices...");
_ref = this.devices;
_results = [];
for (n in _ref) {
device = _ref[n];
Logger.info("Starting device '" + device.name + "'...");
device.start();
_results.push(this[device.name] = device);
2013-10-25 05:25:42 +08:00
}
return _results;
};
2013-10-25 08:25:46 +08:00
Robot.prototype.requireAdaptor = function(adaptorName, connection) {
if (this.robot.adaptors[adaptorName] != null) {
if (typeof this.robot.adaptors[adaptorName] === 'string') {
this.robot.adaptors[adaptorName] = require(this.robot.adaptors[adaptorName]).adaptor({
2013-10-25 05:25:42 +08:00
name: adaptorName,
connection: connection
});
}
} else {
2013-10-25 08:25:46 +08:00
require("cylon-" + adaptorName).register(this);
this.robot.adaptors[adaptorName] = require("cylon-" + adaptorName).adaptor({
2013-10-25 05:25:42 +08:00
name: adaptorName,
connection: connection
});
}
2013-10-25 08:25:46 +08:00
return this.robot.adaptors[adaptorName];
2013-10-25 05:25:42 +08:00
};
2013-10-25 08:25:46 +08:00
Robot.prototype.registerAdaptor = function(moduleName, adaptorName) {
if (this.adaptors[adaptorName] != null) {
2013-10-25 05:25:42 +08:00
return;
}
2013-10-25 08:25:46 +08:00
return this.adaptors[adaptorName] = moduleName;
2013-10-25 05:25:42 +08:00
};
2013-10-25 08:25:46 +08:00
Robot.prototype.requireDriver = function(driverName, device) {
if (this.robot.drivers[driverName] != null) {
if (typeof this.robot.drivers[driverName] === 'string') {
this.robot.drivers[driverName] = require(this.robot.drivers[driverName]).driver({
2013-10-25 05:25:42 +08:00
device: device
});
}
} else {
2013-10-25 08:25:46 +08:00
require("cylon-" + driverName).register(this);
this.robot.drivers[driverName] = require("cylon-" + driverName).driver({
2013-10-25 05:25:42 +08:00
device: device
});
}
2013-10-25 08:25:46 +08:00
return this.robot.drivers[driverName];
2013-10-25 05:25:42 +08:00
};
2013-10-25 08:25:46 +08:00
Robot.prototype.registerDriver = function(moduleName, driverName) {
if (this.drivers[driverName] != null) {
2013-10-25 05:25:42 +08:00
return;
}
2013-10-25 08:25:46 +08:00
return this.drivers[driverName] = moduleName;
2013-10-25 05:25:42 +08:00
};
return Robot;
2013-10-25 08:25:46 +08:00
})();
2013-10-25 05:25:42 +08:00
}).call(this);