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-02-28 01:38:38 +08:00
|
|
|
'use strict';
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-05-14 10:44:40 +08:00
|
|
|
var Basestar = require('./basestar'),
|
2014-06-06 03:11:37 +08:00
|
|
|
Logger = require('./logger'),
|
|
|
|
Utils = require('./utils');
|
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;
|
|
|
|
this.device = opts.device;
|
|
|
|
this.connection = this.device.connection;
|
2014-08-06 09:41:57 +08:00
|
|
|
|
|
|
|
this.commands = {};
|
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-12 05:10:59 +08:00
|
|
|
Driver.prototype.setupCommands = function(commands) {
|
|
|
|
this.proxyMethods(commands, this.connection, this);
|
|
|
|
|
|
|
|
for (var i = 0; i < commands.length; i++) {
|
|
|
|
var command = commands[i];
|
|
|
|
|
|
|
|
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(/^_/, '');
|
|
|
|
|
2014-08-12 06:29:35 +08:00
|
|
|
this.commands[snake_case] = this[command];
|
2014-08-12 05:10:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 00:20:23 +08:00
|
|
|
// Public: Starts up the driver, and triggers the provided callback when done.
|
2014-05-07 09:24:43 +08:00
|
|
|
//
|
|
|
|
// callback - function to run when the driver is started
|
|
|
|
//
|
|
|
|
// Returns nothing
|
|
|
|
Driver.prototype.start = function(callback) {
|
2014-06-11 02:33:52 +08:00
|
|
|
Logger.info("Driver " + this.name + " started.");
|
2014-05-07 09:24:43 +08:00
|
|
|
callback(null);
|
|
|
|
};
|
2014-02-28 01:38:38 +08:00
|
|
|
|
2014-05-07 09:24:43 +08:00
|
|
|
// Public: Halts the driver
|
|
|
|
//
|
2014-06-13 06:31:49 +08:00
|
|
|
// callback - function to be triggered when the driver is halted
|
|
|
|
//
|
2014-05-07 09:24:43 +08:00
|
|
|
// Returns nothing
|
2014-06-13 06:31:49 +08:00
|
|
|
Driver.prototype.halt = function(callback) {
|
2014-06-11 02:33:52 +08:00
|
|
|
Logger.info("Driver " + this.name + " halted.");
|
2014-06-13 06:31:49 +08:00
|
|
|
this.removeAllListeners();
|
2014-07-11 00:51:23 +08:00
|
|
|
callback(null);
|
2014-05-07 09:24:43 +08:00
|
|
|
};
|