cylon/lib/connection.js

125 lines
3.1 KiB
JavaScript
Raw Normal View History

2013-10-25 05:25:42 +08:00
/*
* connection
* 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 23:38:06 +08:00
var EventEmitter = require('events').EventEmitter;
2014-02-28 06:42:33 +08:00
var Logger = require('./logger'),
Utils = require('./utils');
2014-05-14 10:44:40 +08:00
2014-02-28 06:42:33 +08:00
// The Connection class represents the interface to
// a specific group of hardware devices. Examples would be an
// Arduino, a Sphero, or an ARDrone.
2014-05-07 23:38:06 +08:00
var Connection;
// Public: Creates a new Connection
//
// opts - hash of acceptable params:
// id - string ID for the connection
// robot - Robot the Connection belongs to
// name - name for the connection
// adaptor - string module name of the adaptor to be set up
// port - string port to use for the Connection
//
// Returns the newly set-up connection
module.exports = Connection = function Connection(opts) {
if (opts == null) {
opts = {};
}
if (opts.id == null) {
opts.id = Math.floor(Math.random() * 10000);
}
this.connect = this.connect.bind(this);
2014-05-07 23:38:06 +08:00
this.robot = opts.robot;
this.name = opts.name;
this.connection_id = opts.id;
this.port = opts.port;
this.adaptor = this.initAdaptor(opts);
Utils.proxyFunctionsToObject(this.adaptor.commands, this.adaptor, this);
2014-06-17 04:09:13 +08:00
};
2014-05-07 23:38:06 +08:00
Utils.subclass(Connection, EventEmitter);
2014-05-07 23:38:06 +08:00
// Public: Expresses the Connection in JSON format
2014-05-07 23:38:06 +08:00
//
// Returns an Object containing Connection data
Connection.prototype.toJSON = function() {
2014-05-07 23:38:06 +08:00
return {
name: this.name,
port: this.port,
adaptor: this.adaptor.constructor.name || this.adaptor.name,
connection_id: this.connection_id
};
};
// Public: Connect the adaptor's connection
//
// callback - callback function to run when the adaptor is connected
//
// Returns the result of the supplied callback function
Connection.prototype.connect = function(callback) {
var msg = this._logstring("Connecting to");
2014-05-07 23:38:06 +08:00
Logger.info(msg);
return this.adaptor.connect(callback);
};
// Public: Disconnect the adaptor's connection
//
// callback - function to be triggered then the adaptor has disconnected
//
2014-05-07 23:38:06 +08:00
// Returns nothing
Connection.prototype.disconnect = function(callback) {
var msg = this._logstring("Disconnecting from");
2014-05-07 23:38:06 +08:00
Logger.info(msg);
return this.adaptor.disconnect(callback);
2014-05-07 23:38:06 +08:00
};
// Public: sets up adaptor with @robot
//
// opts - options for adaptor being initialized
// adaptor - name of the adaptor
//
// Returns the set-up adaptor
Connection.prototype.initAdaptor = function(opts) {
Logger.debug("Loading adaptor '" + opts.adaptor + "'.");
return this.robot.initAdaptor(opts.adaptor, this, opts);
2014-05-07 23:38:06 +08:00
};
// Public: Halt the adaptor's connection
//
// callback - function to be triggered when the connection has halted
//
2014-05-07 23:38:06 +08:00
// Returns nothing
Connection.prototype.halt = function(callback) {
2014-05-07 23:38:06 +08:00
var msg = "Halting adaptor " + this.name;
if (this.port != null) {
msg += " on port " + this.port;
}
Logger.info(msg);
return this.disconnect(callback);
2014-05-07 23:38:06 +08:00
};
Connection.prototype._logstring = function _logstring(action) {
var msg = action + " '" + this.name + "'";
if (this.port != null) {
msg += " on port " + this.port;
}
msg += ".";
return msg;
};