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">
<li>
<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>
</ul>
</dd>
@ -4310,7 +4310,7 @@
<ul class="dummy">
<li>
<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>
</ul>
</dd>
@ -4619,7 +4619,7 @@
<ul class="dummy">
<li>
<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>
</ul>
</dd>
@ -4781,7 +4781,7 @@
<ul class="dummy">
<li>
<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>
</ul>
</dd>
@ -4967,7 +4967,7 @@
<ul class="dummy">
<li>
<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>
</ul>
</dd>

View File

@ -5783,7 +5783,7 @@
<ul class="dummy">
<li>
<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>
</ul>
</dd>
@ -5945,7 +5945,7 @@
<ul class="dummy">
<li>
<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>
</ul>
</dd>
@ -6254,7 +6254,7 @@
<ul class="dummy">
<li>
<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>
</ul>
</dd>
@ -6416,7 +6416,7 @@
<ul class="dummy">
<li>
<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>
</ul>
</dd>
@ -6602,7 +6602,7 @@
<ul class="dummy">
<li>
<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>
</ul>
</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
setTimeout(() => {
this._name = peripheral.advertisement.localName;
this.emit("ready");
}, 200);
this.emit("discoverComplete");
}, 500);
}
/**
* @readonly

View File

@ -207,18 +207,18 @@ class PoweredUP extends events_1.EventEmitter {
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();
}
});
hub.on("ready", () => {
hub.on("discoverComplete", () => {
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.

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) {
const characteristic = this._getCharacteristic(uuid);
if (characteristic) {
debug(`Sent Message (${uuid})`, message);
if (debug.enabled) {
debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
}
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) {
debug("Received Message (WEDO2_HIGH_CURRENT_ALERT)", data);
// console.log(data);

View File

@ -1,6 +1,6 @@
{
"name": "node-poweredup",
"version": "1.7.2",
"version": "1.7.3",
"description": "A Node.js module to interface with LEGO Powered UP components.",
"homepage": "https://github.com/nathankellenicki/node-poweredup/",
"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
setTimeout(() => {
this._name = peripheral.advertisement.localName;
this.emit("ready");
}, 200);
this.emit("discoverComplete");
}, 500);
}

View File

@ -140,28 +140,31 @@ export class PoweredUP extends EventEmitter {
startScanning();
}
hub.on("connect", () => {
debug(`Hub ${hub.uuid} connected`);
this._connectedHubs[hub.uuid] = hub;
});
hub.on("discoverComplete", () => {
hub.on("disconnect", () => {
debug(`Hub ${hub.uuid} disconnected`);
delete this._connectedHubs[hub.uuid];
hub.on("connect", () => {
debug(`Hub ${hub.uuid} connected`);
this._connectedHubs[hub.uuid] = hub;
});
if (wantScan) {
startScanning();
}
});
hub.on("disconnect", () => {
debug(`Hub ${hub.uuid} disconnected`);
delete this._connectedHubs[hub.uuid];
if (wantScan) {
startScanning();
}
});
hub.on("ready", () => {
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);
});
}

View File

@ -266,12 +266,26 @@ export class WeDo2SmartHub extends Hub {
private _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
const characteristic = this._getCharacteristic(uuid);
if (characteristic) {
debug(`Sent Message (${uuid})`, message);
if (debug.enabled) {
debug(`Sent Message (${this._getCharacteristicNameFromUUID(uuid)})`, message);
}
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) {
debug("Received Message (WEDO2_HIGH_CURRENT_ALERT)", data);
// console.log(data);