Fixed ts errors and types
This commit is contained in:
parent
708c1f74b5
commit
a2e254fa18
18
boosthub.ts
18
boosthub.ts
@ -40,12 +40,12 @@ export class BoostHub extends Hub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public connect (callback: () => void) {
|
public connect (callback?: () => void) {
|
||||||
debug("Connecting to Boost Move Hub");
|
debug("Connecting to Boost Move Hub");
|
||||||
super.connect(() => {
|
super.connect(() => {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
||||||
this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this));
|
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");
|
debug("Connect completed");
|
||||||
if (callback) {
|
if (callback) {
|
||||||
callback();
|
callback();
|
||||||
@ -63,12 +63,12 @@ export class BoostHub extends Hub {
|
|||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
||||||
if (characteristic) {
|
if (characteristic) {
|
||||||
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
|
let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]);
|
||||||
characteristic.write(data);
|
characteristic.write(data, false);
|
||||||
if (color === false) {
|
if (color === false) {
|
||||||
color = 0;
|
color = 0;
|
||||||
}
|
}
|
||||||
data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]);
|
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) {
|
if (time) {
|
||||||
const data = Buffer.from([0x0c, 0x00, 0x81, this._ports[port].value, 0x11, 0x09, 0x00, 0x00, speed, 0x64, 0x7f, 0x03]);
|
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);
|
data.writeUInt16LE(time > 65535 ? 65535 : time, 6);
|
||||||
characteristic.write(data);
|
characteristic.write(data, false);
|
||||||
} else {
|
} else {
|
||||||
const data = Buffer.from([0x0a, 0x00, 0x81, this._ports[port].value, 0x11, 0x01, speed, 0x64, 0x7f, 0x03]);
|
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]);
|
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.writeUInt32LE(angle, 6);
|
||||||
data.writeInt8(speed, 10);
|
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) {
|
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
||||||
if (characteristic) {
|
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) {
|
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL];
|
||||||
if (characteristic) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ export enum Hubs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export enum Devices {
|
export enum Devices {
|
||||||
|
UNKNOWN = 0,
|
||||||
BASIC_MOTOR = 1,
|
BASIC_MOTOR = 1,
|
||||||
BOOST_LED = 22,
|
BOOST_LED = 22,
|
||||||
WEDO2_TILT = 34,
|
WEDO2_TILT = 34,
|
||||||
|
4
hub.ts
4
hub.ts
@ -40,7 +40,7 @@ export class Hub extends EventEmitter {
|
|||||||
* @method Hub#connect
|
* @method Hub#connect
|
||||||
* @param {function} [callback]
|
* @param {function} [callback]
|
||||||
*/
|
*/
|
||||||
public connect (callback: () => void) {
|
public connect (callback?: () => void) {
|
||||||
|
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
@ -165,7 +165,7 @@ export class Hub extends EventEmitter {
|
|||||||
this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00);
|
this._activatePortDevice(port.value, type, this._getModeForDeviceType(type), 0x00);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
port.type = null;
|
port.type = Consts.Devices.UNKNOWN;
|
||||||
debug(`Port ${port.id} disconnected`);
|
debug(`Port ${port.id} disconnected`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
7
port.ts
7
port.ts
@ -1,17 +1,20 @@
|
|||||||
|
import * as Consts from "./consts";
|
||||||
|
|
||||||
|
|
||||||
export class Port {
|
export class Port {
|
||||||
|
|
||||||
|
|
||||||
public id: string;
|
public id: string;
|
||||||
public value: number;
|
public value: number;
|
||||||
public connected: boolean;
|
public connected: boolean;
|
||||||
public type: number | null;
|
public type: Consts.Devices;
|
||||||
|
|
||||||
|
|
||||||
constructor (id: string, value: number) {
|
constructor (id: string, value: number) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
this.type = null;
|
this.type = Consts.Devices.UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
16
wedo2hub.ts
16
wedo2hub.ts
@ -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");
|
debug("Connecting to WeDo 2.0 Smart Hub");
|
||||||
super.connect(() => {
|
super.connect(() => {
|
||||||
this._subscribeToCharacteristic(this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE], this._parsePortMessage.bind(this));
|
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];
|
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
|
||||||
if (motorCharacteristic && portCharacteristic) {
|
if (motorCharacteristic && portCharacteristic) {
|
||||||
let data = Buffer.from([0x06, 0x17, 0x01, 0x01]);
|
let data = Buffer.from([0x06, 0x17, 0x01, 0x01]);
|
||||||
portCharacteristic.write(data);
|
portCharacteristic.write(data, false);
|
||||||
if (color === false) {
|
if (color === false) {
|
||||||
color = 0;
|
color = 0;
|
||||||
}
|
}
|
||||||
data = Buffer.from([0x06, 0x04, 0x01, color]);
|
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];
|
const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
|
||||||
if (motorCharacteristic && portCharacteristic) {
|
if (motorCharacteristic && portCharacteristic) {
|
||||||
const data1 = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]);
|
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]);
|
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) {
|
public setMotorSpeed (port: string, speed: number) {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE];
|
||||||
if (characteristic) {
|
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) {
|
protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
|
||||||
if (characteristic) {
|
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) {
|
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) {
|
||||||
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
|
const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE];
|
||||||
if (characteristic) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user