196 lines
8.3 KiB
HTML
196 lines
8.3 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: poweredup.js</title>
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="main">
|
|
|
|
<h1 class="page-title">Source: poweredup.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source linenums"><code>"use strict";
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && 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 boostmovehub_1 = require("./boostmovehub");
|
|
const duplotrainbase_1 = require("./duplotrainbase");
|
|
const puphub_1 = require("./puphub");
|
|
const pupremote_1 = require("./pupremote");
|
|
const wedo2smarthub_1 = require("./wedo2smarthub");
|
|
const utils_1 = require("./utils");
|
|
const Consts = __importStar(require("./consts"));
|
|
const events_1 = require("events");
|
|
const Debug = require("debug");
|
|
const debug = Debug("PoweredUP");
|
|
const noble = require("noble-mac");
|
|
let ready = false;
|
|
let wantScan = false;
|
|
let discoveryEventAttached = false;
|
|
const startScanning = () => {
|
|
if (utils_1.isBrowserContext) {
|
|
noble.startScanning([Consts.BLEService.WEDO2_SMART_HUB, Consts.BLEService.LPF2_HUB]);
|
|
}
|
|
else {
|
|
noble.startScanning();
|
|
}
|
|
};
|
|
noble.on("stateChange", (state) => {
|
|
ready = (state === "poweredOn");
|
|
if (ready) {
|
|
if (wantScan) {
|
|
debug("Scanning started");
|
|
startScanning();
|
|
}
|
|
}
|
|
else {
|
|
noble.stopScanning();
|
|
}
|
|
});
|
|
/**
|
|
* @class PoweredUP
|
|
* @extends EventEmitter
|
|
*/
|
|
class PoweredUP extends events_1.EventEmitter {
|
|
constructor() {
|
|
super();
|
|
this.autoSubscribe = true;
|
|
this._connectedHubs = {};
|
|
this._discoveryEventHandler = this._discoveryEventHandler.bind(this);
|
|
}
|
|
/**
|
|
* Begin scanning for Powered UP Hub devices.
|
|
* @method PoweredUP#scan
|
|
*/
|
|
scan() {
|
|
wantScan = true;
|
|
if (!discoveryEventAttached) {
|
|
noble.on("discover", this._discoveryEventHandler);
|
|
discoveryEventAttached = true;
|
|
}
|
|
if (ready) {
|
|
debug("Scanning started");
|
|
startScanning();
|
|
}
|
|
}
|
|
/**
|
|
* Stop scanning for Powered UP Hub devices.
|
|
* @method PoweredUP#stop
|
|
*/
|
|
stop() {
|
|
wantScan = false;
|
|
if (discoveryEventAttached) {
|
|
noble.removeListener("discover", this._discoveryEventHandler);
|
|
discoveryEventAttached = false;
|
|
}
|
|
noble.stopScanning();
|
|
}
|
|
/**
|
|
* Retrieve a Powered UP Hub by UUID.
|
|
* @method PoweredUP#getConnectedHubByUUID
|
|
* @param {string} uuid
|
|
* @returns {Hub | null}
|
|
*/
|
|
getConnectedHubByUUID(uuid) {
|
|
return this._connectedHubs[uuid];
|
|
}
|
|
/**
|
|
* Retrieve a list of Powered UP Hubs.
|
|
* @method PoweredUP#getConnectedHubs
|
|
* @returns {Hub[]}
|
|
*/
|
|
getConnectedHubs() {
|
|
return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]);
|
|
}
|
|
async _discoveryEventHandler(peripheral) {
|
|
let hub;
|
|
if (await wedo2smarthub_1.WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) {
|
|
hub = new wedo2smarthub_1.WeDo2SmartHub(peripheral, this.autoSubscribe);
|
|
}
|
|
else if (await boostmovehub_1.BoostMoveHub.IsBoostMoveHub(peripheral)) {
|
|
hub = new boostmovehub_1.BoostMoveHub(peripheral, this.autoSubscribe);
|
|
}
|
|
else if (await puphub_1.PUPHub.IsPUPHub(peripheral)) {
|
|
hub = new puphub_1.PUPHub(peripheral, this.autoSubscribe);
|
|
}
|
|
else if (await pupremote_1.PUPRemote.IsPUPRemote(peripheral)) {
|
|
hub = new pupremote_1.PUPRemote(peripheral, this.autoSubscribe);
|
|
}
|
|
else if (await duplotrainbase_1.DuploTrainBase.IsDuploTrainBase(peripheral)) {
|
|
hub = new duplotrainbase_1.DuploTrainBase(peripheral, this.autoSubscribe);
|
|
}
|
|
else {
|
|
return;
|
|
}
|
|
peripheral.removeAllListeners();
|
|
noble.stopScanning();
|
|
if (!utils_1.isBrowserContext) {
|
|
startScanning();
|
|
}
|
|
hub.on("connect", () => {
|
|
debug(`Hub ${hub.uuid} connected`);
|
|
this._connectedHubs[hub.uuid] = hub;
|
|
});
|
|
hub.on("disconnect", () => {
|
|
debug(`Hub ${hub.uuid} disconnected`);
|
|
delete this._connectedHubs[hub.uuid];
|
|
if (wantScan) {
|
|
startScanning();
|
|
}
|
|
});
|
|
debug(`Hub ${hub.uuid} discovered`);
|
|
/**
|
|
* Emits when a Powered UP Hub device is found.
|
|
* @event PoweredUP#discover
|
|
* @param {WeDo2SmartHub | BoostMoveHub | PUPHub | PUPRemote | DuploTrainBase} hub
|
|
*/
|
|
this.emit("discover", hub);
|
|
}
|
|
}
|
|
exports.PoweredUP = PoweredUP;
|
|
</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BoostMoveHub.html">BoostMoveHub</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><h3>Events</h3><ul><li><a href="BoostMoveHub.html#event:attach">attach</a></li><li><a href="BoostMoveHub.html#event:button">button</a></li><li><a href="BoostMoveHub.html#event:color">color</a></li><li><a href="BoostMoveHub.html#event:colorAndDistance">colorAndDistance</a></li><li><a href="BoostMoveHub.html#event:detach">detach</a></li><li><a href="BoostMoveHub.html#event:distance">distance</a></li><li><a href="BoostMoveHub.html#event:rotate">rotate</a></li><li><a href="BoostMoveHub.html#event:tilt">tilt</a></li><li><a href="DuploTrainBase.html#event:color">color</a></li><li><a href="DuploTrainBase.html#event:speed">speed</a></li><li><a href="Hub.html#event:attach">attach</a></li><li><a href="Hub.html#event:detach">detach</a></li><li><a href="LPF2Hub.html#event:attach">attach</a></li><li><a href="LPF2Hub.html#event:button">button</a></li><li><a href="LPF2Hub.html#event:color">color</a></li><li><a href="LPF2Hub.html#event:colorAndDistance">colorAndDistance</a></li><li><a href="LPF2Hub.html#event:detach">detach</a></li><li><a href="LPF2Hub.html#event:distance">distance</a></li><li><a href="LPF2Hub.html#event:rotate">rotate</a></li><li><a href="LPF2Hub.html#event:speed">speed</a></li><li><a href="LPF2Hub.html#event:tilt">tilt</a></li><li><a href="PoweredUP.html#event:discover">discover</a></li><li><a href="PUPHub.html#event:attach">attach</a></li><li><a href="PUPHub.html#event:button">button</a></li><li><a href="PUPHub.html#event:color">color</a></li><li><a href="PUPHub.html#event:colorAndDistance">colorAndDistance</a></li><li><a href="PUPHub.html#event:detach">detach</a></li><li><a href="PUPHub.html#event:distance">distance</a></li><li><a href="PUPHub.html#event:tilt">tilt</a></li><li><a href="PUPRemote.html#event:button">button</a></li><li><a href="PUPRemote.html#event:colorAndDistance">colorAndDistance</a></li><li><a href="WeDo2SmartHub.html#event:attach">attach</a></li><li><a href="WeDo2SmartHub.html#event:button">button</a></li><li><a href="WeDo2SmartHub.html#event:color">color</a></li><li><a href="WeDo2SmartHub.html#event:detach">detach</a></li><li><a href="WeDo2SmartHub.html#event:distance">distance</a></li><li><a href="WeDo2SmartHub.html#event:rotate">rotate</a></li><li><a href="WeDo2SmartHub.html#event:tilt">tilt</a></li></ul><h3><a href="global.html">Global</a></h3>
|
|
</nav>
|
|
|
|
<br class="clear">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Jan 18 2019 16:10:38 GMT-0800 (Pacific Standard Time)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|