node-switchbot 1.1.3-beta.8 → 1.1.3-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,7 +60,7 @@ class SwitchbotAdvertising {
60
60
  * If the specified `Peripheral` does not represent any switchbot
61
61
  * device, this method will return `null`.
62
62
  * ---------------------------------------------------------------- */
63
- parse(peripheral) {
63
+ parse(peripheral, onlog) {
64
64
  let ad = peripheral.advertisement;
65
65
  if (!ad || !ad.serviceData) {
66
66
  return null;
@@ -75,22 +75,28 @@ class SwitchbotAdvertising {
75
75
  let sd = null;
76
76
 
77
77
  if (model === 'H') { // WoHand
78
- sd = this._parseServiceDataForWoHand(buf);
78
+ sd = this._parseServiceDataForWoHand(buf, onlog);
79
79
  } else if (model === 'e') { // WoHumi
80
- sd = this._parseServiceDataForWoHumi(buf);
80
+ sd = this._parseServiceDataForWoHumi(buf, onlog);
81
81
  } else if (model === 'T') { // WoSensorTH
82
- sd = this._parseServiceDataForWoSensorTH(buf);
82
+ sd = this._parseServiceDataForWoSensorTH(buf, onlog);
83
83
  } else if (model === 'c') { // WoCurtain
84
- sd = this._parseServiceDataForWoCurtain(buf);
84
+ sd = this._parseServiceDataForWoCurtain(buf, onlog);
85
85
  } else if (model === 's') { // WoMotion
86
- sd = this._parseServiceDataForWoPresence(buf);
86
+ sd = this._parseServiceDataForWoPresence(buf, onlog);
87
87
  } else if (model === 'd') { // WoContact
88
- sd = this._parseServiceDataForWoContact(buf);
88
+ sd = this._parseServiceDataForWoContact(buf, onlog);
89
89
  } else {
90
+ if (onlog && typeof onlog === 'function') {
91
+ onlog(`[parseAdvertising.${peripheral.id}] return null, model "${model}" not available!`);
92
+ }
90
93
  return null;
91
94
  }
92
95
 
93
96
  if (!sd) {
97
+ if (onlog && typeof onlog === 'function') {
98
+ onlog(`[parseAdvertising.${peripheral.id}.${model}] return null, parsed serviceData empty!`);
99
+ }
94
100
  return null;
95
101
  }
96
102
  let address = peripheral.address || '';
@@ -114,11 +120,18 @@ class SwitchbotAdvertising {
114
120
  rssi: peripheral.rssi,
115
121
  serviceData: sd
116
122
  };
123
+
124
+ if (onlog && typeof onlog === 'function') {
125
+ onlog(`[parseAdvertising.${peripheral.id}.${model}] return ${JSON.stringify(data)}`);
126
+ }
117
127
  return data;
118
128
  }
119
129
 
120
- _parseServiceDataForWoHand(buf) {
130
+ _parseServiceDataForWoHand(buf, onlog) {
121
131
  if (buf.length !== 3) {
132
+ if (onlog && typeof onlog === 'function') {
133
+ onlog(`[_parseServiceDataForWoHand] Buffer length ${buf.length} !== 3!`);
134
+ }
122
135
  return null;
123
136
  }
124
137
  let byte1 = buf.readUInt8(1);
@@ -139,8 +152,11 @@ class SwitchbotAdvertising {
139
152
  return data;
140
153
  }
141
154
 
142
- _parseServiceDataForWoHumi(buf) {
155
+ _parseServiceDataForWoHumi(buf, onlog) {
143
156
  if (buf.length !== 8) {
157
+ if (onlog && typeof onlog === 'function') {
158
+ onlog(`[_parseServiceDataForWoHumi] Buffer length ${buf.length} !== 8!`);
159
+ }
144
160
  return null;
145
161
  }
146
162
  let byte1 = buf.readUInt8(1);
@@ -164,8 +180,11 @@ class SwitchbotAdvertising {
164
180
  return data;
165
181
  }
166
182
 
167
- _parseServiceDataForWoSensorTH(buf) {
183
+ _parseServiceDataForWoSensorTH(buf, onlog) {
168
184
  if (buf.length !== 6) {
185
+ if (onlog && typeof onlog === 'function') {
186
+ onlog(`[_parseServiceDataForWoSensorTH] Buffer length ${buf.length} !== 6!`);
187
+ }
169
188
  return null;
170
189
  }
171
190
  let byte2 = buf.readUInt8(2);
@@ -193,8 +212,11 @@ class SwitchbotAdvertising {
193
212
  return data;
194
213
  }
195
214
 
196
- _parseServiceDataForWoPresence(buf) {
215
+ _parseServiceDataForWoPresence(buf, onlog) {
197
216
  if (buf.length !== 6) {
217
+ if (onlog && typeof onlog === 'function') {
218
+ onlog(`[_parseServiceDataForWoPresence] Buffer length ${buf.length} !== 6!`);
219
+ }
198
220
  return null;
199
221
  }
200
222
  let byte1 = buf.readUInt8(1);
@@ -218,8 +240,11 @@ class SwitchbotAdvertising {
218
240
  return data;
219
241
  }
220
242
 
221
- _parseServiceDataForWoContact(buf) {
243
+ _parseServiceDataForWoContact(buf, onlog) {
222
244
  if (buf.length !== 9) {
245
+ if (onlog && typeof onlog === 'function') {
246
+ onlog(`[_parseServiceDataForWoContact] Buffer length ${buf.length} !== 9!`);
247
+ }
223
248
  return null;
224
249
  }
225
250
 
@@ -249,14 +274,18 @@ class SwitchbotAdvertising {
249
274
  return data;
250
275
  }
251
276
 
252
- _parseServiceDataForWoCurtain(buf) {
253
- if (buf.length !== 5) {
277
+ _parseServiceDataForWoCurtain(buf, onlog) {
278
+ if (buf.length !== 5 && buf.length !== 6) {
279
+ if (onlog && typeof onlog === 'function') {
280
+ onlog(`[_parseServiceDataForWoCurtain] Buffer length ${buf.length} !== 5 or 6!`);
281
+ }
254
282
  return null;
255
283
  }
256
284
  let byte1 = buf.readUInt8(1);
257
285
  let byte2 = buf.readUInt8(2);
258
286
  let byte3 = buf.readUInt8(3);
259
287
  let byte4 = buf.readUInt8(4);
288
+ // let byte5 = buf.readUInt8(5);
260
289
 
261
290
  let calibration = (byte1 & 0b01000000) ? true : false; // Whether the calibration is completed
262
291
  let battery = byte2 & 0b01111111; // %
package/lib/switchbot.js CHANGED
@@ -34,6 +34,7 @@ class Switchbot {
34
34
  this.noble = noble;
35
35
  this.ondiscover = null;
36
36
  this.onadvertisement = null;
37
+ this.onlog = null;
37
38
 
38
39
  // Private properties
39
40
  this._scanning = false;
@@ -178,7 +179,7 @@ class Switchbot {
178
179
  }
179
180
 
180
181
  _getDeviceObject(peripheral, id, model) {
181
- let ad = switchbotAdvertising.parse(peripheral);
182
+ let ad = switchbotAdvertising.parse(peripheral, this.onlog);
182
183
  if (this._filterAdvertising(ad, id, model)) {
183
184
  let device = null;
184
185
  switch (ad.serviceData.model) {
@@ -293,7 +294,7 @@ class Switchbot {
293
294
 
294
295
  // Set a handler for the 'discover' event
295
296
  this.noble.on('discover', (peripheral) => {
296
- let ad = switchbotAdvertising.parse(peripheral);
297
+ let ad = switchbotAdvertising.parse(peripheral, this.onlog);
297
298
  if (this._filterAdvertising(ad, p.id, p.model)) {
298
299
  if (this.onadvertisement && typeof (this.onadvertisement) === 'function') {
299
300
  this.onadvertisement(ad);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-switchbot",
3
- "version": "1.1.3-beta.8",
3
+ "version": "1.1.3-beta.9",
4
4
  "description": "The node-switchbot is a Node.js module which allows you to move your Switchbot (Bot)'s arm and Switchbot Curtain(Curtain), also monitor the temperature/humidity from SwitchBot Thermometer & Hygrometer (Meter).",
5
5
  "main": "./lib/switchbot.js",
6
6
  "files": [