cylon/dist/device.js

100 lines
2.8 KiB
JavaScript
Raw Normal View History

2013-10-25 05:25:42 +08:00
/*
* device
* cylonjs.com
*
* Copyright (c) 2013 The Hybrid Group
* Licensed under the Apache 2.0 license.
*/
(function() {
'use strict';
2013-11-05 04:16:42 +08:00
var EventEmitter, namespace,
2013-10-29 06:31:39 +08:00
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
2013-10-25 05:25:42 +08:00
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
2013-10-25 05:25:42 +08:00
require('./cylon');
2013-11-05 04:16:42 +08:00
namespace = require('node-namespace');
2013-10-25 05:25:42 +08:00
EventEmitter = require('events').EventEmitter;
2013-11-05 04:16:42 +08:00
namespace('Cylon', function() {
return this.Device = (function(_super) {
__extends(Device, _super);
2013-10-25 05:25:42 +08:00
2013-11-05 04:16:42 +08:00
function Device(opts) {
if (opts == null) {
opts = {};
}
this.stop = __bind(this.stop, this);
2013-11-05 04:16:42 +08:00
this.start = __bind(this.start, this);
this.self = this;
this.robot = opts.robot;
this.name = opts.name;
this.pin = opts.pin;
this.connection = this.determineConnection(opts.connection) || this.defaultConnection();
this.driver = this.requireDriver(opts);
proxyFunctionsToObject(this.driver.commands(), this.driver, this.self);
2013-10-26 11:29:11 +08:00
}
2013-11-05 04:16:42 +08:00
Device.prototype.start = function(callback) {
var msg;
msg = "Starting device '" + this.name + "'";
if (this.pin != null) {
msg += " on pin " + this.pin;
}
Logger.info(msg);
return this.driver.start(callback);
};
Device.prototype.stop = function() {
Logger.info("Stopping device '" + this.name + "'");
return this.driver.stop();
};
2013-11-05 04:16:42 +08:00
Device.prototype.data = function() {
return {
name: this.name,
driver: this.driver.constructor.name || this.driver.name,
pin: this.pin != null ? this.pin.toString : null,
connection: this.connection.data(),
commands: this.driver.commands()
};
};
2013-10-25 05:25:42 +08:00
2013-11-05 04:16:42 +08:00
Device.prototype.determineConnection = function(c) {
if (c) {
return this.robot.connections[c];
}
};
Device.prototype.defaultConnection = function() {
var first, k, v, _ref;
first = 0;
_ref = this.robot.connections;
for (k in _ref) {
v = _ref[k];
first || (first = v);
}
return first;
};
Device.prototype.requireDriver = function(opts) {
if (opts == null) {
opts = {};
}
Logger.debug("Loading driver '" + opts.driver + "'");
return this.robot.requireDriver(opts.driver, this.self, opts);
};
return Device;
2013-10-25 05:25:42 +08:00
2013-11-05 04:16:42 +08:00
})(EventEmitter);
});
2013-10-25 05:25:42 +08:00
2013-11-05 04:16:42 +08:00
module.exports = Cylon.Device;
2013-10-25 05:25:42 +08:00
}).call(this);