diff --git a/package.json b/package.json index 95b6b8f..da6091c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-poweredup", - "version": "1.5.0", + "version": "1.5.1", "description": "A Node.js module to interface with LEGO Powered UP components.", "homepage": "https://github.com/nathankellenicki/node-poweredup/", "main": "dist/index.js", diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 71f4b6c..3277edd 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -240,7 +240,7 @@ export class LPF2Hub extends Hub { if ((data[3] === 0x3b && this.type === Consts.HubType.POWERED_UP_REMOTE) || (data[3] === 0x3c && this.type !== Consts.HubType.POWERED_UP_REMOTE)) { // Voltage data = this._padMessage(data, 6); - const batteryLevel = (data.readUInt16LE(4) / 4096) * 100; + const batteryLevel = data.readUInt16LE(4) / 400; this._batteryLevel = Math.floor(batteryLevel); return; } else if (data[3] === 0x3b && this.type !== Consts.HubType.POWERED_UP_REMOTE) { // Current (Non-PUP Remote) diff --git a/src/poweredup.ts b/src/poweredup.ts index 942d99b..a5c6327 100644 --- a/src/poweredup.ts +++ b/src/poweredup.ts @@ -19,6 +19,7 @@ import noble = require("noble-mac"); let ready = false; let wantScan = false; +let discoveryEventAttached = false; const startScanning = () => { if (isBrowserContext) { @@ -55,6 +56,7 @@ export class PoweredUP extends EventEmitter { constructor () { super(); + this._discoveryEventHandler = this._discoveryEventHandler.bind(this); } @@ -65,53 +67,10 @@ export class PoweredUP extends EventEmitter { public scan () { wantScan = true; - noble.on("discover", async (peripheral: Peripheral) => { - - let hub: Hub; - - if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) { - hub = new WeDo2SmartHub(peripheral, this.autoSubscribe); - } else if (await BoostMoveHub.IsBoostMoveHub(peripheral)) { - hub = new BoostMoveHub(peripheral, this.autoSubscribe); - } else if (await PUPHub.IsPUPHub(peripheral)) { - hub = new PUPHub(peripheral, this.autoSubscribe); - } else if (await PUPRemote.IsPUPRemote(peripheral)) { - hub = new PUPRemote(peripheral, this.autoSubscribe); - } else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) { - hub = new DuploTrainBase(peripheral, this.autoSubscribe); - } else { - return; - } - - peripheral.removeAllListeners(); - noble.stopScanning(); - if (!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); - - }); + if (!discoveryEventAttached) { + noble.on("discover", this._discoveryEventHandler); + discoveryEventAttached = true; + } if (ready) { debug("Scanning started"); @@ -126,6 +85,12 @@ export class PoweredUP extends EventEmitter { */ public stop () { wantScan = false; + + if (discoveryEventAttached) { + noble.removeListener("discover", this._discoveryEventHandler); + discoveryEventAttached = false; + } + noble.stopScanning(); } @@ -151,4 +116,53 @@ export class PoweredUP extends EventEmitter { } + private async _discoveryEventHandler (peripheral: Peripheral) { + + let hub: Hub; + + if (await WeDo2SmartHub.IsWeDo2SmartHub(peripheral)) { + hub = new WeDo2SmartHub(peripheral, this.autoSubscribe); + } else if (await BoostMoveHub.IsBoostMoveHub(peripheral)) { + hub = new BoostMoveHub(peripheral, this.autoSubscribe); + } else if (await PUPHub.IsPUPHub(peripheral)) { + hub = new PUPHub(peripheral, this.autoSubscribe); + } else if (await PUPRemote.IsPUPRemote(peripheral)) { + hub = new PUPRemote(peripheral, this.autoSubscribe); + } else if (await DuploTrainBase.IsDuploTrainBase(peripheral)) { + hub = new DuploTrainBase(peripheral, this.autoSubscribe); + } else { + return; + } + + peripheral.removeAllListeners(); + noble.stopScanning(); + if (!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); + +} + + }