Better handling of message lengths when sending packets

This commit is contained in:
Nathan Kunicki 2018-08-02 16:10:17 +01:00
parent a39352703b
commit 24037d537c
4 changed files with 33 additions and 32 deletions

View File

@ -59,7 +59,7 @@ export class BoostMoveHub extends LPF2Hub {
if (color === false) { if (color === false) {
color = 0; color = 0;
} }
const data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]); const data = Buffer.from([0x81, 0x32, 0x11, 0x51, 0x00, color]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve(); return resolve();
}); });
@ -87,25 +87,25 @@ export class BoostMoveHub extends LPF2Hub {
let data = null; let data = null;
if (portObj.id === "AB") { if (portObj.id === "AB") {
if (speed instanceof Array) { if (speed instanceof Array) {
data = Buffer.from([0x0d, 0x00, 0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
} else { } else {
data = Buffer.from([0x0d, 0x00, 0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]); data = Buffer.from([0x81, portObj.value, 0x11, 0x0a, 0x00, 0x00, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
} }
} else { } else {
// @ts-ignore: The type of speed is properly checked at the start // @ts-ignore: The type of speed is properly checked at the start
data = Buffer.from([0x0c, 0x00, 0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); data = Buffer.from([0x81, portObj.value, 0x11, 0x09, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
} }
data.writeUInt16LE(time > 65535 ? 65535 : time, 6); data.writeUInt16LE(time > 65535 ? 65535 : time, 4);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
portObj.finished = () => { portObj.finished = () => {
return resolve(); return resolve();
}; };
} else { } else {
// @ts-ignore: The type of speed is properly checked at the start // @ts-ignore: The type of speed is properly checked at the start
const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
setTimeout(() => { setTimeout(() => {
const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]); const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, 0x00]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve(); return resolve();
}, time); }, time);
@ -118,13 +118,13 @@ export class BoostMoveHub extends LPF2Hub {
let data = null; let data = null;
if (portObj.id === "AB") { if (portObj.id === "AB") {
if (speed instanceof Array) { if (speed instanceof Array) {
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
} else { } else {
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]); data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
} }
} else { } else {
// @ts-ignore: The type of speed is properly checked at the start // @ts-ignore: The type of speed is properly checked at the start
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); data = Buffer.from([0x81, portObj.value, 0x11, 0x01, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
} }
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
portObj.finished = () => { portObj.finished = () => {
@ -132,7 +132,7 @@ export class BoostMoveHub extends LPF2Hub {
}; };
} else { } else {
// @ts-ignore: The type of speed is properly checked at the start // @ts-ignore: The type of speed is properly checked at the start
const data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]); const data = Buffer.from([0x81, portObj.value, 0x11, 0x51, 0x00, this._mapSpeed(speed)]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
} }
@ -162,15 +162,15 @@ export class BoostMoveHub extends LPF2Hub {
let data = null; let data = null;
if (portObj.id === "AB") { if (portObj.id === "AB") {
if (speed instanceof Array) { if (speed instanceof Array) {
data = Buffer.from([0x0f, 0x00, 0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]); data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed[0]), this._mapSpeed(speed[1]), 0x64, 0x7f, 0x03]);
} else { } else {
data = Buffer.from([0x0f, 0x00, 0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]); data = Buffer.from([0x81, portObj.value, 0x11, 0x0c, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
} }
} else { } else {
// @ts-ignore: The type of speed is properly checked at the start // @ts-ignore: The type of speed is properly checked at the start
data = Buffer.from([0x0e, 0x00, 0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]); data = Buffer.from([0x81, portObj.value, 0x11, 0x0b, 0x00, 0x00, 0x00, 0x00, this._mapSpeed(speed), 0x64, 0x7f, 0x03]);
} }
data.writeUInt32LE(angle, 6); data.writeUInt32LE(angle, 4);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
portObj.finished = () => { portObj.finished = () => {
return resolve(); return resolve();

View File

@ -39,9 +39,9 @@ export class LPF2Hub extends Hub {
await super.connect(); await super.connect();
const characteristic = this._characteristics[Consts.BLECharacteristics.LPF2_ALL]; const characteristic = this._characteristics[Consts.BLECharacteristics.LPF2_ALL];
this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this)); this._subscribeToCharacteristic(characteristic, this._parseMessage.bind(this));
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x05, 0x00, 0x01, 0x02, 0x02])); // Activate button reports this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x0a, 0x00, 0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x0a, 0x00, 0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports
return resolve(); return resolve();
}); });
} }
@ -58,9 +58,8 @@ export class LPF2Hub extends Hub {
throw new Error("Name must be 14 characters or less"); throw new Error("Name must be 14 characters or less");
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let data = Buffer.from([0x00, 0x00, 0x01, 0x01, 0x01]); let data = Buffer.from([0x01, 0x01, 0x01]);
data = Buffer.concat([data, Buffer.from(name, "ascii")]); data = Buffer.concat([data, Buffer.from(name, "ascii")]);
data[0] = data.length;
// Send this twice, as sometimes the first time doesn't take // Send this twice, as sometimes the first time doesn't take
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
@ -71,18 +70,20 @@ export class LPF2Hub 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) {
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x01]), callback);
} }
protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) { protected _deactivatePortDevice (port: number, type: number, mode: number, format: number, callback?: () => void) {
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x0a, 0x00, 0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, Buffer.from([0x41, port, mode, 0x01, 0x00, 0x00, 0x00, 0x00]), callback);
} }
protected _writeMessage (uuid: string, message: Buffer, callback?: () => void) { protected _writeMessage (uuid: string, message: Buffer, callback?: () => void) {
const characteristic = this._characteristics[uuid]; const characteristic = this._characteristics[uuid];
if (characteristic) { if (characteristic) {
message = Buffer.concat([Buffer.alloc(2), message]);
message[0] = message.length;
characteristic.write(message, false, callback); characteristic.write(message, false, callback);
} }
} }

