Made discovery more reliable

This commit is contained in:
Nathan Kellenicki 2019-01-29 20:46:51 -08:00
parent 6d5ad34401
commit ba44189faa
10 changed files with 71 additions and 42 deletions

View File

@ -4148,7 +4148,7 @@
<ul class="dummy"> <ul class="dummy">
<li> <li>
<a href="wedo2smarthub.js.html">wedo2smarthub.js</a>, <a href="wedo2smarthub.js.html">wedo2smarthub.js</a>,
<a href="wedo2smarthub.js.html#sunlight-1-line-268">line 268</a> <a href="wedo2smarthub.js.html#sunlight-1-line-280">line 280</a>
</li> </li>
</ul> </ul>
</dd> </dd>
@ -4310,7 +4310,7 @@
<ul class="dummy"> <ul class="dummy">
<li> <li>
<a href="wedo2smarthub.js.html">wedo2smarthub.js</a>, <a href="wedo2smarthub.js.html">wedo2smarthub.js</a>,
<a href="wedo2smarthub.js.html#sunlight-1-line-313">line 313</a> <a href="wedo2smarthub.js.html#sunlight-1-line-325">line 325</a>
</li> </li>
</ul> </ul>
</dd> </dd>
@ -4619,7 +4619,7 @@
<ul class="dummy"> <ul class="dummy">
<li> <li>
<a href="wedo2smarthub.js.html">wedo2smarthub.js</a>, <a href="wedo2smarthub.js.html">wedo2smarthub.js</a>,
<a href="wedo2smarthub.js.html#sunlight-1-line-302">line 302</a> <a href="wedo2smarthub.js.html#sunlight-1-line-314">line 314</a>
</li> </li>
</ul> </ul>
</dd> </dd>
@ -4781,7 +4781,7 @@
<ul class="dummy"> <ul class="dummy">
<li> <li>
<a href="wedo2smarthub.js.html">wedo2smarthub.js</a>, <a href="wedo2smarthub.js.html">wedo2smarthub.js</a>,
<a href="wedo2smarthub.js.html#sunlight-1-line-343">line 343</a> <a href="wedo2smarthub.js.html#sunlight-1-line-355">line 355</a>
</li> </li>
</ul> </ul>
</dd> </dd>
@ -4967,7 +4967,7 @@
<ul class="dummy"> <ul class="dummy">
<li> <li>
<a href="wedo2smarthub.js.html">wedo2smarthub.js</a>, <a href="wedo2smarthub.js.html">wedo2smarthub.js</a>,
<a href="wedo2smarthub.js.html#sunlight-1-line-331">line 331</a> <a href="wedo2smarthub.js.html#sunlight-1-line-343">line 343</a>
</li> </li>
</ul> </ul>
</dd> </dd>

View File

@ -5783,7 +5783,7 @@
<ul class="dummy"> <ul class="dummy">
<li> <li>
<a href="wedo2smarthub.js.html">wedo2smarthub.js</a>, <a href="wedo2smarthub.js.html">wedo2smarthub.js</a>,
<a href="wedo2smarthub.js.html#sunlight-1-line-268">line 268</a> <a href="wedo2smarthub.js.html#sunlight-1-line-280">line 280</a>
</li> </li>
</ul> </ul>
</dd> </dd>
@ -5945,7 +5945,7 @@
<ul class="dummy"> <ul class="dummy">
<li> <li>
<a href="wedo2smarthub.js.html">wedo2smarthub.js</a>, <a href="wedo2smarthub.js.html">wedo2smarthub.js</a>,
<a href="wedo2smarthub.js.html#sunlight-1-line-313">line 313</a> <a href="wedo2smarthub.js.html#sunlight-1-line-325">line 325</a>
</li> </li>
</ul> </ul>
</dd> </dd>
@ -6254,7 +6254,7 @@
<ul class="dummy"> <ul class="dummy">
<li> <li>
<a href="wedo2smarthub.js.html">wedo2smarthub.js</a>, <a href="wedo2smarthub.js.html">wedo2smarthub.js</a>,
<a href="wedo2smarthub.js.html#sunlight-1-line-302">line 302</a> <a href="wedo2smarthub.js.html#sunlight-1-line-314">line 314</a>
</li> </li>
</ul> </ul>
</dd> </dd>
@ -6416,7 +6416,7 @@
<ul class="dummy"> <ul class="dummy">
<li> <li>
<a href="wedo2smarthub.js.html">wedo2smarthub.js</a>, <a href="wedo2smarthub.js.html">wedo2smarthub.js</a>,
<a href="wedo2smarthub.js.html#sunlight-1-line-343">line 343</a> <a href="wedo2smarthub.js.html#sunlight-1-line-355">line 355</a>
</li> </li>
</ul> </ul>
</dd> </dd>
@ -6602,7 +6602,7 @@
<ul class="dummy"> <ul class="dummy">
<li> <li>
<a href="wedo2smarthub.js.html">wedo2smarthub.js</a>, <a href="wedo2smarthub.js.html">wedo2smarthub.js</a>,
<a href="wedo2smarthub.js.html#sunlight-1-line-331">line 331</a> <a href="wedo2smarthub.js.html#sunlight-1-line-343">line 343</a>
</li> </li>
</ul> </ul>
</dd> </dd>

View File

