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) {
|
public setMotorSpeed (port: string, speed: number, time?: number) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const portObj = this._ports[port];
|
const portObj = this._portLookup(port);
|
||||||
if (time) {
|
if (time) {
|
||||||
if (portObj.type === Consts.Devices.BOOST_INTERACTIVE_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) {
|
if (portObj.type === Consts.Devices.BOOST_INTERACTIVE_MOTOR || portObj.type === Consts.Devices.BOOST_MOVE_HUB_MOTOR) {
|
||||||
portObj.busy = true;
|
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).
|
* @returns {Promise} Resolved upon successful completion of command (ie. once the motor is finished).
|
||||||
*/
|
*/
|
||||||
public setMotorAngle (port: string, angle: number, speed: number = 100) {
|
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)) {
|
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");
|
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) {
|
public subscribe (port: string, mode?: number) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let newMode = this._getModeForDeviceType(this._ports[port].type);
|
let newMode = this._getModeForDeviceType(this._portLookup(port).type);
|
||||||
if (mode) {
|
if (mode) {
|
||||||
newMode = 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();
|
return resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -187,8 +187,8 @@ export class Hub extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
public unsubscribe (port: string) {
|
public unsubscribe (port: string) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const mode = this._getModeForDeviceType(this._ports[port].type);
|
const mode = this._getModeForDeviceType(this._portLookup(port).type);
|
||||||
this._deactivatePortDevice(this._ports[port].value, this._ports[port].type, mode, 0x00, () => {
|
this._deactivatePortDevice(this._portLookup(port).value, this._portLookup(port).type, mode, 0x00, () => {
|
||||||
return resolve();
|
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) {
|
private _getModeForDeviceType (type: Consts.Devices) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Consts.Devices.BASIC_MOTOR:
|
case Consts.Devices.BASIC_MOTOR:
|
||||||
|
@ -80,7 +80,7 @@ export class PUPHub extends LPF2Hub {
|
|||||||
*/
|
*/
|
||||||
public setMotorSpeed (port: string, speed: number, time?: number) {
|
public setMotorSpeed (port: string, speed: number, time?: number) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const portObj = this._ports[port];
|
const portObj = this._portLookup(port);
|
||||||
if (time) {
|
if (time) {
|
||||||
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
|
const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]);
|
||||||
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
|
||||||
|
@ -102,10 +102,10 @@ export class WeDo2Hub extends Hub {
|
|||||||
*/
|
*/
|
||||||
public setMotorSpeed (port: string, speed: number, time?: number) {
|
public setMotorSpeed (port: string, speed: number, time?: number) {
|
||||||
return new Promise((resolve, reject) => {
|
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) {
|
if (time) {
|
||||||
setTimeout(() => {
|
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();
|
return resolve();
|
||||||
}, time);
|
}, time);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user