2013-10-25 05:25:42 +08:00
|
|
|
/*
|
|
|
|
* driver
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
2014-02-28 01:38:38 +08:00
|
|
|
* Copyright (c) 2013-2014 The Hybrid Group
|
2013-10-25 05:25:42 +08:00
|
|
|
* Licensed under the Apache 2.0 license.
|
|
|
|
*/
|
|
|
|
|
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"),
|
2014-12-18 06:42:34 +08:00
|
|
|
Utils = require("./utils"),
|
|
|
|
_ = require("./lodash");
|
2014-04-25 06:31:47 +08:00
|
|
|
|
2014-05-07 09:24:43 +08:00
|
|
|
// 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
|
2014-06-17 04:09:13 +08:00
|
|
|
var Driver = module.exports = function Driver(opts) {
|
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 = {};
|
|
|
|
|
2014-12-18 06:42:34 +08:00
|
|
|
_.forEach(opts, function(opt, name) {
|
2015-01-06 04:20:53 +08:00
|
|
|
if (_.include(["robot", "name", "connection", "driver", "events"], name)) {
|
2014-12-18 06:42:34 +08:00
|
|
|
return;
|
2014-11-11 07:56:25 +08:00
|
|
|
}
|
2014-12-18 06:42:34 +08:00
|
|
|
|
|
|
|
this.details[name] = opt;
|
|
|
|
}, 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
|
|
|
|
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
|
|
|
|
2014-12-18 06:42:34 +08:00
|
|
|
_.forEach(commands, function(command) {
|
2014-08-12 05:10:59 +08:00
|
|
|
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();
|
2014-12-16 03:15:29 +08:00
|
|
|
}).replace(/^_/, "");
|
2014-08-12 05:10:59 +08:00
|
|
|
|
2014-08-12 06:29:35 +08:00
|
|
|
this.commands[snake_case] = 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
|
|
|
|
|
|
|
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,
|
2014-12-18 06:42:34 +08:00
|
|
|
commands: _.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
|
|
|
|
};
|
|
|
|
};
|