Fixed bug where calling scan multiple times adds multiple discovery events, closes #10
This commit is contained in:
parent
b64f9800e4
commit
363eb5f4d0
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "node-poweredup",
|
"name": "node-poweredup",
|
||||||
"version": "1.5.0",
|
"version": "1.5.1",
|
||||||
"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",
|
||||||
|
@ -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
|
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);
|
data = this._padMessage(data, 6);
|
||||||
const batteryLevel = (data.readUInt16LE(4) / 4096) * 100;
|
const batteryLevel = data.readUInt16LE(4) / 400;
|
||||||
this._batteryLevel = Math.floor(batteryLevel);
|
this._batteryLevel = Math.floor(batteryLevel);
|
||||||
return;
|
return;
|
||||||
} else if (data[3] === 0x3b && this.type !== Consts.HubType.POWERED_UP_REMOTE) { // Current (Non-PUP Remote)
|
} else if (data[3] === 0x3b && this.type !== Consts.HubType.POWERED_UP_REMOTE) { // Current (Non-PUP Remote)
|
||||||
|
@ -19,6 +19,7 @@ import noble = require("noble-mac");
|
|||||||
|
|
||||||
let ready = false;
|
let ready = false;
|
||||||
let wantScan = false;
|
let wantScan = false;
|
||||||
|
let discoveryEventAttached = false;
|
||||||
|
|
||||||
const startScanning = () => {
|
const startScanning = () => {
|
||||||
if (isBrowserContext) {
|
if (isBrowserContext) {
|
||||||
@ -55,6 +56,7 @@ export class PoweredUP extends EventEmitter {
|
|||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
super();
|
super();
|
||||||
|
this._discoveryEventHandler = this._discoveryEventHandler.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -65,7 +67,56 @@ export class PoweredUP extends EventEmitter {
|
|||||||
public scan () {
|
public scan () {
|
||||||
wantScan = true;
|
wantScan = true;
|
||||||
|
|
||||||
noble.on("discover", async (peripheral: Peripheral) => {
|
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
|
||||||
|
*/
|
||||||
|
public 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}
|
||||||
|
*/
|
||||||
|
public getConnectedHubByUUID (uuid: string) {
|
||||||
|
return this._connectedHubs[uuid];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a list of Powered UP Hubs.
|
||||||
|
* @method PoweredUP#getConnectedHubs
|
||||||
|
* @returns {Hub[]}
|
||||||
|
*/
|
||||||
|
public getConnectedHubs () {
|
||||||
|
return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private async _discoveryEventHandler (peripheral: Peripheral) {
|
||||||
|
|
||||||
let hub: Hub;
|
let hub: Hub;
|
||||||
|
|
||||||
@ -111,43 +162,6 @@ export class PoweredUP extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
this.emit("discover", hub);
|
this.emit("discover", hub);
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
if (ready) {
|
|
||||||
debug("Scanning started");
|
|
||||||
startScanning();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop scanning for Powered UP Hub devices.
|
|
||||||
* @method PoweredUP#stop
|
|
||||||
*/
|
|
||||||
public stop () {
|
|
||||||
wantScan = false;
|
|
||||||
noble.stopScanning();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve a Powered UP Hub by UUID.
|
|
||||||
* @method PoweredUP#getConnectedHubByUUID
|
|
||||||
* @param {string} uuid
|
|
||||||
* @returns {Hub | null}
|
|
||||||
*/
|
|
||||||
public getConnectedHubByUUID (uuid: string) {
|
|
||||||
return this._connectedHubs[uuid];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve a list of Powered UP Hubs.
|
|
||||||
* @method PoweredUP#getConnectedHubs
|
|
||||||
* @returns {Hub[]}
|
|
||||||
*/
|
|
||||||
public getConnectedHubs () {
|
|
||||||
return Object.keys(this._connectedHubs).map((uuid) => this._connectedHubs[uuid]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user