Begin JSDoc-ing core
This commit is contained in:
parent
80a09d55da
commit
6cccc41aff
158
lib/robot.js
158
lib/robot.js
|
@ -17,15 +17,17 @@ var initializer = require("./initializer"),
|
||||||
var Async = require("async"),
|
var Async = require("async"),
|
||||||
EventEmitter = require("events").EventEmitter;
|
EventEmitter = require("events").EventEmitter;
|
||||||
|
|
||||||
// Public: Creates a new Robot
|
/**
|
||||||
//
|
* Creates a new Robot instance based on provided options
|
||||||
// opts - object containing Robot options
|
*
|
||||||
// name - optional, string name of the robot
|
* @constructor
|
||||||
// connection/connections - object connections to connect to
|
* @param {Object} opts object with Robot options
|
||||||
// device/devices - object devices to connect to
|
* @param {String} [name] the name the robot should have
|
||||||
// work - work to be performed when the Robot is started
|
* @param {Object} [connections] object containing connection info for the Robot
|
||||||
//
|
* @param {Object} [devices] object containing device information for the Robot
|
||||||
// Returns a new Robot
|
* @param {Function} [work] a function the Robot will run when started
|
||||||
|
* @returns {Robot} new Robot instance
|
||||||
|
*/
|
||||||
var Robot = module.exports = function Robot(opts) {
|
var Robot = module.exports = function Robot(opts) {
|
||||||
Utils.classCallCheck(this, Robot);
|
Utils.classCallCheck(this, Robot);
|
||||||
|
|
||||||
|
@ -98,9 +100,12 @@ var Robot = module.exports = function Robot(opts) {
|
||||||
|
|
||||||
Utils.subclass(Robot, EventEmitter);
|
Utils.subclass(Robot, EventEmitter);
|
||||||
|
|
||||||
// Public: Generates a random name for a Robot.
|
/**
|
||||||
//
|
* Generates a random name for a Robot.
|
||||||
// Returns a string name
|
*
|
||||||
|
* @todo should perhaps just increment a counter as a global value?
|
||||||
|
* @returns {String} a robot name
|
||||||
|
*/
|
||||||
Robot.randomName = function() {
|
Robot.randomName = function() {
|
||||||
return "Robot " + (Math.floor(Math.random() * 100000));
|
return "Robot " + (Math.floor(Math.random() * 100000));
|
||||||
};
|
};
|
||||||
|
@ -108,6 +113,12 @@ Robot.randomName = function() {
|
||||||
// Public: Expresses the Robot in a JSON-serializable format
|
// Public: Expresses the Robot in a JSON-serializable format
|
||||||
//
|
//
|
||||||
// Returns an Object containing Robot data
|
// Returns an Object containing Robot data
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Condenses information on a Robot to a JSON-serializable format
|
||||||
|
*
|
||||||
|
* @return {Object} serializable information on the Robot
|
||||||
|
*/
|
||||||
Robot.prototype.toJSON = function() {
|
Robot.prototype.toJSON = function() {
|
||||||
return {
|
return {
|
||||||
name: this.name,
|
name: this.name,
|
||||||
|
@ -118,6 +129,13 @@ Robot.prototype.toJSON = function() {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new Connection to the Robot with the provided name and details.
|
||||||
|
*
|
||||||
|
* @param {String} name string name for the Connection to use
|
||||||
|
* @param {Object} conn options for the Connection initializer
|
||||||
|
* @return {Object} the robot
|
||||||
|
*/
|
||||||
Robot.prototype.connection = function(name, conn) {
|
Robot.prototype.connection = function(name, conn) {
|
||||||
conn.robot = this;
|
conn.robot = this;
|
||||||
conn.name = name;
|
conn.name = name;
|
||||||
|
@ -138,11 +156,12 @@ Robot.prototype.connection = function(name, conn) {
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Initializes all vars for robot
|
/**
|
||||||
//
|
* Initializes all values for a new Robot.
|
||||||
// opts - options array passed to constructor
|
*
|
||||||
//
|
* @param {Object} opts object passed to Robot constructor
|
||||||
// Returns null
|
* @return {void}
|
||||||
|
*/
|
||||||
Robot.prototype.initRobot = function(opts) {
|
Robot.prototype.initRobot = function(opts) {
|
||||||
this.name = opts.name || Robot.randomName();
|
this.name = opts.name || Robot.randomName();
|
||||||
this.connections = {};
|
this.connections = {};
|
||||||
|
@ -158,9 +177,12 @@ Robot.prototype.initRobot = function(opts) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Checks options for bad Cylon syntax
|
/**
|
||||||
//
|
* Checks provided options object for invalid Robot details
|
||||||
// Returns nothing
|
*
|
||||||
|
* @param {Object} opts options object to check for errors
|
||||||
|
* @return {void}
|
||||||
|
*/
|
||||||
Robot.prototype.checkForBadSyntax = function(opts) {
|
Robot.prototype.checkForBadSyntax = function(opts) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
@ -186,11 +208,12 @@ Robot.prototype.checkForBadSyntax = function(opts) {
|
||||||
["connection", "device"].forEach(checkForSingleObjectSyntax);
|
["connection", "device"].forEach(checkForSingleObjectSyntax);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Initializes all connections for the robot
|
/**
|
||||||
//
|
* Initializes a Robot's connections
|
||||||
// opts - options array passed to constructor
|
*
|
||||||
//
|
* @param {Object} opts object passed to constructor
|
||||||
// Returns initialized connections
|
* @return {Array} the Robot's connections
|
||||||
|
*/
|
||||||
Robot.prototype.initConnections = function(opts) {
|
Robot.prototype.initConnections = function(opts) {
|
||||||
this.log("info", "Initializing connections.");
|
this.log("info", "Initializing connections.");
|
||||||
|
|
||||||
|
@ -218,6 +241,13 @@ Robot.prototype.initConnections = function(opts) {
|
||||||
return this.connections;
|
return this.connections;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new Device to the Robot with the provided name and details.
|
||||||
|
*
|
||||||
|
* @param {String} name string name for the Device to use
|
||||||
|
* @param {Object} device options for the Device initializer
|
||||||
|
* @return {Object} the robot
|
||||||
|
*/
|
||||||
Robot.prototype.device = function(name, device) {
|
Robot.prototype.device = function(name, device) {
|
||||||
var str;
|
var str;
|
||||||
|
|
||||||
|
@ -253,11 +283,12 @@ Robot.prototype.device = function(name, device) {
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Initializes all devices for the robot
|
/**
|
||||||
//
|
* Initializes a Robot's devices
|
||||||
// opts - options array passed to constructor
|
*
|
||||||
//
|
* @param {Object} opts object passed to constructor
|
||||||
// Returns initialized devices
|
* @return {Array} the Robot's devices
|
||||||
|
*/
|
||||||
Robot.prototype.initDevices = function(opts) {
|
Robot.prototype.initDevices = function(opts) {
|
||||||
this.log("info", "Initializing devices.");
|
this.log("info", "Initializing devices.");
|
||||||
|
|
||||||
|
@ -278,11 +309,13 @@ Robot.prototype.initDevices = function(opts) {
|
||||||
return this.devices;
|
return this.devices;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Starts the Robot working.
|
/**
|
||||||
//
|
* Starts the Robot's connections, then devices, then work.
|
||||||
// Starts the connections, devices, and work.
|
*
|
||||||
//
|
* @param {Function} callback function to be triggered when the Robot has
|
||||||
// Returns the result of the work
|
* started working
|
||||||
|
* @return {Object} the Robot
|
||||||
|
*/
|
||||||
Robot.prototype.start = function(callback) {
|
Robot.prototype.start = function(callback) {
|
||||||
if (this.running) {
|
if (this.running) {
|
||||||
return this;
|
return this;
|
||||||
|
@ -324,11 +357,11 @@ Robot.prototype.start = function(callback) {
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Starts the Robot"s work and triggers a callback
|
/**
|
||||||
//
|
* Starts the Robot's work function
|
||||||
// callback - callback function to be triggered
|
*
|
||||||
//
|
* @return {void}
|
||||||
// Returns nothing
|
*/
|
||||||
Robot.prototype.startWork = function() {
|
Robot.prototype.startWork = function() {
|
||||||
this.log("info", "Working.");
|
this.log("info", "Working.");
|
||||||
|
|
||||||
|
@ -337,11 +370,13 @@ Robot.prototype.startWork = function() {
|
||||||
this.running = true;
|
this.running = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Starts the Robot"s connections and triggers a callback
|
/**
|
||||||
//
|
* Starts the Robot's connections
|
||||||
// callback - callback function to be triggered
|
*
|
||||||
//
|
* @param {Function} callback function to be triggered after the connections are
|
||||||
// Returns nothing
|
* started
|
||||||
|
* @return {void}
|
||||||
|
*/
|
||||||
Robot.prototype.startConnections = function(callback) {
|
Robot.prototype.startConnections = function(callback) {
|
||||||
this.log("info", "Starting connections.");
|
this.log("info", "Starting connections.");
|
||||||
|
|
||||||
|
@ -365,11 +400,13 @@ Robot.prototype.startConnections = function(callback) {
|
||||||
return Async.parallel(starters, callback);
|
return Async.parallel(starters, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Starts the Robot"s devices and triggers a callback
|
/**
|
||||||
//
|
* Starts the Robot's devices
|
||||||
// callback - callback function to be triggered
|
*
|
||||||
//
|
* @param {Function} callback function to be triggered after the devices are
|
||||||
// Returns nothing
|
* started
|
||||||
|
* @return {void}
|
||||||
|
*/
|
||||||
Robot.prototype.startDevices = function(callback) {
|
Robot.prototype.startDevices = function(callback) {
|
||||||
var log = this.log;
|
var log = this.log;
|
||||||
|
|
||||||
|
@ -393,13 +430,12 @@ Robot.prototype.startDevices = function(callback) {
|
||||||
return Async.parallel(starters, callback);
|
return Async.parallel(starters, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Halts the Robot.
|
/**
|
||||||
//
|
* Halts the Robot, attempting to gracefully stop devices and connections.
|
||||||
// Halts the devices, disconnects the connections.
|
*
|
||||||
//
|
* @param {Function} callback to be triggered when the Robot has stopped
|
||||||
// callback - callback to be triggered when the Robot is stopped
|
* @return {void}
|
||||||
//
|
*/
|
||||||
// Returns nothing
|
|
||||||
Robot.prototype.halt = function(callback) {
|
Robot.prototype.halt = function(callback) {
|
||||||
callback = callback || function() {};
|
callback = callback || function() {};
|
||||||
|
|
||||||
|
@ -423,9 +459,11 @@ Robot.prototype.halt = function(callback) {
|
||||||
this.running = false;
|
this.running = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Public: Returns basic info about the robot as a String
|
/**
|
||||||
//
|
* Generates a String representation of a Robot
|
||||||
// Returns a String
|
*
|
||||||
|
* @return {String} representation of a Robot
|
||||||
|
*/
|
||||||
Robot.prototype.toString = function() {
|
Robot.prototype.toString = function() {
|
||||||
return "[Robot name='" + this.name + "']";
|
return "[Robot name='" + this.name + "']";
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue