2013-10-25 05:25:42 +08:00
|
|
|
/*
|
|
|
|
* device
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
2014-02-28 02:33:43 +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 02:33:43 +08:00
|
|
|
'use strict';
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-05-07 10:18:13 +08:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2014-02-28 09:39:02 +08:00
|
|
|
|
2014-11-01 05:14:02 +08:00
|
|
|
var Registry = require('./registry'),
|
2014-10-31 04:15:40 +08:00
|
|
|
Logger = require('./logger'),
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils = require('./utils');
|
2014-05-14 10:44:40 +08:00
|
|
|
|
2014-10-31 04:15:40 +08:00
|
|
|
var testMode = process.env.NODE_ENV === 'test' && !CYLON_TEST;
|
|
|
|
|
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
|
2014-06-11 09:19:04 +08:00
|
|
|
var Device = module.exports = function Device(opts) {
|
2014-05-07 10:18:13 +08:00
|
|
|
if (opts == null) {
|
|
|
|
opts = {};
|
|
|
|
}
|
|
|
|
|
2014-06-18 10:35:07 +08:00
|
|
|
this.halt = this.halt.bind(this);
|
|
|
|
this.start = this.start.bind(this);
|
2014-05-07 10:18:13 +08:00
|
|
|
|
|
|
|
this.robot = opts.robot;
|
|
|
|
this.name = opts.name;
|
|
|
|
this.pin = opts.pin;
|
2014-11-01 04:48:05 +08:00
|
|
|
this.connection = opts.connection;
|
2014-05-07 10:18:13 +08:00
|
|
|
this.driver = this.initDriver(opts);
|
|
|
|
|
2014-07-26 06:55:16 +08:00
|
|
|
this.details = {};
|
|
|
|
|
|
|
|
for (var opt in opts) {
|
|
|
|
if (['robot', 'name', 'connection', 'driver'].indexOf(opt) < 0) {
|
|
|
|
this.details[opt] = opts[opt];
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 10:18:13 +08:00
|
|
|
};
|
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
Utils.subclass(Device, EventEmitter);
|
2014-05-07 10:18:13 +08:00
|
|
|
|
|
|
|
// Public: Starts the device driver
|
|
|
|
//
|
|
|
|
// callback - callback function to be executed by the driver start
|
|
|
|
//
|
2014-09-09 00:27:18 +08:00
|
|
|
// Returns nothing
|
2014-05-07 10:18:13 +08:00
|
|
|
Device.prototype.start = function(callback) {
|
2014-06-11 02:33:52 +08:00
|
|
|
var msg = "Starting device '" + this.name + "'";
|
2014-05-07 10:18:13 +08:00
|
|
|
|
|
|
|
if (this.pin != null) {
|
|
|
|
msg += " on pin " + this.pin;
|
|
|
|
}
|
|
|
|
|
2014-06-11 02:33:52 +08:00
|
|
|
msg += ".";
|
|
|
|
|
2014-05-07 10:18:13 +08:00
|
|
|
Logger.info(msg);
|
2014-10-02 08:58:14 +08:00
|
|
|
this.driver.start(function() {
|
2014-10-03 02:00:39 +08:00
|
|
|
Utils.proxyFunctions(this.driver, this)
|
2014-10-02 08:58:14 +08:00
|
|
|
callback.apply(this, arguments);
|
|
|
|
}.bind(this));
|
2014-05-07 10:18:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Halt the device driver
|
|
|
|
//
|
2014-06-13 06:31:49 +08:00
|
|
|
// callback - function to trigger when the device has been halted
|
|
|
|
//
|
2014-09-09 00:27:18 +08:00
|
|
|
// Returns nothing
|
2014-06-13 06:31:49 +08:00
|
|
|
Device.prototype.halt = function(callback) {
|
2014-06-11 02:33:52 +08:00
|
|
|
Logger.info("Halting device '" + this.name + "'.");
|
2014-09-05 23:43:40 +08:00
|
|
|
this.removeAllListeners();
|
2014-06-13 06:31:49 +08:00
|
|
|
this.driver.halt(callback);
|
2014-05-07 10:18:13 +08:00
|
|
|
};
|
|
|
|
|
2014-06-07 05:11:35 +08:00
|
|
|
// Public: Expresses the Device in JSON format
|
2014-05-07 10:18:13 +08:00
|
|
|
//
|
|
|
|
// Returns an Object containing Connection data
|
2014-06-07 05:11:35 +08:00
|
|
|
Device.prototype.toJSON = function() {
|
2014-05-07 10:18:13 +08:00
|
|
|
return {
|
|
|
|
name: this.name,
|
|
|
|
driver: this.driver.constructor.name || this.driver.name,
|
2014-07-16 02:49:29 +08:00
|
|
|
connection: this.connection.name,
|
2014-08-08 04:44:25 +08:00
|
|
|
commands: Object.keys(this.driver.commands),
|
2014-07-26 06:55:16 +08:00
|
|
|
details: this.details
|
2014-05-07 10:18:13 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: sets up driver with @robot
|
|
|
|
//
|
|
|
|
// opts - object containing options when initializing driver
|
|
|
|
// driver - name of the driver to intt()
|
|
|
|
//
|
|
|
|
// Returns the set-up driver
|
|
|
|
Device.prototype.initDriver = function(opts) {
|
2014-11-06 02:04:45 +08:00
|
|
|
var module;
|
|
|
|
|
|
|
|
if (opts.module) {
|
|
|
|
module = Registry.register(opts.module);
|
|
|
|
} else {
|
|
|
|
module = Registry.findByDriver(opts.driver);
|
|
|
|
}
|
2014-10-31 04:15:40 +08:00
|
|
|
|
|
|
|
opts.device = this;
|
|
|
|
|
|
|
|
if (!module) {
|
2014-11-01 05:14:02 +08:00
|
|
|
Registry.register('cylon-' + opts.driver);
|
|
|
|
module = Registry.findByDriver(opts.driver);
|
2014-10-31 04:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var driver = module.driver(opts);
|
|
|
|
|
|
|
|
if (testMode) {
|
2014-11-01 05:14:02 +08:00
|
|
|
var testDriver = Registry.findByDriver('test').driver(opts);
|
2014-10-31 04:15:40 +08:00
|
|
|
|
|
|
|
for (var prop in driver) {
|
|
|
|
if (typeof driver[prop] === 'function' && !testDriver[prop]) {
|
|
|
|
testDriver[prop] = function() { return true; };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return testDriver;
|
2014-06-11 09:19:04 +08:00
|
|
|
}
|
|
|
|
|
2014-10-31 04:15:40 +08:00
|
|
|
return driver;
|
2014-05-07 10:18:13 +08:00
|
|
|
};
|