diff --git a/lpf2hub.ts b/lpf2hub.ts index 4bb9cec..cdb31a4 100644 --- a/lpf2hub.ts +++ b/lpf2hub.ts @@ -73,7 +73,7 @@ export class LPF2Hub extends Hub { await super.connect(); const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); - this._writeMessage(Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); debug("Connect completed"); return resolve(); }); @@ -89,12 +89,12 @@ export class LPF2Hub extends Hub { public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { let data = Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02]); - this._writeMessage(data); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data); if (color === false) { color = 0; } data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]); - this._writeMessage(data); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data); return resolve(); }); } @@ -115,13 +115,13 @@ export class LPF2Hub extends Hub { portObj.busy = true; const data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); data.writeUInt16LE(time > 65535 ? 65535 : time, 6); - this._writeMessage(data); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data); portObj.finished = () => { return resolve(); }; } else { const data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); - this._writeMessage(data); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data); return resolve(); } }); @@ -143,7 +143,7 @@ export class LPF2Hub extends Hub { const data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7f, 0x03]); data.writeUInt32LE(angle, 6); data.writeUInt8(this._mapSpeed(speed), 10); - this._writeMessage(data); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, data); portObj.finished = () => { return resolve(); }; @@ -152,17 +152,17 @@ export class LPF2Hub extends Hub { protected _activatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { - this._writeMessage(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); } protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { - this._writeMessage(Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); + this._writeMessage(Consts.BLECharacteristics.BOOST_ALL, Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); } - private _writeMessage (message: Buffer, callback?: () => void) { - const characteristic = this._characteristics[Consts.BLECharacteristics.BOOST_ALL]; + private _writeMessage (uuid: string, message: Buffer, callback?: () => void) { + const characteristic = this._characteristics[uuid]; if (characteristic) { characteristic.write(message, false, callback); } diff --git a/wedo2hub.ts b/wedo2hub.ts index f74751e..188a998 100644 --- a/wedo2hub.ts +++ b/wedo2hub.ts @@ -58,18 +58,14 @@ export class WeDo2Hub extends Hub { */ public setLEDColor (color: number | boolean) { return new Promise((resolve, reject) => { - const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; - const portCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; - if (motorCharacteristic && portCharacteristic) { - let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); - portCharacteristic.write(data, false); - if (color === false) { - color = 0; - } - data = Buffer.from([0x06, 0x04, 0x01, color]); - motorCharacteristic.write(data, false); - return resolve(); + let data = Buffer.from([0x06, 0x17, 0x01, 0x01]); + this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + if (color === false) { + color = 0; } + data = Buffer.from([0x06, 0x04, 0x01, color]); + this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data); + return resolve(); }); } @@ -84,15 +80,11 @@ export class WeDo2Hub extends Hub { */ public setLEDRGB (red: number, green: number, blue: number) { return new Promise((resolve, reject) => { - const motorCharacteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; - 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, false); - const data2 = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); - motorCharacteristic.write(data2, false); - return resolve(); - } + let data = Buffer.from([0x01, 0x02, 0x06, 0x17, 0x01, 0x02]); + this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, data); + data = Buffer.from([0x06, 0x04, 0x03, red, green, blue]); + this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, data); + return resolve(); }); } @@ -106,27 +98,26 @@ export class WeDo2Hub extends Hub { */ public setMotorSpeed (port: string, speed: number) { return new Promise((resolve, reject) => { - const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE]; - if (characteristic) { - characteristic.write(Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)]), false); - return resolve(); - } + this._writeMessage(Consts.BLECharacteristics.WEDO2_MOTOR_VALUE_WRITE, Buffer.from([this._ports[port].value, 0x01, 0x02, this._mapSpeed(speed)])); + return resolve(); }); } 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]), false, callback); - } + this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x01]), callback); } protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback: () => void) { - const characteristic = this._characteristics[Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE]; + this._writeMessage(Consts.BLECharacteristics.WEDO2_PORT_TYPE_WRITE, Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), callback); + } + + + private _writeMessage (uuid: string, message: Buffer, callback?: () => void) { + const characteristic = this._characteristics[uuid]; if (characteristic) { - characteristic.write(Buffer.from([0x01, 0x02, port, type, mode, 0x01, 0x00, 0x00, 0x00, format, 0x00]), false, callback); + characteristic.write(message, false, callback); } }