2014-12-16 03:15:29 +08:00
|
|
|
"use strict";
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-12-16 03:15:29 +08:00
|
|
|
var Basestar = require("./basestar"),
|
2015-02-21 10:49:31 +08:00
|
|
|
Utils = require("./utils"),
|
|
|
|
_ = require("./utils/helpers");
|
2014-04-25 06:31:47 +08:00
|
|
|
|
2015-03-20 08:33:31 +08:00
|
|
|
function formatErrorMessage(name, message) {
|
|
|
|
return ["Error in driver", "'" + name + "'", "- " + message].join(" ");
|
|
|
|
}
|
|
|
|
|
2015-06-30 01:21:25 +08:00
|
|
|
/**
|
|
|
|
* Driver class
|
|
|
|
*
|
|
|
|
* @constructor Driver
|
|
|
|
* @param {Object} [opts] driver options
|
|
|
|
* @param {String} [opts.name] the driver's name
|
|
|
|
* @param {Object} [opts.robot] the robot the driver belongs to
|
|
|
|
* @param {Object} [opts.connection] the adaptor the driver works through
|
|
|
|
* @param {Number} [opts.pin] the pin number the driver should have
|
|
|
|
* @param {Number} [opts.interval=10] read interval in milliseconds
|
|
|
|
*/
|
2014-06-17 04:09:13 +08:00
|
|
|
var Driver = module.exports = function Driver(opts) {
|
2015-05-13 09:47:27 +08:00
|
|
|
Driver.__super__.constructor.apply(this, arguments);
|
|
|
|
|
2014-06-18 10:15:38 +08:00
|
|
|
opts = opts || {};
|
|
|
|
|
2014-06-25 01:37:59 +08:00
|
|
|
this.name = opts.name;
|
2014-12-16 03:15:29 +08:00
|
|
|
this.robot = opts.robot;
|
2014-11-01 04:50:22 +08:00
|
|
|
|
2014-11-15 02:56:45 +08:00
|
|
|
this.connection = opts.connection;
|
2014-11-01 04:50:22 +08:00
|
|
|
|
2014-11-11 07:56:25 +08:00
|
|
|
this.commands = {};
|
2015-01-06 04:20:53 +08:00
|
|
|
this.events = [];
|
2014-11-11 07:56:25 +08:00
|
|
|
|
|
|
|
// some default options
|
|
|
|
this.pin = opts.pin;
|
2014-11-01 04:50:22 +08:00
|
|
|
this.interval = opts.interval || 10;
|
2014-08-06 09:41:57 +08:00
|
|
|
|
2014-11-11 07:56:25 +08:00
|
|
|
this.details = {};
|
|
|
|
|
2015-02-21 10:49:31 +08:00
|
|
|
_.each(opts, function(opt, name) {
|
|
|
|
var banned = ["robot", "name", "connection", "driver", "events"];
|
2014-12-18 06:42:34 +08:00
|
|
|
|
2015-03-12 03:31:45 +08:00
|
|
|
if (!_.includes(banned, name)) {
|
2015-02-20 09:23:41 +08:00
|
|
|
this.details[name] = opt;
|
|
|
|
}
|
2015-02-21 10:49:31 +08:00
|
|
|
}, this);
|
2014-05-07 09:24:43 +08:00
|
|
|
};
|
2014-02-28 01:38:38 +08:00
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils.subclass(Driver, Basestar);
|
2014-02-28 01:38:38 +08:00
|
|
|
|
2015-06-30 01:21:25 +08:00
|
|
|
/**
|
|
|
|
* A base start function. Must be overwritten by a descendent.
|
|
|
|
*
|
|
|
|
* @throws Error if not overridden by a child class
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2015-03-20 08:33:31 +08:00
|
|
|
Driver.prototype.start = function() {
|
|
|
|
var message = formatErrorMessage(
|
|
|
|
this.name,
|
|
|
|
"Driver#start method must be overwritten by descendant classes."
|
|
|
|
);
|
|
|
|
|
|
|
|
throw new Error(message);
|
|
|
|
};
|
|
|
|
|
2015-06-30 01:21:25 +08:00
|
|
|
/**
|
|
|
|
* A base halt function. Must be overwritten by a descendent.
|
|
|
|
*
|
|
|
|
* @throws Error if not overridden by a child class
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2015-03-20 08:33:31 +08:00
|
|
|
Driver.prototype.halt = function() {
|
|
|
|
var message = formatErrorMessage(
|
|
|
|
this.name,
|
|
|
|
"Driver#halt method must be overwritten by descendant classes."
|
|
|
|
);
|
|
|
|
|
|
|
|
throw new Error(message);
|
|
|
|
};
|
|
|
|
|
2015-06-30 01:21:25 +08:00
|
|
|
/**
|
|
|
|
* Sets up an array of commands for the Driver.
|
|
|
|
*
|
|
|
|
* Proxies commands from the Driver to its connection (or a manually specified
|
|
|
|
* proxy), and adds a snake_cased version to the driver's commands object.
|
|
|
|
*
|
|
|
|
* @param {String[]} commands an array of driver commands
|
|
|
|
* @param {Object} [proxy=this.connection] proxy target
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2014-08-13 01:07:17 +08:00
|
|
|
Driver.prototype.setupCommands = function(commands, proxy) {
|
|
|
|
if (proxy == null) {
|
2014-11-15 03:34:37 +08:00
|
|
|
proxy = this.connection;
|
2014-08-13 01:07:17 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 05:19:58 +08:00
|
|
|
Utils.proxyFunctionsToObject(commands, proxy, this);
|
2014-08-12 05:10:59 +08:00
|
|
|
|
2015-07-04 01:37:08 +08:00
|
|
|
function endsWith(string, substr) {
|
|
|
|
return string.indexOf(substr, string.length - substr.length) !== -1;
|
|
|
|
}
|
|
|
|
|
2015-02-20 09:23:41 +08:00
|
|
|
commands.forEach(function(command) {
|
2015-04-19 08:15:55 +08:00
|
|
|
var snakeCase = command.replace(/[A-Z]+/g, function(match) {
|
2015-07-04 01:37:08 +08:00
|
|
|
if (match.length > 1 && !endsWith(command, match)) {
|
2014-08-12 05:10:59 +08:00
|
|
|
match = match.replace(/[A-Z]$/, function(m) {
|
|
|
|
return "_" + m.toLowerCase();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return "_" + match.toLowerCase();
|
2014-12-16 03:15:29 +08:00
|
|
|
}).replace(/^_/, "");
|
2014-08-12 05:10:59 +08:00
|
|
|
|
2015-04-19 08:15:55 +08:00
|
|
|
this.commands[snakeCase] = this[command];
|
2014-12-18 06:42:34 +08:00
|
|
|
}, this);
|
2014-12-16 03:15:29 +08:00
|
|
|
};
|
2014-11-11 07:56:25 +08:00
|
|
|
|
2015-06-30 01:21:25 +08:00
|
|
|
/**
|
|
|
|
* Expresses the Driver in a JSON-serializable format
|
|
|
|
*
|
|
|
|
* @return {Object} a representation of the Driver in a serializable format
|
|
|
|
*/
|
2014-11-11 07:56:25 +08:00
|
|
|
Driver.prototype.toJSON = function() {
|
|
|
|
return {
|
|
|
|
name: this.name,
|
|
|
|
driver: this.constructor.name || this.name,
|
2014-11-15 02:56:45 +08:00
|
|
|
connection: this.connection.name,
|
2015-02-20 09:23:41 +08:00
|
|
|
commands: Object.keys(this.commands),
|
2015-01-06 04:20:53 +08:00
|
|
|
events: this.events,
|
2014-11-11 07:56:25 +08:00
|
|
|
details: this.details
|
|
|
|
};
|
|
|
|
};
|