diff --git a/DOCS.md b/DOCS.md index cd48484..aced8b9 100644 --- a/DOCS.md +++ b/DOCS.md @@ -20,8 +20,8 @@ * [LPF2](#LPF2) ⇐ EventEmitter * [.scan()](#LPF2+scan) * [.stop()](#LPF2+stop) - * [.getConnectedDeviceByUUID(uuid)](#LPF2+getConnectedDeviceByUUID) ⇒ [Hub](#Hub) \| null - * [.getConnectedDevices()](#LPF2+getConnectedDevices) ⇒ [Array.<Hub>](#Hub) + * [.getConnectedHubByUUID(uuid)](#LPF2+getConnectedHubByUUID) ⇒ [Hub](#Hub) \| null + * [.getConnectedHubs()](#LPF2+getConnectedHubs) ⇒ [Array.<Hub>](#Hub) * ["discover" (hub)](#LPF2+event_discover) @@ -36,10 +36,10 @@ Begin scanning for LPF2 Hub devices. Stop scanning for LPF2 Hub devices. **Kind**: instance method of [LPF2](#LPF2) - + -### lpF2.getConnectedDeviceByUUID(uuid) ⇒ [Hub](#Hub) \| null -Retrieve a LPF2 Hub device by UUID. +### lpF2.getConnectedHubByUUID(uuid) ⇒ [Hub](#Hub) \| null +Retrieve a LPF2 Hub by UUID. **Kind**: instance method of [LPF2](#LPF2) @@ -47,10 +47,10 @@ Retrieve a LPF2 Hub device by UUID. | --- | --- | | uuid | string | - + -### lpF2.getConnectedDevices() ⇒ [Array.<Hub>](#Hub) -Retrieve a list of LPF2 Hub devices. +### lpF2.getConnectedHubs() ⇒ [Array.<Hub>](#Hub) +Retrieve a list of LPF2 Hubs. **Kind**: instance method of [LPF2](#LPF2) diff --git a/README.md b/README.md index 76961be..75d435f 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,8 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R * [LPF2](#LPF2) ⇐ EventEmitter * [.scan()](#LPF2+scan) * [.stop()](#LPF2+stop) - * [.getConnectedDeviceByUUID(uuid)](#LPF2+getConnectedDeviceByUUID) ⇒ [Hub](#Hub) \| null - * [.getConnectedDevices()](#LPF2+getConnectedDevices) ⇒ [Array.<Hub>](#Hub) + * [.getConnectedHubByUUID(uuid)](#LPF2+getConnectedHubByUUID) ⇒ [Hub](#Hub) \| null + * [.getConnectedHubs()](#LPF2+getConnectedHubs) ⇒ [Array.<Hub>](#Hub) * ["discover" (hub)](#LPF2+event_discover) @@ -86,10 +86,10 @@ Begin scanning for LPF2 Hub devices. Stop scanning for LPF2 Hub devices. **Kind**: instance method of [LPF2](#LPF2) - + -### lpF2.getConnectedDeviceByUUID(uuid) ⇒ [Hub](#Hub) \| null -Retrieve a LPF2 Hub device by UUID. +### lpF2.getConnectedHubByUUID(uuid) ⇒ [Hub](#Hub) \| null +Retrieve a LPF2 Hub by UUID. **Kind**: instance method of [LPF2](#LPF2) @@ -97,10 +97,10 @@ Retrieve a LPF2 Hub device by UUID. | --- | --- | | uuid | string | - + -### lpF2.getConnectedDevices() ⇒ [Array.<Hub>](#Hub) -Retrieve a list of LPF2 Hub devices. +### lpF2.getConnectedHubs() ⇒ [Array.<Hub>](#Hub) +Retrieve a list of LPF2 Hubs. **Kind**: instance method of [LPF2](#LPF2) diff --git a/examples/leds.js b/examples/leds.js index ef1b323..ff36d0b 100644 --- a/examples/leds.js +++ b/examples/leds.js @@ -8,14 +8,18 @@ console.log("Looking for Hubs..."); lpf2.on("discover", async (hub) => { // Wait to discover hubs await hub.connect(); // Connect to hub - console.log("Connected to Hub!"); + console.log(`Connected to ${hub.name}!`); + + hub.on("disconnect", () => { + console.log("Hub disconnected"); + }) }); let color = 0; setInterval(() => { - const hubs = lpf2.getConnectedDevices(); // Get an array of all connected hubs + const hubs = lpf2.getConnectedHubs(); // Get an array of all connected hubs hubs.forEach((hub) => { hub.setLEDColor(color); // Set the color }) diff --git a/hub.ts b/hub.ts index 4b38fad..c28f964 100644 --- a/hub.ts +++ b/hub.ts @@ -20,6 +20,7 @@ export class Hub extends EventEmitter { public useSpeedMap: boolean = true; public type: Consts.Hubs = Consts.Hubs.UNKNOWN; public uuid: string; + public name: string; protected _ports: {[port: string]: Port} = {}; protected _characteristics: {[uuid: string]: Characteristic} = {}; @@ -33,6 +34,7 @@ export class Hub extends EventEmitter { this.autoSubscribe = !!autoSubscribe; this._peripheral = peripheral; this.uuid = peripheral.uuid; + this.name = peripheral.advertisement.localName; } @@ -63,8 +65,8 @@ export class Hub extends EventEmitter { }, 2000); self._peripheral.on("disconnect", () => { - clearInterval(rssiUpdateInterval); - this.emit("disconnect"); + clearInterval(rssiUpdateInterval); + this.emit("disconnect"); }); self._peripheral.discoverServices([], (err: string, services: Service[]) => { diff --git a/lpf2.ts b/lpf2.ts index 8493bb4..4790fb9 100644 --- a/lpf2.ts +++ b/lpf2.ts @@ -37,7 +37,7 @@ export class LPF2 extends EventEmitter { public autoSubscribe: boolean = true; - private _connectedDevices: {[uuid: string]: Hub} = {}; + private _connectedHubs: {[uuid: string]: Hub} = {}; constructor () { @@ -70,18 +70,16 @@ export class LPF2 extends EventEmitter { hub.on("connect", () => { debug(`Hub ${hub.uuid} connected`); - this._connectedDevices[hub.uuid] = hub; + this._connectedHubs[hub.uuid] = hub; }); hub.on("disconnect", () => { debug(`Hub ${hub.uuid} disconnected`); - delete this._connectedDevices[hub.uuid]; + delete this._connectedHubs[hub.uuid]; if (wantScan) { noble.startScanning(); } - - hub.emit("disconnect"); }); debug(`Hub ${hub.uuid} discovered`); @@ -112,24 +110,24 @@ export class LPF2 extends EventEmitter { /** - * Retrieve a LPF2 Hub device by UUID. - * @method LPF2#getConnectedDeviceByUUID + * Retrieve a LPF2 Hub by UUID. + * @method LPF2#getConnectedHubByUUID * @param {string} uuid * @returns {Hub | null} */ - public getConnectedDeviceByUUID (uuid: string) { - return this._connectedDevices[uuid]; + public getConnectedHubByUUID (uuid: string) { + return this._connectedHubs[uuid]; } /** - * Retrieve a list of LPF2 Hub devices. - * @method LPF2#getConnectedDevices + * Retrieve a list of LPF2 Hubs. + * @method LPF2#getConnectedHubs * @returns {Hub[]} */ - public getConnectedDevices () { - return Object.keys(this._connectedDevices).map((uuid) => { - return this._connectedDevices[uuid]; + public getConnectedHubs () { + return Object.keys(this._connectedHubs).map((uuid) => { + return this._connectedHubs[uuid]; }); }