diff --git a/lib/switchbot-advertising.js b/lib/switchbot-advertising.js index 95569d3c..3650322c 100644 --- a/lib/switchbot-advertising.js +++ b/lib/switchbot-advertising.js @@ -60,7 +60,7 @@ 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) { return null; @@ -75,22 +75,28 @@ 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 available!`); + } return null; } if (!sd) { + if (onlog && typeof onlog === 'function') { + onlog(`[parseAdvertising.${peripheral.id}.${model}] return null, parsed serviceData empty!`); + } return null; } let address = peripheral.address || ''; @@ -114,11 +120,18 @@ class SwitchbotAdvertising { rssi: peripheral.rssi, serviceData: sd }; + + if (onlog && typeof onlog === 'function') { + onlog(`[parseAdvertising.${peripheral.id}.${model}] return ${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); @@ -139,8 +152,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); @@ -164,8 +180,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); @@ -193,8 +212,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); @@ -218,8 +240,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; } @@ -249,14 +274,18 @@ class SwitchbotAdvertising { return data; } - _parseServiceDataForWoCurtain(buf) { - if (buf.length !== 5) { + _parseServiceDataForWoCurtain(buf, onlog) { + if (buf.length !== 5 && buf.length !== 6) { + if (onlog && typeof onlog === 'function') { + onlog(`[_parseServiceDataForWoCurtain] Buffer length ${buf.length} !== 5 or 6!`); + } return null; } let byte1 = buf.readUInt8(1); 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; // % diff --git a/lib/switchbot.js b/lib/switchbot.js index 2dc7fb18..8721901a 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; @@ -178,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) { @@ -293,7 +294,7 @@ class Switchbot { // Set a handler for the 'discover' event this.noble.on('discover', (peripheral) => { - let ad = switchbotAdvertising.parse(peripheral); + 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);