cylon/lib/device.js

136 lines
3.2 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
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 = {};
}
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);
this.details = {};
for (var opt in opts) {
if (['robot', 'name', 'connection', 'driver'].indexOf(opt) < 0) {
this.details[opt] = opts[opt];
}
}
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
};
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) {
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);
return this.driver.start(callback);
};
// Public: Halt the device driver
//
// callback - function to trigger when the device has been halted
//
2014-05-07 10:18:13 +08:00
// Returns result of supplied callback
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: 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 = {};
}
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
};