Fixed ts errors and types

This commit is contained in:
Nathan Kunicki 2018-06-20 14:58:42 +01:00
parent 708c1f74b5
commit a2e254fa18
5 changed files with 25 additions and 21 deletions

View File

@ -40,12 +40,12 @@ export class BoostHub extends Hub {
}
public connect (callback: () => void) {
public connect (callback?: () => void) {
debug("Connecting to Boost Move Hub");
super.connect(() => {
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this));
characteristic.write(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]));
characteristic.write(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]), false);
debug("Connect completed");
if (callback) {
callback();
@ -63,12 +63,12 @@ export class BoostHub extends Hub {
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
if (characteristic) {
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
characteristic.write(data);
characteristic.write(data, false);
if (color === false) {
color = 0;
}
data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]);
characteristic.write(data);
characteristic.write(data, false);
}
}
@ -97,10 +97,10 @@ export class BoostHub extends Hub {
if (time) {
const data = Buffer.from([0x0c, 0x00, 0x81, this._ports[port].value, 0x11, 0x09, 0x00, 0x00, speed, 0x64, 0x7f, 0x03]);
data.writeUInt16LE(time > 65535 ? 65535 : time, 6);
characteristic.write(data);
characteristic.write(data, false);
} else {
const data = Buffer.from([0x0a, 0x00, 0x81, this._ports[port].value, 0x11, 0x01, speed, 0x64, 0x7f, 0x03]);
characteristic.write(data);
characteristic.write(data, false);
}
}
}
@ -119,7 +119,7 @@ export class BoostHub extends Hub {
const data = Buffer.from([0x0e, 0x00, 0x81, this._ports[port].value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]);
data.writeUInt32LE(angle, 6);
data.writeInt8(speed, 10);
characteristic.write(data);
characteristic.write(data, false);
}
}
@ -127,7 +127,7 @@ export class BoostHub extends Hub {
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
if (characteristic) {
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback);
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), false, callback);
}
}
@ -135,7 +135,7 @@ export class BoostHub extends Hub {
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
if (characteristic) {
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback);
characteristic.write(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), false, callback);
}
}

View File

@ -7,6 +7,7 @@ export enum Hubs {
}
export enum Devices {
UNKNOWN = 0,
BASIC_MOTOR = 1,
BOOST_LED = 22,
WEDO2_TILT = 34,

4
hub.ts
View File

@ -40,7 +40,7 @@ export class Hub extends EventEmitter {
* @method Hub#connect
* @param {function} [callback]
*/
public connect (callback: () => void) {
public connect (callback?: () => void) {
const self = this;
@ -165,7 +165,7 @@ export class Hub extends EventEmitter {
this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00);
}
} else {
port.type = null;
port.type = Consts.Devices.UNKNOWN;
debug(`Port ${port.id} disconnected`);
}

View File

@ -1,17 +1,20 @@
import * as Consts from "./consts";
export class Port {
public id: string;
public value: number;
public connected: boolean;
public type: number | null;
public type: Consts.Devices;
constructor (id: string, value: number) {
this.id = id;
this.value = value;
this.connected = false;
this.type = null;
this.type = Consts.Devices.UNKNOWN;
}
}

View File

@ -36,7 +36,7 @@ export class WeDo2Hub extends Hub {
}
public connect (callback: () => void) {
public connect (callback?: () => void) {
debug("Connecting to WeDo 2.0 Smart Hub");
super.connect(() => {
this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE], this._parsePortMessage.bind(this));
@ -60,12 +60,12 @@ export class WeDo2Hub extends Hub {
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
if (motorCharacteristic && portCharacteristic) {
let data = Buffer.from([0x06, 0x17, 0x01, 0x01]);
portCharacteristic.write(data);
portCharacteristic.write(data, false);
if (color === false) {
color = 0;
}
data = Buffer.from([0x06, 0x04, 0x01, color]);
motorCharacteristic.write(data);
motorCharacteristic.write(data, false);
}
}
@ -82,9 +82,9 @@ export class WeDo2Hub extends Hub {
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
if (motorCharacteristic && portCharacteristic) {
const data1 = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]);
portCharacteristic.write(data1);
portCharacteristic.write(data1, false);
const data2 = Buffer.from([0x06, 0x04, 0x03, red, green, blue]);
motorCharacteristic.write(data2);
motorCharacteristic.write(data2, false);
}
}
@ -98,7 +98,7 @@ export class WeDo2Hub extends Hub {
public setMotorSpeed (port: string, speed: number) {
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
if (characteristic) {
characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]));
characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, speed]), false);
}
}
@ -106,7 +106,7 @@ export class WeDo2Hub extends Hub {
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
if (characteristic) {
characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback);
characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), false, callback);
}
}
@ -114,7 +114,7 @@ export class WeDo2Hub extends Hub {
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
if (characteristic) {
characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback);
characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), false, callback);
}
}