All command methods now return promises

This commit is contained in:
Nathan Kunicki 2018-06-22 12:20:08 +01:00
parent 92248caca5
commit 47fa748273
6 changed files with 36 additions and 28 deletions

14
DOCS.md
View File

@ -39,7 +39,7 @@ Stop scanning for LPF2 Hub devices.
<a name="LPF2+getConnectedDeviceByUUID"></a>
### lpF2.getConnectedDeviceByUUID(uuid) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
Retrieve a LPF2 Hub device by UUID
Retrieve a LPF2 Hub device by UUID.
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
@ -50,7 +50,7 @@ Retrieve a LPF2 Hub device by UUID
<a name="LPF2+getConnectedDevices"></a>
### lpF2.getConnectedDevices() ⇒ [<code>Array.&lt;Hub&gt;</code>](#Hub)
Retrieve a list of LPF2 Hub devices
Retrieve a list of LPF2 Hub devices.
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
<a name="LPF2+event_discover"></a>
@ -62,7 +62,7 @@ Emits when a LPF2 Hub device is found.
| Param | Type |
| --- | --- |
| hub | [<code>Hub</code>](#Hub) |
| hub | [<code>WeDo2Hub</code>](#WeDo2Hub) \| [<code>BoostHub</code>](#BoostHub) |
<a name="BoostHub"></a>
@ -90,7 +90,7 @@ Emits when a LPF2 Hub device is found.
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
@ -248,7 +248,7 @@ Emits when a rotation sensor is activated.
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
@ -260,7 +260,7 @@ Set the color of the LED on the Hub via a color value.
Set the color of the LED on the Hub via RGB values.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type |
| --- | --- |
@ -274,7 +274,7 @@ Set the color of the LED on the Hub via RGB values.
Set the motor speed on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |

View File

@ -79,7 +79,7 @@ Stop scanning for LPF2 Hub devices.
<a name="LPF2+getConnectedDeviceByUUID"></a>
### lpF2.getConnectedDeviceByUUID(uuid) ⇒ [<code>Hub</code>](#Hub) \| <code>null</code>
Retrieve a LPF2 Hub device by UUID
Retrieve a LPF2 Hub device by UUID.
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
@ -90,7 +90,7 @@ Retrieve a LPF2 Hub device by UUID
<a name="LPF2+getConnectedDevices"></a>
### lpF2.getConnectedDevices() ⇒ [<code>Array.&lt;Hub&gt;</code>](#Hub)
Retrieve a list of LPF2 Hub devices
Retrieve a list of LPF2 Hub devices.
**Kind**: instance method of [<code>LPF2</code>](#LPF2)
<a name="LPF2+event_discover"></a>
@ -102,7 +102,7 @@ Emits when a LPF2 Hub device is found.
| Param | Type |
| --- | --- |
| hub | [<code>Hub</code>](#Hub) |
| hub | [<code>WeDo2Hub</code>](#WeDo2Hub) \| [<code>BoostHub</code>](#BoostHub) |
<a name="BoostHub"></a>
@ -130,7 +130,7 @@ Emits when a LPF2 Hub device is found.
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [<code>BoostHub</code>](#BoostHub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
@ -288,7 +288,7 @@ Emits when a rotation sensor is activated.
Set the color of the LED on the Hub via a color value.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |
@ -300,7 +300,7 @@ Set the color of the LED on the Hub via a color value.
Set the color of the LED on the Hub via RGB values.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type |
| --- | --- |
@ -314,7 +314,7 @@ Set the color of the LED on the Hub via RGB values.
Set the motor speed on a given port.
**Kind**: instance method of [<code>WeDo2Hub</code>](#WeDo2Hub)
**Returns**: <code>Promise</code> - Resolved upon successful completion of command.
**Returns**: <code>Promise</code> - Resolved upon successful issuance of command.
| Param | Type | Description |
| --- | --- | --- |

View File

@ -57,7 +57,7 @@ export class BoostHub extends Hub {
* Set the color of the LED on the Hub via a color value.
* @method BoostHub#setLEDColor
* @param {number} color A number representing one of the LED color consts.
* @returns {Promise} Resolved upon successful completion of command.
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setLEDColor (color: number | boolean) {
return new Promise((resolve, reject) => {

12
hub.ts
View File

@ -123,11 +123,15 @@ export class Hub extends EventEmitter {
* @param {number} [mode] The sensor mode to activate. If no mode is provided, the default for that sensor will be chosen.
*/
public subscribe (port: string, mode?: number) {
return new Promise((resolve, reject) => {
let newMode = 0x00;
if (mode && !(typeof mode === "number")) {
newMode = this._getModeForDeviceType(this._ports[port].type);
}
this._activatePortDevice(this._ports[port].value, this._ports[port].type, newMode, 0x00);
this._activatePortDevice(this._ports[port].value, this._ports[port].type, newMode, 0x00, () => {
return resolve();
});
});
}
/**
@ -136,8 +140,12 @@ export class Hub extends EventEmitter {
* @param {string} port
*/
public unsubscribe (port: string) {
return new Promise((resolve, reject) => {
const mode = this._getModeForDeviceType(this._ports[port].type);
this._deactivatePortDevice(this._ports[port].value, this._ports[port].type, mode, 0x00);
this._deactivatePortDevice(this._ports[port].value, this._ports[port].type, mode, 0x00, () => {
return resolve();
});
});
}

View File

@ -86,7 +86,7 @@ export class LPF2 extends EventEmitter {
/**
* Emits when a LPF2 Hub device is found.
* @event LPF2#discover
* @param {Hub} hub
* @param {WeDo2Hub | BoostHub} hub
*/
this.emit("discover", hub);
@ -110,7 +110,7 @@ export class LPF2 extends EventEmitter {
/**
* Retrieve a LPF2 Hub device by UUID
* Retrieve a LPF2 Hub device by UUID.
* @method LPF2#getConnectedDeviceByUUID
* @param {string} uuid
* @returns {Hub | null}
@ -121,7 +121,7 @@ export class LPF2 extends EventEmitter {
/**
* Retrieve a list of LPF2 Hub devices
* Retrieve a list of LPF2 Hub devices.
* @method LPF2#getConnectedDevices
* @returns {Hub[]}
*/

View File

@ -53,7 +53,7 @@ export class WeDo2Hub extends Hub {
* Set the color of the LED on the Hub via a color value.
* @method WeDo2Hub#setLEDColor
* @param {number} color A number representing one of the LED color consts.
* @returns {Promise} Resolved upon successful completion of command.
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setLEDColor (color: number | boolean) {
return new Promise((resolve, reject) => {
@ -79,7 +79,7 @@ export class WeDo2Hub extends Hub {
* @param {number} red
* @param {number} green
* @param {number} blue
* @returns {Promise} Resolved upon successful completion of command.
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setLEDRGB (red: number, green: number, blue: number) {
return new Promise((resolve, reject) => {
@ -101,7 +101,7 @@ export class WeDo2Hub extends Hub {
* @method WeDo2Hub#setMotorSpeed
* @param {string} port
* @param {number} speed For forward, a value between 1 - 100 should be set. For reverse, a value between -1 to -100. Stop is 0.
* @returns {Promise} Resolved upon successful completion of command.
* @returns {Promise} Resolved upon successful issuance of command.
*/
public setMotorSpeed (port: string, speed: number) {
return new Promise((resolve, reject) => {