cylon/lib/device.js

137 lines
3.1 KiB
JavaScript
Raw Normal View History

2013-10-25 05:25:42 +08:00
/*
* device
* cylonjs.com
*
* Copyright (c) 2013-2014 The Hybrid Group
2013-10-25 05:25:42 +08:00
* Licensed under the Apache 2.0 license.
*/
'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'),
Logger = require('./logger'),
Utils = require('./utils');
2014-05-14 10:44: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 = {};
}
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);
this.details = {};
for (var opt in opts) {
2014-11-07 01:10:56 +08:00
if (['robot', 'name', 'connection', 'driver', 'device', 'adaptor'].indexOf(opt) < 0) {
this.details[opt] = opts[opt];
}
}
2014-05-07 10:18:13 +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
//
// Returns nothing
2014-05-07 10:18:13 +08:00
Device.prototype.start = function(callback) {
var msg = "Starting device '" + this.name + "'";
2014-05-07 10:18:13 +08:00
if (this.pin != null) {
msg += " on pin " + this.pin;
}
msg += ".";
2014-05-07 10:18:13 +08:00
Logger.info(msg);
2014-10-02 08:58:14 +08:00
this.driver.start(function() {
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
//
// callback - function to trigger when the device has been halted
//
// Returns nothing
Device.prototype.halt = function(callback) {
Logger.info("Halting device '" + this.name + "'.");
this.removeAllListeners();
this.driver.halt(callback);
2014-05-07 10:18:13 +08:00
};
// Public: Expresses the Device in JSON format
2014-05-07 10:18:13 +08:00
//
// Returns an Object containing Connection data
Device.prototype.toJSON = function() {
2014-05-07 10:18:13 +08:00
return {
name: this.name,
driver: this.driver.constructor.name || this.driver.name,
connection: this.connection.name,
2014-08-08 04:44:25 +08:00
commands: Object.keys(this.driver.commands),
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) {
var module;
if (opts.module) {
module = Registry.register(opts.module);
} else {
module = Registry.findByDriver(opts.driver);
}
opts.device = this;
if (!module) {
2014-11-01 05:14:02 +08:00
Registry.register('cylon-' + opts.driver);
module = Registry.findByDriver(opts.driver);
}
var driver = module.driver(opts);
if (testMode) {
2014-11-01 05:14:02 +08:00
var testDriver = Registry.findByDriver('test').driver(opts);
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
}
return driver;
2014-05-07 10:18:13 +08:00
};