Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions lib/switchbot-advertising.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 || '';
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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; // %
Expand Down
5 changes: 3 additions & 2 deletions lib/switchbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Switchbot {
this.noble = noble;
this.ondiscover = null;
this.onadvertisement = null;
this.onlog = null;

// Private properties
this._scanning = false;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down