node-poweredup/docs/hub.js.html

557 lines
22 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>node-poweredup Source: hub.js</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.simplex.css">
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top navbar-inverse">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.html">node-poweredup</a>
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="topNavigation">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a>
<ul class="dropdown-menu ">
<li><a href="BoostMoveHub.html">BoostMoveHub</a></li><li><a href="ControlPlusHub.html">ControlPlusHub</a></li><li><a href="DuploTrainBase.html">DuploTrainBase</a></li><li><a href="Hub.html">Hub</a></li><li><a href="LPF2Hub.html">LPF2Hub</a></li><li><a href="PoweredUP.html">PoweredUP</a></li><li><a href="PUPHub.html">PUPHub</a></li><li><a href="PUPRemote.html">PUPRemote</a></li><li><a href="WeDo2SmartHub.html">WeDo2SmartHub</a></li>
</ul>
</li>
<li class="dropdown">
<a href="events.list.html" class="dropdown-toggle" data-toggle="dropdown">Events<b class="caret"></b></a>
<ul class="dropdown-menu ">
<li><a href="BoostMoveHub.html#event:accel">BoostMoveHub#event:accel</a></li><li><a href="BoostMoveHub.html#event:attach">BoostMoveHub#event:attach</a></li><li><a href="BoostMoveHub.html#event:button">BoostMoveHub#event:button</a></li><li><a href="BoostMoveHub.html#event:color">BoostMoveHub#event:color</a></li><li><a href="BoostMoveHub.html#event:colorAndDistance">BoostMoveHub#event:colorAndDistance</a></li><li><a href="BoostMoveHub.html#event:detach">BoostMoveHub#event:detach</a></li><li><a href="BoostMoveHub.html#event:distance">BoostMoveHub#event:distance</a></li><li><a href="BoostMoveHub.html#event:rotate">BoostMoveHub#event:rotate</a></li><li><a href="BoostMoveHub.html#event:speed">BoostMoveHub#event:speed</a></li><li><a href="BoostMoveHub.html#event:temp">BoostMoveHub#event:temp</a></li><li><a href="BoostMoveHub.html#event:tilt">BoostMoveHub#event:tilt</a></li><li><a href="ControlPlusHub.html#event:accel">ControlPlusHub#event:accel</a></li><li><a href="ControlPlusHub.html#event:attach">ControlPlusHub#event:attach</a></li><li><a href="ControlPlusHub.html#event:button">ControlPlusHub#event:button</a></li><li><a href="ControlPlusHub.html#event:color">ControlPlusHub#event:color</a></li><li><a href="ControlPlusHub.html#event:colorAndDistance">ControlPlusHub#event:colorAndDistance</a></li><li><a href="ControlPlusHub.html#event:detach">ControlPlusHub#event:detach</a></li><li><a href="ControlPlusHub.html#event:distance">ControlPlusHub#event:distance</a></li><li><a href="ControlPlusHub.html#event:rotate">ControlPlusHub#event:rotate</a></li><li><a href="ControlPlusHub.html#event:speed">ControlPlusHub#event:speed</a></li><li><a href="ControlPlusHub.html#event:temp">ControlPlusHub#event:temp</a></li><li><a href="ControlPlusHub.html#event:tilt">ControlPlusHub#event:tilt</a></li><li><a href="DuploTrainBase.html#event:accel">DuploTrainBase#event:accel</a></li><li><a href="DuploTrainBase.html#event:attach">DuploTrainBase#event:attach</a></li><li><a href="DuploTrainBase.html#event:button">DuploTrainBase#event:button</a></li><li><a href="DuploTrainBase.html#event:color">DuploTrainBase#event:color</a></li><li><a href="DuploTrainBase.html#event:colorAndDistance">DuploTrainBase#event:colorAndDistance</a></li><li><a href="DuploTrainBase.html#event:detach">DuploTrainBase#event:detach</a></li><li><a href="DuploTrainBase.html#event:distance">DuploTrainBase#event:distance</a></li><li><a href="DuploTrainBase.html#event:rotate">DuploTrainBase#event:rotate</a></li><li><a href="DuploTrainBase.html#event:speed">DuploTrainBase#event:speed</a></li><li><a href="DuploTrainBase.html#event:temp">DuploTrainBase#event:temp</a></li><li><a href="DuploTrainBase.html#event:tilt">DuploTrainBase#event:tilt</a></li><li><a href="Hub.html#event:attach">Hub#event:attach</a></li><li><a href="Hub.html#event:detach">Hub#event:detach</a></li><li><a href="LPF2Hub.html#event:accel">LPF2Hub#event:accel</a></li><li><a href="LPF2Hub.html#event:attach">LPF2Hub#event:attach</a></li><li><a href="LPF2Hub.html#event:button">LPF2Hub#event:button</a></li><li><a href="LPF2Hub.html#event:color">LPF2Hub#event:color</a></li><li><a href="LPF2Hub.html#event:colorAndDistance">LPF2Hub#event:colorAndDistance</a></li><li><a href="LPF2Hub.html#event:detach">LPF2Hub#event:detach</a></li><li><a href="LPF2Hub.html#event:distance">LPF2Hub#event:distance</a></li><li><a href="LPF2Hub.html#event:rotate">LPF2Hub#event:rotate</a></li><li><a href="LPF2Hub.html#event:speed">LPF2Hub#event:speed</a></li><li><a href="LPF2Hub.html#event:temp">LPF2Hub#event:temp</a></li><li><a href="LPF2Hub.html#event:tilt">LPF2Hub#event:tilt</a></li><li><a href="PoweredUP.html#event:discover">PoweredUP#event:discover</a></li><li><a href="PUPHub.html#event:accel">PUPHub#event:accel</a></li><li><a href="PUPHub.html#event:attach">PUPHub#event:attach</a></li><li><a href="PUPHub.html#event:button">PUPHub#event:button</a></li><li><a href="PUPHub.html#event:color">PUPHub#event:color</a></li><li><a href="PUPHub.html#event:colorAndDistance">PUPHub#event:colorAndDistance</a></li><li><a href="PUPHub.html#event:detach">PUPHub#event:detach</a></li><li><a href="PUPHub.html#event:distance">PUPHub#event:distance</a></li><li><a href="PUPHub.html#event:rotate">PUPHub#event:rotate</a></li><li><a href="PUPHub.html#event:speed">PUPHub#event:speed</a></li><li><a href="PUPHub.html#event:temp">PUPHub#event:temp</a></li><li><a href="PUPHub.html#event:tilt">PUPHub#event:tilt</a></li><li><a href="PUPRemote.html#event:accel">PUPRemote#event:accel</a></li><li><a href="PUPRemote.html#event:attach">PUPRemote#event:attach</a></li><li><a href="PUPRemote.html#event:button">PUPRemote#event:button</a></li><li><a href="PUPRemote.html#event:color">PUPRemote#event:color</a></li><li><a href="PUPRemote.html#event:colorAndDistance">PUPRemote#event:colorAndDistance</a></li><li><a href="PUPRemote.html#event:detach">PUPRemote#event:detach</a></li><li><a href="PUPRemote.html#event:distance">PUPRemote#event:distance</a></li><li><a href="PUPRemote.html#event:rotate">PUPRemote#event:rotate</a></li><li><a href="PUPRemote.html#event:speed">PUPRemote#event:speed</a></li><li><a href="PUPRemote.html#event:temp">PUPRemote#event:temp</a></li><li><a href="PUPRemote.html#event:tilt">PUPRemote#event:tilt</a></li><li><a href="WeDo2SmartHub.html#event:attach">WeDo2SmartHub#event:attach</a></li><li><a href="WeDo2SmartHub.html#event:button">WeDo2SmartHub#event:button</a></li><li><a href="WeDo2SmartHub.html#event:color">WeDo2SmartHub#event:color</a></li><li><a href="WeDo2SmartHub.html#event:detach">WeDo2SmartHub#event:detach</a></li><li><a href="WeDo2SmartHub.html#event:distance">WeDo2SmartHub#event:distance</a></li><li><a href="WeDo2SmartHub.html#event:rotate">WeDo2SmartHub#event:rotate</a></li><li><a href="WeDo2SmartHub.html#event:tilt">WeDo2SmartHub#event:tilt</a></li>
</ul>
</li>
<li class="dropdown">
<a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b class="caret"></b></a>
<ul class="dropdown-menu ">
<li><a href="global.html">Global</a></li>
</ul>
</li>
</ul>
<div class="col-sm-3 col-md-3">
<form class="navbar-form" role="search">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="q" id="search-input">
<div class="input-group-btn">
<button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="container" id="toc-content">
<div class="row">
<div class="col-md-12">
<div id="main">
<h1 class="page-title">Source: hub.js</h1>
<section>
<article>
<pre
class="sunlight-highlight-javascript linenums">"use strict";
var __importStar = (this &amp;&amp; this.__importStar) || function (mod) {
if (mod &amp;&amp; mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const Consts = __importStar(require("./consts"));
const Debug = require("debug");
const debug = Debug("hub");
/**
* @class Hub
* @extends EventEmitter
*/
class Hub extends events_1.EventEmitter {
constructor(device, autoSubscribe = true) {
super();
this.autoSubscribe = true;
this.useSpeedMap = true;
this.type = Consts.HubType.UNKNOWN;
this._ports = {};
this._virtualPorts = {};
this._name = "";
this._firmwareInfo = { major: 0, minor: 0, bugFix: 0, build: 0 };
this._batteryLevel = 100;
this._voltage = 0;
this._current = 0;
this._rssi = -100;
this._isConnecting = false;
this._isConnected = false;
this.autoSubscribe = !!autoSubscribe;
this._bleDevice = device;
device.on("disconnect", () => {
this.emit("disconnect");
});
}
/**
* @readonly
* @property {string} name Name of the hub
*/
get name() {
return this._bleDevice.name;
}
/**
* @readonly
* @property {string} firmwareVersion Firmware version of the hub
*/
get firmwareVersion() {
return `${this._firmwareInfo.major}.${this._firmwareInfo.minor}.${this._lpad(this._firmwareInfo.bugFix.toString(), 2)}.${this._lpad(this._firmwareInfo.build.toString(), 4)}`;
}
/**
* @readonly
* @property {string} uuid UUID of the hub
*/
get uuid() {
return this._bleDevice.uuid;
}
/**
* @readonly
* @property {number} rssi Signal strength of the hub
*/
get rssi() {
return this._rssi;
}
/**
* @readonly
* @property {number} batteryLevel Battery level of the hub (Percentage between 0-100)
*/
get batteryLevel() {
return this._batteryLevel;
}
/**
* @readonly
* @property {number} voltage Voltage of the hub (Volts)
*/
get voltage() {
return this._voltage;
}
/**
* @readonly
* @property {number} current Current usage of the hub (Milliamps)
*/
get current() {
return this._current;
}
/**
* Connect to the Hub.
* @method Hub#connect
* @returns {Promise} Resolved upon successful connect.
*/
connect() {
return new Promise(async (connectResolve, connectReject) => {
if (this._bleDevice.connecting) {
return connectReject("Already connecting");
}
else if (this._bleDevice.connected) {
return connectReject("Already connected");
}
this._isConnecting = true;
await this._bleDevice.connect();
return connectResolve();
});
}
/**
* Disconnect the Hub.
* @method Hub#disconnect
* @returns {Promise} Resolved upon successful disconnect.
*/
async disconnect() {
this.emit("disconnect");
this._bleDevice.disconnect();
}
/**
* Subscribe to sensor notifications on a given port.
* @method Hub#subscribe
* @param {string} port
* @param {number} [mode] The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.
* @returns {Promise} Resolved upon successful issuance of command.
*/
subscribe(port, mode) {
return new Promise((resolve, reject) => {
let newMode = this._getModeForDeviceType(this._portLookup(port).type);
if (mode) {
newMode = mode;
}
this._activatePortDevice(this._portLookup(port).value, this._portLookup(port).type, newMode, 0x00, () => {
return resolve();
});
});
}
/**
* Unsubscribe to sensor notifications on a given port.
* @method Hub#unsubscribe
* @param {string} port
* @returns {Promise} Resolved upon successful issuance of command.
*/
unsubscribe(port) {
return new Promise((resolve, reject) => {
const mode = this._getModeForDeviceType(this._portLookup(port).type);
this._deactivatePortDevice(this._portLookup(port).value, this._portLookup(port).type, mode, 0x00, () => {
return resolve();
});
});
}
/**
* Sleep a given amount of time.
*
* This is a helper method to make it easier to add delays into a chain of commands.
* @method Hub#sleep
* @param {number} delay How long to sleep (in milliseconds).
* @returns {Promise} Resolved after the delay is finished.
*/
sleep(delay) {
return new Promise((resolve) => {
global.setTimeout(resolve, delay);
});
}
/**
* Wait until a given list of concurrently running commands are complete.
*
* This is a helper method to make it easier to wait for concurrent commands to complete.
* @method Hub#wait
* @param {Array&lt;Promise&lt;any>>} commands Array of executing commands.
* @returns {Promise} Resolved after the commands are finished.
*/
wait(commands) {
return Promise.all(commands);
}
/**
* Get the hub type.
* @method Hub#getHubType
* @returns {HubType}
*/
getHubType() {
return this.type;
}
/**
* Get the device type for a given port.
* @method Hub#getPortDeviceType
* @param {string} port
* @returns {DeviceType}
*/
getPortDeviceType(port) {
return this._portLookup(port).type;
}
// protected _getCharacteristic (uuid: string) {
// return this._characteristics[uuid.replace(/-/g, "")];
// }
// protected _subscribeToCharacteristic (characteristic: Characteristic, callback: (data: Buffer) => void) {
// characteristic.on("data", (data: Buffer) => {
// return callback(data);
// });
// characteristic.subscribe((err) => {
// if (err) {
// this.emit("error", err);
// }
// });
// }
_activatePortDevice(port, type, mode, format, callback) {
if (callback) {
callback();
}
}
_deactivatePortDevice(port, type, mode, format, callback) {
if (callback) {
callback();
}
}
_registerDeviceAttachment(port, type) {
if (port.connected) {
port.type = type;
if (this.autoSubscribe) {
this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00);
/**
* Emits when a motor or sensor is attached to the Hub.
* @event Hub#attach
* @param {string} port
* @param {DeviceType} type
*/
this.emit("attach", port.id, type);
}
}
else {
port.type = Consts.DeviceType.UNKNOWN;
debug(`Port ${port.id} disconnected`);
/**
* Emits when an attached motor or sensor is detached from the Hub.
* @event Hub#detach
* @param {string} port
*/
if (this._virtualPorts[port.id]) {
delete this._virtualPorts[port.id];
}
this.emit("detach", port.id);
}
}
_getPortForPortNumber(num) {
for (const key of Object.keys(this._ports)) {
if (this._ports[key].value === num) {
return this._ports[key];
}
}
for (const key of Object.keys(this._virtualPorts)) {
if (this._virtualPorts[key].value === num) {
return this._virtualPorts[key];
}
}
return false;
}
_mapSpeed(speed) {
if (!this.useSpeedMap) {
return speed;
}
if (speed === 127) {
return 127; // Hard stop
}
if (speed > 100) {
speed = 100;
}
else if (speed &lt; -100) {
speed = -100;
}
return speed;
}
_calculateRamp(fromSpeed, toSpeed, time, port) {
const emitter = new events_1.EventEmitter();
const steps = Math.abs(toSpeed - fromSpeed);
let delay = time / steps;
let increment = 1;
if (delay &lt; 50 &amp;&amp; steps > 0) {
increment = 50 / delay;
delay = 50;
}
if (fromSpeed > toSpeed) {
increment = -increment;
}
let i = 0;
const interval = setInterval(() => {
let speed = Math.round(fromSpeed + (++i * increment));
if (toSpeed > fromSpeed &amp;&amp; speed > toSpeed) {
speed = toSpeed;
}
else if (fromSpeed > toSpeed &amp;&amp; speed &lt; toSpeed) {
speed = toSpeed;
}
emitter.emit("changeSpeed", speed);
if (speed === toSpeed) {
clearInterval(interval);
emitter.emit("finished");
}
}, delay);
port.setEventTimer(interval);
return emitter;
}
_portLookup(port) {
if (!this._ports[port.toUpperCase()] &amp;&amp; !this._virtualPorts[port.toUpperCase()]) {
throw new Error(`Port ${port.toUpperCase()} does not exist on this Hub type`);
}
return this._ports[port] || this._virtualPorts[port];
}
_lpad(str, length) {
while (str.length &lt; length) {
str = "0" + str;
}
return str;
}
_getModeForDeviceType(type) {
switch (type) {
case Consts.DeviceType.BASIC_MOTOR:
return 0x02;
case Consts.DeviceType.TRAIN_MOTOR:
return 0x02;
case Consts.DeviceType.BOOST_TACHO_MOTOR:
return 0x02;
case Consts.DeviceType.BOOST_MOVE_HUB_MOTOR:
return 0x02;
case Consts.DeviceType.CONTROL_PLUS_LARGE_MOTOR:
return 0x02;
case Consts.DeviceType.CONTROL_PLUS_XLARGE_MOTOR:
return 0x02;
case Consts.DeviceType.BOOST_DISTANCE:
return (this.type === Consts.HubType.WEDO2_SMART_HUB ? 0x00 : 0x08);
case Consts.DeviceType.BOOST_TILT:
return 0x04;
default:
return 0x00;
}
}
}
exports.Hub = Hub;
//# sourceMappingURL=hub.js.map</pre>
</article>
</section>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="modal fade" id="searchResults">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Search results</h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
<footer>
<span class="copyright">
node-poweredup by Nathan Kellenicki licensed under the MIT license.
</span>
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a>
on Sun Nov 10th 2019
using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
</footer>
<script src="scripts/docstrap.lib.js"></script>
<script src="scripts/toc.js"></script>
<script type="text/javascript" src="scripts/fulltext-search-ui.js"></script>
<script>
$( function () {
$( "[id*='$']" ).each( function () {
var $this = $( this );
$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
} );
$( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () {
var $this = $( this );
var example = $this.find( "code" );
exampleText = example.html();
var lang = /{@lang (.*?)}/.exec( exampleText );
if ( lang && lang[1] ) {
exampleText = exampleText.replace( lang[0], "" );
example.html( exampleText );
lang = lang[1];
} else {
var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/);
lang = langClassMatch ? langClassMatch[1] : "javascript";
}
if ( lang ) {
$this
.addClass( "sunlight-highlight-" + lang )
.addClass( "linenums" )
.html( example.html() );
}
} );
Sunlight.highlightAll( {
lineNumbers : true,
showMenu : true,
enableDoclinks : true
} );
$.catchAnchorLinks( {
navbarOffset: 10
} );
$( "#toc" ).toc( {
anchorName : function ( i, heading, prefix ) {
return $( heading ).attr( "id" ) || ( prefix + i );
},
selectors : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4",
showAndHide : false,
smoothScrolling: true
} );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
$( '.dropdown-toggle' ).dropdown();
$( "table" ).each( function () {
var $this = $( this );
$this.addClass('table');
} );
} );
</script>
<!--Navigation and Symbol Display-->
<!--Google Analytics-->
<script type="text/javascript">
$(document).ready(function() {
SearcherDisplay.init();
});
</script>
</body>
</html>