cylon/lib/device.js

92 lines
2.3 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
require('./cylon');
require('./driver');
var namespace = require('node-namespace');
var EventEmitter = require('events').EventEmitter;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
2013-10-25 05:25:42 +08:00
namespace("Cylon", function() {
this.Device = (function(klass) {
subclass(Device, klass);
2013-10-25 05:25:42 +08:00
function Device(opts) {
if (opts == null) {
opts = {};
2013-10-26 11:29:11 +08:00
}
this.stop = __bind(this.stop, this);
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.initDriver(opts);
proxyFunctionsToObject(this.driver.commands(), this.driver, this.self);
}
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();
};
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-11-05 04:16: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;
};
2013-11-05 04:16:42 +08:00
Device.prototype.initDriver = function(opts) {
if (opts == null) {
opts = {};
}
Logger.debug("Loading driver '" + opts.driver + "'");
return this.robot.initDriver(opts.driver, this.self, opts);
};
2013-10-25 05:25:42 +08:00
return Device;
2013-10-25 05:25:42 +08:00
})(EventEmitter);
});
2013-10-25 05:25:42 +08:00
module.exports = Cylon.Device;