Fixed disconnect bug, made discovery better
This commit is contained in:
parent
d5707c7a23
commit
d917f9cb12
16
DOCS.md
16
DOCS.md
@ -20,8 +20,8 @@
|
||||
* [LPF2](#LPF2) ⇐ <code>EventEmitter</code>
|
||||
* [.scan()](#LPF2+scan)
|
||||
* [.stop()](#LPF2+stop)
|
||||
* [.getConnectedDeviceByUUID(uuid)](#LPF2+getConnectedDeviceByUUID) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||
* [.getConnectedDevices()](#LPF2+getConnectedDevices) ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||
* [.getConnectedHubByUUID(uuid)](#LPF2+getConnectedHubByUUID) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||
* [.getConnectedHubs()](#LPF2+getConnectedHubs) ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||
* ["discover" (hub)](#LPF2+event_discover)
|
||||
|
||||
<a name="LPF2+scan"></a>
|
||||
@ -36,10 +36,10 @@ Begin scanning for LPF2 Hub devices.
|
||||
Stop scanning for LPF2 Hub devices.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
<a name="LPF2+getConnectedDeviceByUUID"></a>
|
||||
<a name="LPF2+getConnectedHubByUUID"></a>
|
||||
|
||||
### lpF2.getConnectedDeviceByUUID(uuid) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||
Retrieve a LPF2 Hub device by UUID.
|
||||
### lpF2.getConnectedHubByUUID(uuid) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||
Retrieve a LPF2 Hub by UUID.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
|
||||
@ -47,10 +47,10 @@ Retrieve a LPF2 Hub device by UUID.
|
||||
| --- | --- |
|
||||
| uuid | <code>string</code> |
|
||||
|
||||
<a name="LPF2+getConnectedDevices"></a>
|
||||
<a name="LPF2+getConnectedHubs"></a>
|
||||
|
||||
### lpF2.getConnectedDevices() ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||
Retrieve a list of LPF2 Hub devices.
|
||||
### lpF2.getConnectedHubs() ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||
Retrieve a list of LPF2 Hubs.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
<a name="LPF2+event_discover"></a>
|
||||
|
16
README.md
16
README.md
@ -70,8 +70,8 @@ Thanks go to Jorge Pereira ([@JorgePe](https://github.com/JorgePe)), Sebastian R
|
||||
* [LPF2](#LPF2) ⇐ <code>EventEmitter</code>
|
||||
* [.scan()](#LPF2+scan)
|
||||
* [.stop()](#LPF2+stop)
|
||||
* [.getConnectedDeviceByUUID(uuid)](#LPF2+getConnectedDeviceByUUID) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||
* [.getConnectedDevices()](#LPF2+getConnectedDevices) ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||
* [.getConnectedHubByUUID(uuid)](#LPF2+getConnectedHubByUUID) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||
* [.getConnectedHubs()](#LPF2+getConnectedHubs) ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||
* ["discover" (hub)](#LPF2+event_discover)
|
||||
|
||||
<a name="LPF2+scan"></a>
|
||||
@ -86,10 +86,10 @@ Begin scanning for LPF2 Hub devices.
|
||||
Stop scanning for LPF2 Hub devices.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
<a name="LPF2+getConnectedDeviceByUUID"></a>
|
||||
<a name="LPF2+getConnectedHubByUUID"></a>
|
||||
|
||||
### lpF2.getConnectedDeviceByUUID(uuid) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||
Retrieve a LPF2 Hub device by UUID.
|
||||
### lpF2.getConnectedHubByUUID(uuid) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||
Retrieve a LPF2 Hub by UUID.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
|
||||
@ -97,10 +97,10 @@ Retrieve a LPF2 Hub device by UUID.
|
||||
| --- | --- |
|
||||
| uuid | <code>string</code> |
|
||||
|
||||
<a name="LPF2+getConnectedDevices"></a>
|
||||
<a name="LPF2+getConnectedHubs"></a>
|
||||
|
||||
### lpF2.getConnectedDevices() ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||
Retrieve a list of LPF2 Hub devices.
|
||||
### lpF2.getConnectedHubs() ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||
Retrieve a list of LPF2 Hubs.
|
||||
|
||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||
<a name="LPF2+event_discover"></a>
|
||||
|
@ -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
|
||||
})
|
||||
|
6
hub.ts
6
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[]) => {
|
||||
|
26
lpf2.ts
26
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];
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user