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-06-06 03:11:37 +08:00
|
|
|
var Logger = require('./logger'),
|
|
|
|
Utils = require('./utils');
|
2014-05-14 10:44:40 +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
|
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;
|
|
|
|
this.connection = this.determineConnection(opts.connection) || this.defaultConnection();
|
|
|
|
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-08-13 09:33:20 +08:00
|
|
|
for (var opt in this.driver) {
|
|
|
|
if (!this[opt] && typeof this.driver[opt] === 'function') {
|
|
|
|
this[opt] = this.driver[opt].bind(this.driver);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
//
|
|
|
|
// Returns result of supplied callback
|
|
|
|
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);
|
|
|
|
return this.driver.start(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Halt the device driver
|
|
|
|
//
|
2014-06-13 06:31:49 +08:00
|
|
|
// callback - function to trigger when the device has been halted
|
|
|
|
//
|
2014-05-07 10:18:13 +08:00
|
|
|
// Returns result of supplied callback
|
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: Retrieves the connections from the parent Robot instances
|
|
|
|
//
|
2014-06-11 09:19:04 +08:00
|
|
|
// conn - name of the connection to fetch
|
2014-05-07 10:18:13 +08:00
|
|
|
//
|
|
|
|
// Returns a Connection instance
|
2014-06-11 09:19:04 +08:00
|
|
|
Device.prototype.determineConnection = function(conn) {
|
|
|
|
return this.robot.connections[conn];
|
2014-05-07 10:18:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Public: Returns a default Connection to use
|
|
|
|
//
|
|
|
|
// Returns a Connection instance
|
|
|
|
Device.prototype.defaultConnection = function() {
|
|
|
|
var first = 0;
|
|
|
|
|
|
|
|
for (var c in this.robot.connections) {
|
|
|
|
var connection = this.robot.connections[c];
|
|
|
|
first || (first = connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
return first;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-06-11 09:19:04 +08:00
|
|
|
if (opts == null) {
|
|
|
|
opts = {};
|
|
|
|
}
|
|
|
|
|
2014-06-11 02:33:52 +08:00
|
|
|
Logger.debug("Loading driver '" + opts.driver + "'.");
|
2014-06-11 09:19:04 +08:00
|
|
|
return this.robot.initDriver(opts.driver, this, opts);
|
2014-05-07 10:18:13 +08:00
|
|
|
};
|