@ -121,8 +121,8 @@ class Hub extends events_1.EventEmitter {
// NK: This hack allows LPF2.0 hubs to send a second advertisement packet consisting of the hub name before we try to read it // NK: This hack allows LPF2.0 hubs to send a second advertisement packet consisting of the hub name before we try to read it
setTimeout(() => { setTimeout(() => {
this._name = peripheral.advertisement.localName; this._name = peripheral.advertisement.localName;
this.emit("ready"); this.emit("discoverComplete");
}, 200); }, 500);
} }
/** /**
* @readonly * @readonly

View File

@ -207,6 +207,7 @@ class PoweredUP extends events_1.EventEmitter {
if (!utils_1.isBrowserContext) { if (!utils_1.isBrowserContext) {
startScanning(); startScanning();
} }
hub.on("discoverComplete", () => {
hub.on("connect", () => { hub.on("connect", () => {
debug(`Hub ${hub.uuid} connected`); debug(`Hub ${hub.uuid} connected`);
this._connectedHubs[hub.uuid] = hub; this._connectedHubs[hub.uuid] = hub;
@ -218,7 +219,6 @@ class PoweredUP extends events_1.EventEmitter {
startScanning(); startScanning();
} }
}); });
hub.on("ready", () => {
debug(`Hub ${hub.uuid} discovered`); debug(`Hub ${hub.uuid} discovered`);
/** /**
* Emits when a Powered UP Hub device is found. * Emits when a Powered UP Hub device is found.

File diff suppressed because one or more lines are too long

View File

@ -322,10 +322,22 @@ class WeDo2SmartHub extends hub_1.Hub {
_writeMessage(uuid, message, callback) { _writeMessage(uuid, message, callback) {
const characteristic = this._getCharacteristic(uuid); const characteristic = this._getCharacteristic(uuid);
if (characteristic) { if (characteristic) {
debug(`Sent Message (${uuid})`, message); if (debug.enabled) {
debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
}
characteristic.write(message, false, callback); characteristic.write(message, false, callback);
} }
} }
_getCharacteristicNameFromUUID(uuid) {
const keys = Object.keys(Consts.BLECharacteristic);
for (let i = 0; i &lt; keys.length; i++) {
const key = keys[i];
if (Consts.BLECharacteristic[key] === uuid) {
return key;
}
}
return "UNKNOWN";
}
_parseHighCurrentAlert(data) { _parseHighCurrentAlert(data) {
debug("Received Message (WEDO2_HIGH_CURRENT_ALERT)", data); debug("Received Message (WEDO2_HIGH_CURRENT_ALERT)", data);
// console.log(data); // console.log(data);

View File

@ -1,6 +1,6 @@
{ {
"name": "node-poweredup", "name": "node-poweredup",
"version": "1.7.2", "version": "1.7.3",
"description": "A Node.js module to interface with LEGO Powered UP components.", "description": "A Node.js module to interface with LEGO Powered UP components.",
"homepage": "https://github.com/nathankellenicki/node-poweredup/", "homepage": "https://github.com/nathankellenicki/node-poweredup/",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -49,8 +49,8 @@ export class Hub extends EventEmitter {
// NK: This hack allows LPF2.0 hubs to send a second advertisement packet consisting of the hub name before we try to read it // NK: This hack allows LPF2.0 hubs to send a second advertisement packet consisting of the hub name before we try to read it
setTimeout(() => { setTimeout(() => {
this._name = peripheral.advertisement.localName; this._name = peripheral.advertisement.localName;
this.emit("ready"); this.emit("discoverComplete");
}, 200); }, 500);
} }

View File

@ -140,6 +140,8 @@ export class PoweredUP extends EventEmitter {
startScanning(); startScanning();
} }
hub.on("discoverComplete", () => {
hub.on("connect", () => { hub.on("connect", () => {
debug(`Hub ${hub.uuid} connected`); debug(`Hub ${hub.uuid} connected`);
this._connectedHubs[hub.uuid] = hub; this._connectedHubs[hub.uuid] = hub;
@ -154,14 +156,15 @@ export class PoweredUP extends EventEmitter {
} }
}); });
hub.on("ready", () => {
debug(`Hub ${hub.uuid} discovered`); debug(`Hub ${hub.uuid} discovered`);
/** /**
* Emits when a Powered UP Hub device is found. * Emits when a Powered UP Hub device is found.
* @event PoweredUP#discover * @event PoweredUP#discover
* @param {WeDo2SmartHub | BoostMoveHub | PUPHub | PUPRemote | DuploTrainBase} hub * @param {WeDo2SmartHub | BoostMoveHub | PUPHub | PUPRemote | DuploTrainBase} hub
*/ */
this.emit("discover", hub); this.emit("discover", hub);
}); });
} }

View File

@ -266,12 +266,26 @@ export class WeDo2SmartHub extends Hub {
private _writeMessage (uuid: string, message: Buffer, callback?: () => void) { private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
const characteristic = this._getCharacteristic(uuid); const characteristic = this._getCharacteristic(uuid);
if (characteristic) { if (characteristic) {
debug(`Sent Message (${uuid})`, message); if (debug.enabled) {
debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
}
characteristic.write(message, false, callback); characteristic.write(message, false, callback);
} }
} }
private _getCharacteristicNameFromUUID (uuid: string) {
const keys = Object.keys(Consts.BLECharacteristic);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (Consts.BLECharacteristic[key as any] === uuid) {
return key;
}
}
return "UNKNOWN";
}
private _parseHighCurrentAlert (data: Buffer) { private _parseHighCurrentAlert (data: Buffer) {
debug("Received Message (WEDO2_HIGH_CURRENT_ALERT)", data); debug("Received Message (WEDO2_HIGH_CURRENT_ALERT)", data);
// console.log(data); // console.log(data);