View File

@ -64,7 +64,7 @@ export class PUPHub extends LPF2Hub {
if (color === false) { if (color === false) {
color = 0; color = 0;
} }
const data = Buffer.from([0x08, 0x00, 0x81, 0x32, 0x11, 0x51, 0x00, color]); const data = Buffer.from([0x81, 0x32, 0x11, 0x51, 0x00, color]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve(); return resolve();
}); });
@ -96,21 +96,21 @@ export class PUPHub extends LPF2Hub {
let data = null; let data = null;
if (portObj.id === "AB") { if (portObj.id === "AB") {
if (speed instanceof Array) { if (speed instanceof Array) {
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]); data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]);
} else { } else {
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]); data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]);
} }
} else { } else {
// @ts-ignore: The type of speed is properly checked at the start // @ts-ignore: The type of speed is properly checked at the start
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]); data = Buffer.from([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);
setTimeout(() => { setTimeout(() => {
let data = null; let data = null;
if (portObj.id === "AB") { if (portObj.id === "AB") {
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, 0x00]); data = Buffer.from([0x81, portObj.value, 0x11, 0x02, 0x00]);
} else { } else {
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]); data = Buffer.from([0x81, portObj.value, 0x11, 0x60, 0x00, 0x00, 0x00, 0x00]);
} }
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve(); return resolve();
@ -119,13 +119,13 @@ export class PUPHub extends LPF2Hub {
let data = null; let data = null;
if (portObj.id === "AB") { if (portObj.id === "AB") {
if (speed instanceof Array) { if (speed instanceof Array) {
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]); data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed[0]), this._mapSpeed(speed[1])]);
} else { } else {
data = Buffer.from([0x08, 0x00, 0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]); data = Buffer.from([0x81, portObj.value, 0x11, 0x02, this._mapSpeed(speed), this._mapSpeed(speed)]);
} }
} else { } else {
// @ts-ignore: The type of speed is properly checked at the start // @ts-ignore: The type of speed is properly checked at the start
data = Buffer.from([0x0a, 0x00, 0x81, portObj.value, 0x11, 0x60, 0x00, this._mapSpeed(speed), 0x00, 0x00]); data = Buffer.from([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);
return resolve(); return resolve();

View File

@ -88,7 +88,7 @@ export class PUPRemote extends LPF2Hub {
if (color === false) { if (color === false) {
color = 0; color = 0;
} }
const data = Buffer.from([0x08, 0x00, 0x81, 0x34, 0x11, 0x51, 0x00, color]); const data = Buffer.from([0x81, 0x34, 0x11, 0x51, 0x00, color]);
this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data); this._writeMessage(Consts.BLECharacteristics.LPF2_ALL, data);
return resolve(); return resolve();
}); });