From abbbd218ee6097adede00728b22a5b2811899f67 Mon Sep 17 00:00:00 2001 From: Leo Bonnargent Date: Wed, 13 Nov 2019 20:58:09 +0100 Subject: [PATCH 1/4] Add mac address to hub/lpf2hub - should close nathankellenicki/node-poweredup/issues/46 --- src/hub.ts | 10 ++++++++++ src/lpf2hub.ts | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/hub.ts b/src/hub.ts index 2d1ed25..1e18b2d 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -25,6 +25,7 @@ export class Hub extends EventEmitter { protected _name: string = ""; protected _firmwareVersion: string = "0.0.00.0000"; + protected _macAddress: string = "00:00:00:00:00:00"; protected _batteryLevel: number = 100; protected _voltage: number = 0; protected _current: number = 0; @@ -63,6 +64,15 @@ export class Hub extends EventEmitter { } + /** + * @readonly + * @property {string} macAddress Primary mac address of the hub + */ + public get macAddress () { + return this._macAddress; + } + + /** * @readonly * @property {string} uuid UUID of the hub diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index ee8e583..8d23f09 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -20,6 +20,17 @@ export class LPF2Hub extends Hub { return [t[0], t[1], t.substring(2, 4), t.substring(4)].join("."); } + private static decodeMacAddress(v: Uint8Array) { + return [ + ("00" + v[0].toString(16)).slice(-2), + ("00" + v[1].toString(16)).slice(-2), + ("00" + v[2].toString(16)).slice(-2), + ("00" + v[3].toString(16)).slice(-2), + ("00" + v[4].toString(16)).slice(-2), + ("00" + v[5].toString(16)).slice(-2), + ].join(":"); + } + protected _ledPort: number = 0x32; private _lastTiltX: number = 0; @@ -37,6 +48,7 @@ export class LPF2Hub extends Hub { this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x02, 0x02])); // Activate button reports this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x03, 0x05])); // Request firmware version this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x06, 0x02])); // Activate battery level reports + this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x01, 0x0d, 0x05])); // Request primary MAC address this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate voltage reports this._writeMessage(Consts.BLECharacteristic.LPF2_ALL, Buffer.from([0x41, 0x3b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])); // Activate current reports if (this.type === Consts.HubType.DUPLO_TRAIN_HUB) { @@ -250,6 +262,10 @@ export class LPF2Hub extends Hub { this._firmwareVersion = LPF2Hub.decodeVersion(data.readInt32LE(5)); this._checkFirmware(this._firmwareVersion); + // primary MAC Address + } else if (data[3] === 0x0d) { + this._macAddress = LPF2Hub.decodeMacAddress(data.slice(4, 10)); + // Battery level reports } else if (data[3] === 0x06) { this._batteryLevel = data[5]; From 7c23bd437fd957b48d7b84e820cf91bb058c9b73 Mon Sep 17 00:00:00 2001 From: Leo Bonnargent Date: Wed, 13 Nov 2019 21:38:59 +0100 Subject: [PATCH 2/4] Better MAC address paring - as per @dlech suggestion --- src/lpf2hub.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index fdce955..af8b6cd 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -21,14 +21,7 @@ export class LPF2Hub extends Hub { } private static decodeMacAddress(v: Uint8Array) { - return [ - ("00" + v[0].toString(16)).slice(-2), - ("00" + v[1].toString(16)).slice(-2), - ("00" + v[2].toString(16)).slice(-2), - ("00" + v[3].toString(16)).slice(-2), - ("00" + v[4].toString(16)).slice(-2), - ("00" + v[5].toString(16)).slice(-2), - ].join(":"); + return Array.from(v).map((n) => ("00" + n.toString(16)).slice(-2)).join(":"); } protected _ledPort: number = 0x32; From 1d575cf63ce7ba299095cba610c1708780ca9bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Bonnargent?= Date: Wed, 13 Nov 2019 22:13:49 +0100 Subject: [PATCH 3/4] code optimization from @nutki MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Michał Szafrański --- src/lpf2hub.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index af8b6cd..be68cb2 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -21,7 +21,7 @@ export class LPF2Hub extends Hub { } private static decodeMacAddress(v: Uint8Array) { - return Array.from(v).map((n) => ("00" + n.toString(16)).slice(-2)).join(":"); + return Array.from(v).map((n) => n.toString(16).padStart(2, "0")).join(":"); } protected _ledPort: number = 0x32; From 89ad6fe39badaed13bd03bef8b1365e354771ea9 Mon Sep 17 00:00:00 2001 From: Leo Bonnargent Date: Wed, 13 Nov 2019 23:30:45 +0100 Subject: [PATCH 4/4] Use primaryMACAddress naming for consistency --- src/hub.ts | 8 ++++---- src/lpf2hub.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/hub.ts b/src/hub.ts index 0169563..684fd5c 100644 --- a/src/hub.ts +++ b/src/hub.ts @@ -26,7 +26,7 @@ export class Hub extends EventEmitter { protected _name: string = ""; protected _firmwareVersion: string = "0.0.00.0000"; protected _hardwareVersion: string = "0.0.00.0000"; - protected _macAddress: string = "00:00:00:00:00:00"; + protected _primaryMACAddress: string = "00:00:00:00:00:00"; protected _batteryLevel: number = 100; protected _voltage: number = 0; protected _current: number = 0; @@ -75,10 +75,10 @@ export class Hub extends EventEmitter { /** * @readonly - * @property {string} macAddress Primary mac address of the hub + * @property {string} primaryMACAddress Primary MAC address of the hub */ - public get macAddress () { - return this._macAddress; + public get primaryMACAddress () { + return this._primaryMACAddress; } diff --git a/src/lpf2hub.ts b/src/lpf2hub.ts index 5f71a2f..d8393d1 100644 --- a/src/lpf2hub.ts +++ b/src/lpf2hub.ts @@ -21,7 +21,7 @@ export class LPF2Hub extends Hub { return [t[0], t[1], t.substring(2, 4), t.substring(4)].join("."); } - private static decodeMacAddress(v: Uint8Array) { + private static decodeMACAddress(v: Uint8Array) { return Array.from(v).map((n) => toHex(n, 2)).join(":"); } @@ -274,7 +274,7 @@ export class LPF2Hub extends Hub { // primary MAC Address } else if (data[3] === 0x0d) { - this._macAddress = LPF2Hub.decodeMacAddress(data.slice(4, 10)); + this._primaryMACAddress = LPF2Hub.decodeMACAddress(data.slice(4, 10)); // Battery level reports } else if (data[3] === 0x06) {