cylon/lib/driver.js

60 lines
1.4 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'),
Logger = require('./logger'),
Utils = require('./utils');
// 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 || {};
var extraParams = opts.extraParams || {}
this.name = opts.name;
this.device = opts.device;
this.connection = this.device.connection;
this.adaptor = this.connection.adaptor;
this.interval = extraParams.interval || 10;
this.commands = {};
};
Utils.subclass(Driver, Basestar);
Driver.prototype.setupCommands = function(commands, proxy) {
if (proxy == null) {
proxy = this.connection;
}
this.proxyMethods(commands, proxy, 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(/^_/, '');
this.commands[snake_case] = this[command];
}
}