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>
|
* [LPF2](#LPF2) ⇐ <code>EventEmitter</code>
|
||||||
* [.scan()](#LPF2+scan)
|
* [.scan()](#LPF2+scan)
|
||||||
* [.stop()](#LPF2+stop)
|
* [.stop()](#LPF2+stop)
|
||||||
* [.getConnectedDeviceByUUID(uuid)](#LPF2+getConnectedDeviceByUUID) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
* [.getConnectedHubByUUID(uuid)](#LPF2+getConnectedHubByUUID) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||||
* [.getConnectedDevices()](#LPF2+getConnectedDevices) ⇒ [<code>Array.<Hub></code>](#Hub)
|
* [.getConnectedHubs()](#LPF2+getConnectedHubs) ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||||
* ["discover" (hub)](#LPF2+event_discover)
|
* ["discover" (hub)](#LPF2+event_discover)
|
||||||
|
|
||||||
<a name="LPF2+scan"></a>
|
<a name="LPF2+scan"></a>
|
||||||
@ -36,10 +36,10 @@ Begin scanning for LPF2 Hub devices.
|
|||||||
Stop scanning for LPF2 Hub devices.
|
Stop scanning for LPF2 Hub devices.
|
||||||
|
|
||||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
**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>
|
### lpF2.getConnectedHubByUUID(uuid) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||||
Retrieve a LPF2 Hub device by UUID.
|
Retrieve a LPF2 Hub by UUID.
|
||||||
|
|
||||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||||
|
|
||||||
@ -47,10 +47,10 @@ Retrieve a LPF2 Hub device by UUID.
|
|||||||
| --- | --- |
|
| --- | --- |
|
||||||
| uuid | <code>string</code> |
|
| uuid | <code>string</code> |
|
||||||
|
|
||||||
<a name="LPF2+getConnectedDevices"></a>
|
<a name="LPF2+getConnectedHubs"></a>
|
||||||
|
|
||||||
### lpF2.getConnectedDevices() ⇒ [<code>Array.<Hub></code>](#Hub)
|
### lpF2.getConnectedHubs() ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||||
Retrieve a list of LPF2 Hub devices.
|
Retrieve a list of LPF2 Hubs.
|
||||||
|
|
||||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||||
<a name="LPF2+event_discover"></a>
|
<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>
|
* [LPF2](#LPF2) ⇐ <code>EventEmitter</code>
|
||||||
* [.scan()](#LPF2+scan)
|
* [.scan()](#LPF2+scan)
|
||||||
* [.stop()](#LPF2+stop)
|
* [.stop()](#LPF2+stop)
|
||||||
* [.getConnectedDeviceByUUID(uuid)](#LPF2+getConnectedDeviceByUUID) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
* [.getConnectedHubByUUID(uuid)](#LPF2+getConnectedHubByUUID) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||||
* [.getConnectedDevices()](#LPF2+getConnectedDevices) ⇒ [<code>Array.<Hub></code>](#Hub)
|
* [.getConnectedHubs()](#LPF2+getConnectedHubs) ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||||
* ["discover" (hub)](#LPF2+event_discover)
|
* ["discover" (hub)](#LPF2+event_discover)
|
||||||
|
|
||||||
<a name="LPF2+scan"></a>
|
<a name="LPF2+scan"></a>
|
||||||
@ -86,10 +86,10 @@ Begin scanning for LPF2 Hub devices.
|
|||||||
Stop scanning for LPF2 Hub devices.
|
Stop scanning for LPF2 Hub devices.
|
||||||
|
|
||||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
**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>
|
### lpF2.getConnectedHubByUUID(uuid) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
|
||||||
Retrieve a LPF2 Hub device by UUID.
|
Retrieve a LPF2 Hub by UUID.
|
||||||
|
|
||||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||||
|
|
||||||
@ -97,10 +97,10 @@ Retrieve a LPF2 Hub device by UUID.
|
|||||||
| --- | --- |
|
| --- | --- |
|
||||||
| uuid | <code>string</code> |
|
| uuid | <code>string</code> |
|
||||||
|
|
||||||
<a name="LPF2+getConnectedDevices"></a>
|
<a name="LPF2+getConnectedHubs"></a>
|
||||||
|
|
||||||
### lpF2.getConnectedDevices() ⇒ [<code>Array.<Hub></code>](#Hub)
|
### lpF2.getConnectedHubs() ⇒ [<code>Array.<Hub></code>](#Hub)
|
||||||
Retrieve a list of LPF2 Hub devices.
|
Retrieve a list of LPF2 Hubs.
|
||||||
|
|
||||||
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
|
||||||
<a name="LPF2+event_discover"></a>
|
<a name="LPF2+event_discover"></a>
|
||||||
|
@ -8,14 +8,18 @@ console.log("Looking for Hubs...");
|
|||||||
lpf2.on("discover", async (hub) => { // Wait to discover hubs
|
lpf2.on("discover", async (hub) => { // Wait to discover hubs
|
||||||
|
|
||||||
await hub.connect(); // Connect to hub
|
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;
|
let color = 0;
|
||||||
setInterval(() => {
|
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) => {
|
hubs.forEach((hub) => {
|
||||||
hub.setLEDColor(color); // Set the color
|
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 useSpeedMap: boolean = true;
|
||||||
public type: Consts.Hubs = Consts.Hubs.UNKNOWN;
|
public type: Consts.Hubs = Consts.Hubs.UNKNOWN;
|
||||||
public uuid: string;
|
public uuid: string;
|
||||||
|
public name: string;
|
||||||
|
|
||||||
protected _ports: {[port: string]: Port} = {};
|
protected _ports: {[port: string]: Port} = {};
|
||||||
protected _characteristics: {[uuid: string]: Characteristic} = {};
|
protected _characteristics: {[uuid: string]: Characteristic} = {};
|
||||||
@ -33,6 +34,7 @@ export class Hub extends EventEmitter {
|
|||||||
this.autoSubscribe = !!autoSubscribe;
|
this.autoSubscribe = !!autoSubscribe;
|
||||||
this._peripheral = peripheral;
|
this._peripheral = peripheral;
|
||||||
this.uuid = peripheral.uuid;
|
this.uuid = peripheral.uuid;
|
||||||
|
this.name = peripheral.advertisement.localName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -63,8 +65,8 @@ export class Hub extends EventEmitter {
|
|||||||
}, 2000);
|
}, 2000);
|
||||||
|
|
||||||
self._peripheral.on("disconnect", () => {
|
self._peripheral.on("disconnect", () => {
|
||||||
clearInterval(rssiUpdateInterval);
|
clearInterval(rssiUpdateInterval);
|
||||||
this.emit("disconnect");
|
this.emit("disconnect");
|
||||||
});
|
});
|
||||||
|
|
||||||
self._peripheral.discoverServices([], (err: string, services: Service[]) => {
|
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;
|
public autoSubscribe: boolean = true;
|
||||||
|
|
||||||
|
|
||||||
private _connectedDevices: {[uuid: string]: Hub} = {};
|
private _connectedHubs: {[uuid: string]: Hub} = {};
|
||||||
|
|
||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
@ -70,18 +70,16 @@ export class LPF2 extends EventEmitter {
|
|||||||
|
|
||||||
hub.on("connect", () => {
|
hub.on("connect", () => {
|
||||||
debug(`Hub ${hub.uuid} connected`);
|
debug(`Hub ${hub.uuid} connected`);
|
||||||
this._connectedDevices[hub.uuid] = hub;
|
this._connectedHubs[hub.uuid] = hub;
|
||||||
});
|
});
|
||||||
|
|
||||||
hub.on("disconnect", () => {
|
hub.on("disconnect", () => {
|
||||||
debug(`Hub ${hub.uuid} disconnected`);
|
debug(`Hub ${hub.uuid} disconnected`);
|
||||||
delete this._connectedDevices[hub.uuid];
|
delete this._connectedHubs[hub.uuid];
|
||||||
|
|
||||||
if (wantScan) {
|
if (wantScan) {
|
||||||
noble.startScanning();
|
noble.startScanning();
|
||||||
}
|
}
|
||||||
|
|
||||||
hub.emit("disconnect");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
debug(`Hub ${hub.uuid} discovered`);
|
debug(`Hub ${hub.uuid} discovered`);
|
||||||
@ -112,24 +110,24 @@ export class LPF2 extends EventEmitter {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a LPF2 Hub device by UUID.
|
* Retrieve a LPF2 Hub by UUID.
|
||||||
* @method LPF2#getConnectedDeviceByUUID
|
* @method LPF2#getConnectedHubByUUID
|
||||||
* @param {string} uuid
|
* @param {string} uuid
|
||||||
* @returns {Hub | null}
|
* @returns {Hub | null}
|
||||||
*/
|
*/
|
||||||
public getConnectedDeviceByUUID (uuid: string) {
|
public getConnectedHubByUUID (uuid: string) {
|
||||||
return this._connectedDevices[uuid];
|
return this._connectedHubs[uuid];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a list of LPF2 Hub devices.
|
* Retrieve a list of LPF2 Hubs.
|
||||||
* @method LPF2#getConnectedDevices
|
* @method LPF2#getConnectedHubs
|
||||||
* @returns {Hub[]}
|
* @returns {Hub[]}
|
||||||
*/
|
*/
|
||||||
public getConnectedDevices () {
|
public getConnectedHubs () {
|
||||||
return Object.keys(this._connectedDevices).map((uuid) => {
|
return Object.keys(this._connectedHubs).map((uuid) => {
|
||||||
return this._connectedDevices[uuid];
|
return this._connectedHubs[uuid];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user