Better error handling
This commit is contained in:
parent
0d0d1a8d32
commit
7b8d7bd8d2
@ -76,7 +76,7 @@ export class BoostMoveHub extends LPF2Hub {
|
||||
*/
|
||||
public setMotorSpeed (port: string, speed: number, time?: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const portObj = this._ports[port];
|
||||
const portObj = this._portLookup(port);
|
||||
if (time) {
|
||||
if (portObj.type === Consts.Devices.BOOST_INTERACTIVE_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) {
|
||||
portObj.busy = true;
|
||||
@ -113,7 +113,7 @@ export class BoostMoveHub extends LPF2Hub {
|
||||
* @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
|
||||
*/
|
||||
public setMotorAngle (port: string, angle: number, speed: number = 100) {
|
||||
const portObj = this._ports[port];
|
||||
const portObj = this._portLookup(port);
|
||||
if (!(portObj.type === Consts.Devices.BOOST_INTERACTIVE_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR)) {
|
||||
throw new Error("Angle rotation is only available when using a Boost Interactive Motor or Boost Move Hub Motor");
|
||||
}
|
||||
|
16
hub.ts
16
hub.ts
@ -169,11 +169,11 @@ export class Hub extends EventEmitter {
|
||||
*/
|
||||
public subscribe (port: string, mode?: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let newMode = this._getModeForDeviceType(this._ports[port].type);
|
||||
let newMode = this._getModeForDeviceType(this._portLookup(port).type);
|
||||
if (mode) {
|
||||
newMode = mode;
|
||||
}
|
||||
this._activatePortDevice(this._ports[port].value, this._ports[port].type, newMode, 0x00, () => {
|
||||
this._activatePortDevice(this._portLookup(port).value, this._portLookup(port).type, newMode, 0x00, () => {
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
@ -187,8 +187,8 @@ export class Hub extends EventEmitter {
|
||||
*/
|
||||
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, () => {
|
||||
const mode = this._getModeForDeviceType(this._portLookup(port).type);
|
||||
this._deactivatePortDevice(this._portLookup(port).value, this._portLookup(port).type, mode, 0x00, () => {
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
@ -310,6 +310,14 @@ export class Hub extends EventEmitter {
|
||||
}
|
||||
|
||||
|
||||
protected _portLookup (port: string) {
|
||||
if (!this._ports[port]) {
|
||||
throw new Error(`Port ${port} does not exist on this Hub type`);
|
||||
}
|
||||
return this._ports[port];
|
||||
}
|
||||
|
||||
|
||||
private _getModeForDeviceType (type: Consts.Devices) {
|
||||
switch (type) {
|
||||
case Consts.Devices.BASIC_MOTOR:
|
||||
|
@ -80,7 +80,7 @@ export class PUPHub extends LPF2Hub {
|
||||
*/
|
||||
public setMotorSpeed (port: string, speed: number, time?: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const portObj = this._ports[port];
|
||||
const portObj = this._portLookup(port);
|
||||
if (time) {
|
||||
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
|
||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||
|
@ -102,10 +102,10 @@ export class WeDo2Hub extends Hub {
|
||||
*/
|
||||
public setMotorSpeed (port: string, speed: number, time?: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)]));
|
||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._portLookup(port).value, 0x01, 0x02, this._mapSpeed(speed)]));
|
||||
if (time) {
|
||||
setTimeout(() => {
|
||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, 0x00]));
|
||||
this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._portLookup(port).value, 0x01, 0x02, 0x00]));
|
||||
return resolve();
|
||||
}, time);
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user