From 5b720fdc75c321e1130f120513407776efe226bc Mon Sep 17 00:00:00 2001 From: Patrick <49351462+patrickbs96@users.noreply.github.com> Date: Fri, 4 Mar 2022 10:55:57 +0100 Subject: [PATCH 1/7] AddLogging --- lib/switchbot.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/switchbot.js b/lib/switchbot.js index 2dc7fb18..a9229286 100644 --- a/lib/switchbot.js +++ b/lib/switchbot.js @@ -34,6 +34,7 @@ class Switchbot { this.noble = noble; this.ondiscover = null; this.onadvertisement = null; + this.onlog = null; // Private properties this._scanning = false; @@ -293,7 +294,13 @@ class Switchbot { // Set a handler for the 'discover' event this.noble.on('discover', (peripheral) => { + if (this.onlog && typeof this.onlog === 'function') { + this.onlog(`[startScan.discover.beforeParse] ${JSON.stringify(peripheral)}`); + } let ad = switchbotAdvertising.parse(peripheral); + if (this.onlog && typeof this.onlog === 'function') { + this.onlog(`[startScan.discover.afterParse] ${JSON.stringify(ad)}`); + } if (this._filterAdvertising(ad, p.id, p.model)) { if (this.onadvertisement && typeof (this.onadvertisement) === 'function') { this.onadvertisement(ad); From 3c59fa85031e0031820eacb4f260e48d8fa03c76 Mon Sep 17 00:00:00 2001 From: Patrick <49351462+patrickbs96@users.noreply.github.com> Date: Fri, 4 Mar 2022 11:29:44 +0100 Subject: [PATCH 2/7] add logging to parsing function --- lib/switchbot-advertising.js | 18 +++++++++++++++++- lib/switchbot.js | 8 +------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index 95569d3c..22d4606b 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -60,14 +60,20 @@ class SwitchbotAdvertising { * If the specified `Peripheral` does not represent any switchbot * device, this method will return `null`. * ---------------------------------------------------------------- */ - parse(peripheral) { + parse(peripheral, onlog) { let ad = peripheral.advertisement; if (!ad || !ad.serviceData) { + if (onlog && typeof onlog === 'function') { + onlog(`[parseAdvertising] return null, Empty or no serviceData!`); + } return null; } let serviceData = ad.serviceData[0] || ad.serviceData; let buf = serviceData.data; if (!buf || !Buffer.isBuffer(buf) || buf.length < 3) { + if (onlog && typeof onlog === 'function') { + onlog(`[parseAdvertising] return null, Empty or no buffer!`); + } return null; } @@ -87,10 +93,16 @@ class SwitchbotAdvertising { } else if (model === 'd') { // WoContact sd = this._parseServiceDataForWoContact(buf); } else { + if (onlog && typeof onlog === 'function') { + onlog(`[parseAdvertising] model ${model} not detected!`); + } return null; } if (!sd) { + if (onlog && typeof onlog === 'function') { + onlog(`[parseAdvertising] parsed serviceData empty!`); + } return null; } let address = peripheral.address || ''; @@ -114,6 +126,10 @@ class SwitchbotAdvertising { rssi: peripheral.rssi, serviceData: sd }; + + if (onlog && typeof onlog === 'function') { + onlog(`[parseAdvertising] result: ${JSON.stringify(data)}`); + } return data; } diff --git a/lib/switchbot.js b/lib/switchbot.js index a9229286..dac40f17 100644 --- a/lib/switchbot.js +++ b/lib/switchbot.js @@ -294,13 +294,7 @@ class Switchbot { // Set a handler for the 'discover' event this.noble.on('discover', (peripheral) => { - if (this.onlog && typeof this.onlog === 'function') { - this.onlog(`[startScan.discover.beforeParse] ${JSON.stringify(peripheral)}`); - } - let ad = switchbotAdvertising.parse(peripheral); - if (this.onlog && typeof this.onlog === 'function') { - this.onlog(`[startScan.discover.afterParse] ${JSON.stringify(ad)}`); - } + let ad = switchbotAdvertising.parse(peripheral, this.onlog); if (this._filterAdvertising(ad, p.id, p.model)) { if (this.onadvertisement && typeof (this.onadvertisement) === 'function') { this.onadvertisement(ad); From d9c7b78eb6ab748d71190d86f1210aab7711ac22 Mon Sep 17 00:00:00 2001 From: Patrick <49351462+patrickbs96@users.noreply.github.com> Date: Fri, 4 Mar 2022 11:44:11 +0100 Subject: [PATCH 3/7] Add Id to logging --- lib/switchbot-advertising.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index 22d4606b..b94410a5 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -64,7 +64,7 @@ class SwitchbotAdvertising { let ad = peripheral.advertisement; if (!ad || !ad.serviceData) { if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising] return null, Empty or no serviceData!`); + onlog(`[parseAdvertising.${peripheral.id}] return null, Empty or no serviceData!`); } return null; } @@ -72,7 +72,7 @@ class SwitchbotAdvertising { let buf = serviceData.data; if (!buf || !Buffer.isBuffer(buf) || buf.length < 3) { if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising] return null, Empty or no buffer!`); + onlog(`[parseAdvertising.${peripheral.id}] return null, Empty or no buffer!`); } return null; } @@ -94,14 +94,14 @@ class SwitchbotAdvertising { sd = this._parseServiceDataForWoContact(buf); } else { if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising] model ${model} not detected!`); + onlog(`[parseAdvertising.${peripheral.id}] return null, model ${model} not detected!`); } return null; } if (!sd) { if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising] parsed serviceData empty!`); + onlog(`[parseAdvertising.${peripheral.id}] return null, parsed serviceData empty!`); } return null; } @@ -128,7 +128,7 @@ class SwitchbotAdvertising { }; if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising] result: ${JSON.stringify(data)}`); + onlog(`[parseAdvertising.${peripheral.id}] result: ${JSON.stringify(data)}`); } return data; } From 24d62d1ca0e9601a11c97a90ed9b421854c647c0 Mon Sep 17 00:00:00 2001 From: Patrick <49351462+patrickbs96@users.noreply.github.com> Date: Fri, 4 Mar 2022 12:40:14 +0100 Subject: [PATCH 4/7] Extend Logging --- lib/switchbot-advertising.js | 58 +++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index b94410a5..8fdad03e 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -63,17 +63,17 @@ class SwitchbotAdvertising { parse(peripheral, onlog) { let ad = peripheral.advertisement; if (!ad || !ad.serviceData) { - if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising.${peripheral.id}] return null, Empty or no serviceData!`); - } + // if (onlog && typeof onlog === 'function') { + // onlog(`[parseAdvertising.${peripheral.id}] return null, Empty or no serviceData!`); + // } return null; } let serviceData = ad.serviceData[0] || ad.serviceData; let buf = serviceData.data; if (!buf || !Buffer.isBuffer(buf) || buf.length < 3) { - if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising.${peripheral.id}] return null, Empty or no buffer!`); - } + // if (onlog && typeof onlog === 'function') { + // onlog(`[parseAdvertising.${peripheral.id}] return null, Empty or no buffer!`); + // } return null; } @@ -81,17 +81,17 @@ class SwitchbotAdvertising { let sd = null; if (model === 'H') { // WoHand - sd = this._parseServiceDataForWoHand(buf); + sd = this._parseServiceDataForWoHand(buf, onlog); } else if (model === 'e') { // WoHumi - sd = this._parseServiceDataForWoHumi(buf); + sd = this._parseServiceDataForWoHumi(buf), onlog; } else if (model === 'T') { // WoSensorTH - sd = this._parseServiceDataForWoSensorTH(buf); + sd = this._parseServiceDataForWoSensorTH(buf, onlog); } else if (model === 'c') { // WoCurtain - sd = this._parseServiceDataForWoCurtain(buf); + sd = this._parseServiceDataForWoCurtain(buf, onlog); } else if (model === 's') { // WoMotion - sd = this._parseServiceDataForWoPresence(buf); + sd = this._parseServiceDataForWoPresence(buf, onlog); } else if (model === 'd') { // WoContact - sd = this._parseServiceDataForWoContact(buf); + sd = this._parseServiceDataForWoContact(buf, onlog); } else { if (onlog && typeof onlog === 'function') { onlog(`[parseAdvertising.${peripheral.id}] return null, model ${model} not detected!`); @@ -101,7 +101,7 @@ class SwitchbotAdvertising { if (!sd) { if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising.${peripheral.id}] return null, parsed serviceData empty!`); + onlog(`[parseAdvertising.${peripheral.id}.${model}] return null, parsed serviceData empty!`); } return null; } @@ -128,13 +128,16 @@ class SwitchbotAdvertising { }; if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising.${peripheral.id}] result: ${JSON.stringify(data)}`); + onlog(`[parseAdvertising.${peripheral.id}.${model}] result: ${JSON.stringify(data)}`); } return data; } - _parseServiceDataForWoHand(buf) { + _parseServiceDataForWoHand(buf, onlog) { if (buf.length !== 3) { + if (onlog && typeof onlog === 'function') { + onlog(`[_parseServiceDataForWoHand] Buffer length ${buf.length} !== 3!`); + } return null; } let byte1 = buf.readUInt8(1); @@ -155,8 +158,11 @@ class SwitchbotAdvertising { return data; } - _parseServiceDataForWoHumi(buf) { + _parseServiceDataForWoHumi(buf, onlog) { if (buf.length !== 8) { + if (onlog && typeof onlog === 'function') { + onlog(`[_parseServiceDataForWoHumi] Buffer length ${buf.length} !== 8!`); + } return null; } let byte1 = buf.readUInt8(1); @@ -180,8 +186,11 @@ class SwitchbotAdvertising { return data; } - _parseServiceDataForWoSensorTH(buf) { + _parseServiceDataForWoSensorTH(buf, onlog) { if (buf.length !== 6) { + if (onlog && typeof onlog === 'function') { + onlog(`[_parseServiceDataForWoSensorTH] Buffer length ${buf.length} !== 6!`); + } return null; } let byte2 = buf.readUInt8(2); @@ -209,8 +218,11 @@ class SwitchbotAdvertising { return data; } - _parseServiceDataForWoPresence(buf) { + _parseServiceDataForWoPresence(buf, onlog) { if (buf.length !== 6) { + if (onlog && typeof onlog === 'function') { + onlog(`[_parseServiceDataForWoPresence] Buffer length ${buf.length} !== 6!`); + } return null; } let byte1 = buf.readUInt8(1); @@ -234,8 +246,11 @@ class SwitchbotAdvertising { return data; } - _parseServiceDataForWoContact(buf) { + _parseServiceDataForWoContact(buf, onlog) { if (buf.length !== 9) { + if (onlog && typeof onlog === 'function') { + onlog(`[_parseServiceDataForWoContact] Buffer length ${buf.length} !== 9!`); + } return null; } @@ -265,8 +280,11 @@ class SwitchbotAdvertising { return data; } - _parseServiceDataForWoCurtain(buf) { + _parseServiceDataForWoCurtain(buf, onlog) { if (buf.length !== 5) { + if (onlog && typeof onlog === 'function') { + onlog(`[_parseServiceDataForWoCurtain] Buffer length ${buf.length} !== 5!`); + } return null; } let byte1 = buf.readUInt8(1); From 2b1adf7ec4359d310ee0f2718496ff1f1b5bbb8b Mon Sep 17 00:00:00 2001 From: Patrick <49351462+patrickbs96@users.noreply.github.com> Date: Fri, 4 Mar 2022 12:46:28 +0100 Subject: [PATCH 5/7] Increase Buffer length --- lib/switchbot-advertising.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index 8fdad03e..5bb9970d 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -281,9 +281,9 @@ class SwitchbotAdvertising { } _parseServiceDataForWoCurtain(buf, onlog) { - if (buf.length !== 5) { + if (buf.length !== 6) { if (onlog && typeof onlog === 'function') { - onlog(`[_parseServiceDataForWoCurtain] Buffer length ${buf.length} !== 5!`); + onlog(`[_parseServiceDataForWoCurtain] Buffer length ${buf.length} !== 6!`); } return null; } @@ -291,6 +291,7 @@ class SwitchbotAdvertising { let byte2 = buf.readUInt8(2); let byte3 = buf.readUInt8(3); let byte4 = buf.readUInt8(4); + // let byte5 = buf.readUInt8(5); let calibration = (byte1 & 0b01000000) ? true : false; // Whether the calibration is completed let battery = byte2 & 0b01111111; // % From 5ec219320c35c74e20bd62233ebb972459b15ebc Mon Sep 17 00:00:00 2001 From: Patrick <49351462+patrickbs96@users.noreply.github.com> Date: Fri, 4 Mar 2022 16:38:30 +0100 Subject: [PATCH 6/7] Buffer Size for Curtain 5 or 6 --- lib/switchbot-advertising.js | 14 ++++---------- lib/switchbot.js | 2 +- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index 5bb9970d..dcd956cf 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -63,17 +63,11 @@ class SwitchbotAdvertising { parse(peripheral, onlog) { let ad = peripheral.advertisement; if (!ad || !ad.serviceData) { - // if (onlog && typeof onlog === 'function') { - // onlog(`[parseAdvertising.${peripheral.id}] return null, Empty or no serviceData!`); - // } return null; } let serviceData = ad.serviceData[0] || ad.serviceData; let buf = serviceData.data; if (!buf || !Buffer.isBuffer(buf) || buf.length < 3) { - // if (onlog && typeof onlog === 'function') { - // onlog(`[parseAdvertising.${peripheral.id}] return null, Empty or no buffer!`); - // } return null; } @@ -94,7 +88,7 @@ class SwitchbotAdvertising { sd = this._parseServiceDataForWoContact(buf, onlog); } else { if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising.${peripheral.id}] return null, model ${model} not detected!`); + onlog(`[parseAdvertising.${peripheral.id}] return null, model "${model}" not available!`); } return null; } @@ -128,7 +122,7 @@ class SwitchbotAdvertising { }; if (onlog && typeof onlog === 'function') { - onlog(`[parseAdvertising.${peripheral.id}.${model}] result: ${JSON.stringify(data)}`); + onlog(`[parseAdvertising.${peripheral.id}.${model}] return ${JSON.stringify(data)}`); } return data; } @@ -281,9 +275,9 @@ class SwitchbotAdvertising { } _parseServiceDataForWoCurtain(buf, onlog) { - if (buf.length !== 6) { + if (buf.length !== 5 && buf.length !== 6) { if (onlog && typeof onlog === 'function') { - onlog(`[_parseServiceDataForWoCurtain] Buffer length ${buf.length} !== 6!`); + onlog(`[_parseServiceDataForWoCurtain] Buffer length ${buf.length} !== 5 or 6!`); } return null; } diff --git a/lib/switchbot.js b/lib/switchbot.js index dac40f17..8721901a 100644 --- a/lib/switchbot.js +++ b/lib/switchbot.js @@ -179,7 +179,7 @@ class Switchbot { } _getDeviceObject(peripheral, id, model) { - let ad = switchbotAdvertising.parse(peripheral); + let ad = switchbotAdvertising.parse(peripheral, this.onlog); if (this._filterAdvertising(ad, id, model)) { let device = null; switch (ad.serviceData.model) { From 836ac24a5597fbc20533f4d1fceee342f89b4e1d Mon Sep 17 00:00:00 2001 From: Patrick <49351462+patrickbs96@users.noreply.github.com> Date: Fri, 4 Mar 2022 18:16:47 +0100 Subject: [PATCH 7/7] correct error in onlog --- lib/switchbot-advertising.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index dcd956cf..3650322c 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -77,7 +77,7 @@ class SwitchbotAdvertising { if (model === 'H') { // WoHand sd = this._parseServiceDataForWoHand(buf, onlog); } else if (model === 'e') { // WoHumi - sd = this._parseServiceDataForWoHumi(buf), onlog; + sd = this._parseServiceDataForWoHumi(buf, onlog); } else if (model === 'T') { // WoSensorTH sd = this._parseServiceDataForWoSensorTH(buf, onlog); } else if (model === 'c') { // WoCurtain