cylon/lib/device.js

71 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2013-10-25 05:25:42 +08:00
/*
* device
* cylonjs.com
*
2015-01-08 04:58:50 +08:00
* Copyright (c) 2013-2015 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 Registry = require("./registry"),
2014-12-18 06:42:34 +08:00
Config = require("./config"),
_ = require("./lodash");
2014-05-14 10:44:40 +08:00
2014-11-07 01:48:21 +08:00
var testMode = function() {
2014-12-16 03:15:29 +08:00
return process.env.NODE_ENV === "test" && Config.testMode;
2014-11-07 01:48:21 +08:00
};
2014-05-07 10:18:13 +08:00
// Public: Creates a new Device
//
// opts - object containing Device params
// name - string name of the device
// pin - string pin of the device
// robot - parent Robot to the device
// connection - connection to the device
// driver - string name of the module the device driver logic lives in
//
// Returns a new Device
module.exports = function Device(opts) {
2014-12-18 06:42:34 +08:00
var module;
if (opts.module) {
module = Registry.register(opts.module);
} else {
module = Registry.findByDriver(opts.driver);
}
opts.device = this;
if (!module) {
2014-12-16 03:15:29 +08:00
Registry.register("cylon-" + opts.driver);
2014-11-01 05:14:02 +08:00
module = Registry.findByDriver(opts.driver);
}
var driver = module.driver(opts);
_.forIn(driver, function(prop, name) {
2014-12-18 06:42:34 +08:00
if (name === "constructor") {
return;
2014-11-12 05:41:09 +08:00
}
2014-12-18 06:42:34 +08:00
if (_.isFunction(prop)) {
driver[name] = prop.bind(driver);
2014-11-12 03:41:18 +08:00
}
2014-12-18 06:42:34 +08:00
});
2014-11-12 03:41:18 +08:00
2014-11-07 01:48:21 +08:00
if (testMode()) {
2014-12-16 03:15:29 +08:00
var testDriver = Registry.findByDriver("test").driver(opts);
_.forIn(driver, function(prop, name) {
2014-12-18 06:42:34 +08:00
if (_.isFunction(prop) && !testDriver[name]) {
testDriver[name] = function() { return true; };
}
2014-12-18 06:42:34 +08:00
});
return testDriver;
2014-06-11 09:19:04 +08:00
}
return driver;
2014-05-07 10:18:13 +08:00
};