82 lines
1.8 KiB
JavaScript
82 lines
1.8 KiB
JavaScript
/*
|
|
* driver
|
|
* cylonjs.com
|
|
*
|
|
* Copyright (c) 2013-2014 The Hybrid Group
|
|
* Licensed under the Apache 2.0 license.
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
var Basestar = require("./basestar"),
|
|
Utils = require("./utils"),
|
|
_ = require("./lodash");
|
|
|
|
// Public: Creates a new Driver
|
|
//
|
|
// opts - hash of acceptable params
|
|
// name - name of the Driver, used when printing to console
|
|
// device - Device the driver will use to proxy commands/events
|
|
//
|
|
// Returns a new Driver
|
|
var Driver = module.exports = function Driver(opts) {
|
|
opts = opts || {};
|
|
|
|
this.name = opts.name;
|
|
this.robot = opts.robot;
|
|
|
|
this.connection = opts.connection;
|
|
|
|
this.commands = {};
|
|
this.events = [];
|
|
|
|
// some default options
|
|
this.pin = opts.pin;
|
|
this.interval = opts.interval || 10;
|
|
|
|
this.details = {};
|
|
|
|
_.forEach(opts, function(opt, name) {
|
|
if (_.include(["robot", "name", "connection", "driver", "events"], name)) {
|
|
return;
|
|
}
|
|
|
|
this.details[name] = opt;
|
|
}, this);
|
|
};
|
|
|
|
Utils.subclass(Driver, Basestar);
|
|
|
|
Driver.prototype.setupCommands = function(commands, proxy) {
|
|
if (proxy == null) {
|
|
proxy = this.connection;
|
|
}
|
|
|
|
Utils.proxyFunctionsToObject(commands, proxy, this);
|
|
|
|
_.forEach(commands, function(command) {
|
|
var snake_case = command.replace(/[A-Z]+/g, function(match) {
|
|
if (match.length > 1) {
|
|
match = match.replace(/[A-Z]$/, function(m) {
|
|
return "_" + m.toLowerCase();
|
|
});
|
|
}
|
|
|
|
return "_" + match.toLowerCase();
|
|
}).replace(/^_/, "");
|
|
|
|
this.commands[snake_case] = this[command];
|
|
}, this);
|
|
};
|
|
|
|
Driver.prototype.toJSON = function() {
|
|
return {
|
|
name: this.name,
|
|
driver: this.constructor.name || this.name,
|
|
connection: this.connection.name,
|
|
commands: _.keys(this.commands),
|
|
events: this.events,
|
|
details: this.details
|
|
};
|
|
};
|