Added attach/detach events

This commit is contained in:
Nathan Kunicki 2018-06-28 16:55:53 +01:00
parent 12e3fdb8b7
commit ed515d6ec0
4 changed files with 165 additions and 0 deletions

75
DOCS.md
View File

@ -86,6 +86,8 @@ Emits when a LPF2 Hub device is found.
* ["color" (port, color)](#LPF2Hub+event_color)
* ["tilt" (port, x, y)](#LPF2Hub+event_tilt)
* ["rotate" (port, rotation)](#LPF2Hub+event_rotate)
* ["attach" (port, type)](#Hub+event_attach)
* ["detach" (port)](#Hub+event_detach)
<a name="new_LPF2Hub_new"></a>
@ -261,6 +263,29 @@ Emits when a rotation sensor is activated.
| port | <code>string</code> |
| rotation | <code>number</code> |
<a name="Hub+event_attach"></a>
### "attach" (port, type)
Emits when a motor or sensor is attached to the Hub.
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| type | <code>number</code> | A number representing one of the peripheral consts. |
<a name="Hub+event_detach"></a>
### "detach" (port)
Emits when an attached motor or sensor is detached from the Hub.
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
<a name="WeDo2Hub"></a>
## WeDo2Hub ⇐ [<code>Hub</code>](#Hub)
@ -283,6 +308,8 @@ Emits when a rotation sensor is activated.
* ["color" (port, color)](#WeDo2Hub+event_color)
* ["tilt" (port, x, y)](#WeDo2Hub+event_tilt)
* ["rotate" (port, rotation)](#WeDo2Hub+event_rotate)
* ["attach" (port, type)](#Hub+event_attach)
* ["detach" (port)](#Hub+event_detach)
<a name="new_WeDo2Hub_new"></a>
@ -457,6 +484,29 @@ Emits when a rotation sensor is activated.
| port | <code>string</code> |
| rotation | <code>number</code> |
<a name="Hub+event_attach"></a>
### "attach" (port, type)
Emits when a motor or sensor is attached to the Hub.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| type | <code>number</code> | A number representing one of the peripheral consts. |
<a name="Hub+event_detach"></a>
### "detach" (port)
Emits when an attached motor or sensor is detached from the Hub.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
<a name="Hub"></a>
## Hub ⇐ <code>EventEmitter</code>
@ -470,6 +520,8 @@ Emits when a rotation sensor is activated.
* [.unsubscribe(port)](#Hub+unsubscribe) ⇒ <code>Promise</code>
* [.sleep(delay)](#Hub+sleep) ⇒ <code>Promise</code>
* [.wait(commands)](#Hub+wait) ⇒ <code>Promise</code>
* ["attach" (port, type)](#Hub+event_attach)
* ["detach" (port)](#Hub+event_detach)
<a name="Hub+connect"></a>
@ -538,3 +590,26 @@ This is a helper method to make it easier to wait for concurrent commands to com
| --- | --- | --- |
| commands | <code>Array.&lt;Promise.&lt;any&gt;&gt;</code> | Array of executing commands. |
<a name="Hub+event_attach"></a>
### "attach" (port, type)
Emits when a motor or sensor is attached to the Hub.
**Kind**: event emitted by [<code>Hub</code>](#Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| type | <code>number</code> | A number representing one of the peripheral consts. |
<a name="Hub+event_detach"></a>
### "detach" (port)
Emits when an attached motor or sensor is detached from the Hub.
**Kind**: event emitted by [<code>Hub</code>](#Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |

View File

@ -127,6 +127,8 @@ Emits when a LPF2 Hub device is found.
* ["color" (port, color)](#LPF2Hub+event_color)
* ["tilt" (port, x, y)](#LPF2Hub+event_tilt)
* ["rotate" (port, rotation)](#LPF2Hub+event_rotate)
* ["attach" (port, type)](#Hub+event_attach)
* ["detach" (port)](#Hub+event_detach)
<a name="new_LPF2Hub_new"></a>
@ -302,6 +304,29 @@ Emits when a rotation sensor is activated.
| port | <code>string</code> |
| rotation | <code>number</code> |
<a name="Hub+event_attach"></a>
### "attach" (port, type)
Emits when a motor or sensor is attached to the Hub.
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| type | <code>number</code> | A number representing one of the peripheral consts. |
<a name="Hub+event_detach"></a>
### "detach" (port)
Emits when an attached motor or sensor is detached from the Hub.
**Kind**: event emitted by [<code>LPF2Hub</code>](#LPF2Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
<a name="WeDo2Hub"></a>
## WeDo2Hub ⇐ [<code>Hub</code>](#Hub)
@ -324,6 +349,8 @@ Emits when a rotation sensor is activated.
* ["color" (port, color)](#WeDo2Hub+event_color)
* ["tilt" (port, x, y)](#WeDo2Hub+event_tilt)
* ["rotate" (port, rotation)](#WeDo2Hub+event_rotate)
* ["attach" (port, type)](#Hub+event_attach)
* ["detach" (port)](#Hub+event_detach)
<a name="new_WeDo2Hub_new"></a>
@ -498,6 +525,29 @@ Emits when a rotation sensor is activated.
| port | <code>string</code> |
| rotation | <code>number</code> |
<a name="Hub+event_attach"></a>
### "attach" (port, type)
Emits when a motor or sensor is attached to the Hub.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| type | <code>number</code> | A number representing one of the peripheral consts. |
<a name="Hub+event_detach"></a>
### "detach" (port)
Emits when an attached motor or sensor is detached from the Hub.
**Kind**: event emitted by [<code>WeDo2Hub</code>](#WeDo2Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |
<a name="Hub"></a>
## Hub ⇐ <code>EventEmitter</code>
@ -511,6 +561,8 @@ Emits when a rotation sensor is activated.
* [.unsubscribe(port)](#Hub+unsubscribe) ⇒ <code>Promise</code>
* [.sleep(delay)](#Hub+sleep) ⇒ <code>Promise</code>
* [.wait(commands)](#Hub+wait) ⇒ <code>Promise</code>
* ["attach" (port, type)](#Hub+event_attach)
* ["detach" (port)](#Hub+event_detach)
<a name="Hub+connect"></a>
@ -579,3 +631,26 @@ This is a helper method to make it easier to wait for concurrent commands to com
| --- | --- | --- |
| commands | <code>Array.&lt;Promise.&lt;any&gt;&gt;</code> | Array of executing commands. |
<a name="Hub+event_attach"></a>
### "attach" (port, type)
Emits when a motor or sensor is attached to the Hub.
**Kind**: event emitted by [<code>Hub</code>](#Hub)
| Param | Type | Description |
| --- | --- | --- |
| port | <code>string</code> | |
| type | <code>number</code> | A number representing one of the peripheral consts. |
<a name="Hub+event_detach"></a>
### "detach" (port)
Emits when an attached motor or sensor is detached from the Hub.
**Kind**: event emitted by [<code>Hub</code>](#Hub)
| Param | Type |
| --- | --- |
| port | <code>string</code> |

13
hub.ts
View File

@ -217,10 +217,23 @@ export class Hub extends EventEmitter {
port.type = type;
if (this.autoSubscribe) {
this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00);
/**
* Emits when a motor or sensor is attached to the Hub.
* @event Hub#attach
* @param {string} port
* @param {number} type A number representing one of the peripheral consts.
*/
this.emit("attach", port.id, type);
}
} else {
port.type = Consts.Devices.UNKNOWN;
debug(`Port ${port.id} disconnected`);
/**
* Emits when an attached motor or sensor is detached from the Hub.
* @event Hub#detach
* @param {string} port
*/
this.emit("detach", port.id);
}
}

View File

@ -64,6 +64,8 @@ export class LPF2 extends EventEmitter {
return;
}
console.log(peripheral);
peripheral.removeAllListeners();
noble.stopScanning();
noble.startScanning();