node-switchbot 1.6.0-beta.11 → 1.6.0-beta.13

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.
@@ -37,7 +37,7 @@ class ParameterChecker {
37
37
  * an `Error` object will be set to `this._error`.
38
38
  *
39
39
  * [Usage]
40
- * let valid = parameterChecker.check(params, {
40
+ * const valid = parameterChecker.check(params, {
41
41
  * level: {
42
42
  * required: false,
43
43
  * type: 'integer',
@@ -50,7 +50,7 @@ class ParameterChecker {
50
50
  * }
51
51
  * });
52
52
  * if(!valid) {
53
- * let e = parameterChecker.error.message;
53
+ * const e = parameterChecker.error.message;
54
54
  * throw new Error(message);
55
55
  * }
56
56
  * ---------------------------------------------------------------- */
@@ -78,13 +78,13 @@ class ParameterChecker {
78
78
  return false;
79
79
  }
80
80
 
81
- let result = true;
82
- let name_list = Object.keys(rules);
81
+ const result = true;
82
+ const name_list = Object.keys(rules);
83
83
 
84
- for (let i = 0; i < name_list.length; i++) {
85
- let name = name_list[i];
86
- let v = obj[name];
87
- let rule = rules[name];
84
+ for (const i = 0; i < name_list.length; i++) {
85
+ const name = name_list[i];
86
+ const v = obj[name];
87
+ const rule = rules[name];
88
88
 
89
89
  if (!rule) {
90
90
  rule = {};
@@ -458,7 +458,7 @@ class ParameterChecker {
458
458
  }
459
459
  }
460
460
  if (typeof rule.minBytes === "number") {
461
- let blen = Buffer.from(value, "utf8").length;
461
+ const blen = Buffer.from(value, "utf8").length;
462
462
  if (blen < rule.minBytes) {
463
463
  this._error = {
464
464
  code: "LENGTH_UNDERFLOW",
@@ -475,7 +475,7 @@ class ParameterChecker {
475
475
  }
476
476
  }
477
477
  if (typeof rule.maxBytes === "number") {
478
- let blen = Buffer.from(value, "utf8").length;
478
+ const blen = Buffer.from(value, "utf8").length;
479
479
  if (blen > rule.maxBytes) {
480
480
  this._error = {
481
481
  code: "LENGTH_OVERFLOW",
@@ -61,13 +61,13 @@ class SwitchbotAdvertising {
61
61
  * device, this method will return `null`.
62
62
  * ---------------------------------------------------------------- */
63
63
  parse(peripheral, onlog) {
64
- let ad = peripheral.advertisement;
64
+ const ad = peripheral.advertisement;
65
65
  if (!ad || !ad.serviceData) {
66
66
  return null;
67
67
  }
68
- let serviceData = ad.serviceData[0] || ad.serviceData;
69
- let manufacturerData = ad.manufacturerData;
70
- let buf = serviceData.data;
68
+ const serviceData = ad.serviceData[0] || ad.serviceData;
69
+ const manufacturerData = ad.manufacturerData;
70
+ const buf = serviceData.data;
71
71
 
72
72
  const bufIsInvalid = !buf || !Buffer.isBuffer(buf) || buf.length < 3;
73
73
  const manufacturerDataIsInvalid =
@@ -79,8 +79,8 @@ class SwitchbotAdvertising {
79
79
  return null;
80
80
  }
81
81
 
82
- let model = buf.slice(0, 1).toString("utf8");
83
- let sd = null;
82
+ const model = buf.slice(0, 1).toString("utf8");
83
+ const sd = null;
84
84
 
85
85
  if (model === "H") {
86
86
  sd = this._parseServiceDataForWoHand(buf, onlog);//WoHand
@@ -123,7 +123,7 @@ class SwitchbotAdvertising {
123
123
  }
124
124
  return null;
125
125
  }
126
- let address = peripheral.address || "";
126
+ const address = peripheral.address || "";
127
127
  if (address === "") {
128
128
  address = peripheral.advertisement.manufacturerData || "";
129
129
  if (address !== "") {
@@ -139,7 +139,7 @@ class SwitchbotAdvertising {
139
139
  } else {
140
140
  address = address.replace(/-/g, ":");
141
141
  }
142
- let data = {
142
+ const data = {
143
143
  id: peripheral.id,
144
144
  address: address,
145
145
  rssi: peripheral.rssi,
@@ -165,14 +165,14 @@ class SwitchbotAdvertising {
165
165
  }
166
166
  return null;
167
167
  }
168
- let byte1 = buf.readUInt8(1);
169
- let byte2 = buf.readUInt8(2);
168
+ const byte1 = buf.readUInt8(1);
169
+ const byte2 = buf.readUInt8(2);
170
170
 
171
- let mode = byte1 & 0b10000000 ? true : false; // Whether the light switch Add-on is used or not
172
- let state = byte1 & 0b01000000 ? true : false; // Whether the switch status is ON or OFF
173
- let battery = byte2 & 0b01111111; // %
171
+ const mode = byte1 & 0b10000000 ? true : false; // Whether the light switch Add-on is used or not
172
+ const state = byte1 & 0b01000000 ? true : false; // Whether the switch status is ON or OFF
173
+ const battery = byte2 & 0b01111111; // %
174
174
 
175
- let data = {
175
+ const data = {
176
176
  model: "H",
177
177
  modelName: "WoHand",
178
178
  mode: mode,
@@ -192,17 +192,17 @@ class SwitchbotAdvertising {
192
192
  }
193
193
  return null;
194
194
  }
195
- let byte2 = buf.readUInt8(2);
196
- let byte3 = buf.readUInt8(3);
197
- let byte4 = buf.readUInt8(4);
198
- let byte5 = buf.readUInt8(5);
199
-
200
- let temp_sign = byte4 & 0b10000000 ? 1 : -1;
201
- let temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10);
202
- let temp_f = (temp_c * 9 / 5) + 32;
195
+ const byte2 = buf.readUInt8(2);
196
+ const byte3 = buf.readUInt8(3);
197
+ const byte4 = buf.readUInt8(4);
198
+ const byte5 = buf.readUInt8(5);
199
+
200
+ const temp_sign = byte4 & 0b10000000 ? 1 : -1;
201
+ const temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10);
202
+ const temp_f = (temp_c * 9 / 5) + 32;
203
203
  temp_f = Math.round(temp_f * 10) / 10;
204
204
 
205
- let data = {
205
+ const data = {
206
206
  model: "T",
207
207
  modelName: "WoSensorTH",
208
208
  temperature: {
@@ -226,15 +226,15 @@ class SwitchbotAdvertising {
226
226
  }
227
227
  return null;
228
228
  }
229
- let byte1 = buf.readUInt8(1);
230
- let byte4 = buf.readUInt8(4);
229
+ const byte1 = buf.readUInt8(1);
230
+ const byte4 = buf.readUInt8(4);
231
231
 
232
232
 
233
- let onState = byte1 & 0b10000000 ? true : false; // 1 - on
234
- let autoMode = byte4 & 0b10000000 ? true : false; // 1 - auto
235
- let percentage = byte4 & 0b01111111; // 0-100%, 101/102/103 - Quick gear 1/2/3
233
+ const onState = byte1 & 0b10000000 ? true : false; // 1 - on
234
+ const autoMode = byte4 & 0b10000000 ? true : false; // 1 - auto
235
+ const percentage = byte4 & 0b01111111; // 0-100%, 101/102/103 - Quick gear 1/2/3
236
236
 
237
- let data = {
237
+ const data = {
238
238
  model: "e",
239
239
  modelName: "WoHumi",
240
240
  onState: onState,
@@ -255,20 +255,20 @@ class SwitchbotAdvertising {
255
255
  return null;
256
256
  }
257
257
 
258
- let byte1 = buf.readUInt8(1);
259
- let byte2 = buf.readUInt8(2);
260
- let byte5 = buf.readUInt8(5);
258
+ const byte1 = buf.readUInt8(1);
259
+ const byte2 = buf.readUInt8(2);
260
+ const byte5 = buf.readUInt8(5);
261
261
 
262
- let tested = byte1 & 0b10000000 ? true : false;
263
- let movement = byte1 & 0b01000000 ? true : false;
264
- let battery = byte2 & 0b01111111;
265
- let led = (byte5 & 0b00100000) >> 5;
266
- let iot = (byte5 & 0b00010000) >> 4;
267
- let sense_distance = (byte5 & 0b00001100) >> 2;
268
- let lightLevel = byte5 & 0b00000011;
269
- let is_light = byte5 & 0b00000010 ? true : false;
262
+ const tested = byte1 & 0b10000000 ? true : false;
263
+ const movement = byte1 & 0b01000000 ? true : false;
264
+ const battery = byte2 & 0b01111111;
265
+ const led = (byte5 & 0b00100000) >> 5;
266
+ const iot = (byte5 & 0b00010000) >> 4;
267
+ const sense_distance = (byte5 & 0b00001100) >> 2;
268
+ const lightLevel = byte5 & 0b00000011;
269
+ const is_light = byte5 & 0b00000010 ? true : false;
270
270
 
271
- let data = {
271
+ const data = {
272
272
  model: "s",
273
273
  modelName: "WoMotion",
274
274
  tested: tested,
@@ -295,21 +295,21 @@ class SwitchbotAdvertising {
295
295
  return null;
296
296
  }
297
297
 
298
- let byte1 = buf.readUInt8(1);
299
- let byte2 = buf.readUInt8(2);
300
- let byte3 = buf.readUInt8(3);
301
- let byte8 = buf.readUInt8(8);
302
-
303
- let hallState = (byte3 >> 1) & 0b00000011;
304
- let tested = byte1 & 0b10000000;
305
- let movement = byte1 & 0b01000000 ? true : false; // 1 - Movement detected
306
- let battery = byte2 & 0b01111111; // %
307
- let contact_open = byte3 & 0b00000010 == 0b00000010;
308
- let contact_timeout = byte3 & 0b00000100 == 0b00000100;
309
- let lightLevel = byte3 & 0b00000001;
310
- let button_count = byte8 & 0b00001111;
311
-
312
- let data = {
298
+ const byte1 = buf.readUInt8(1);
299
+ const byte2 = buf.readUInt8(2);
300
+ const byte3 = buf.readUInt8(3);
301
+ const byte8 = buf.readUInt8(8);
302
+
303
+ const hallState = (byte3 >> 1) & 0b00000011;
304
+ const tested = byte1 & 0b10000000;
305
+ const movement = byte1 & 0b01000000 ? true : false; // 1 - Movement detected
306
+ const battery = byte2 & 0b01111111; // %
307
+ const contact_open = byte3 & 0b00000010 == 0b00000010;
308
+ const contact_timeout = byte3 & 0b00000100 == 0b00000100;
309
+ const lightLevel = byte3 & 0b00000001;
310
+ const button_count = byte8 & 0b00001111;
311
+
312
+ const data = {
313
313
  model: "d",
314
314
  modelName: "WoContact",
315
315
  movement: movement,
@@ -339,20 +339,19 @@ class SwitchbotAdvertising {
339
339
  }
340
340
  return null;
341
341
  }
342
- let byte1 = buf.readUInt8(1);
343
- let byte2 = buf.readUInt8(2);
344
- let byte3 = buf.readUInt8(3);
345
- let byte4 = buf.readUInt8(4);
346
-
347
- let calibration = byte1 & 0b01000000 ? true : false; // Whether the calibration is completed
348
- let battery = byte2 & 0b01111111; // %
349
- let inMotion = byte3 & 0b10000000 ? true : false;
350
- let currPosition = byte3 & 0b01111111; // current positon %
351
- //let currPosition = max(min(byte3 & 0b01111111, 100), 0) //byte3 & 0b01111111; // current positon %
352
- let lightLevel = (byte4 >> 4) & 0b00001111; // light sensor level (1-10)
353
- let deviceChain = byte4 & 0b00000111;
354
-
355
- let data = {
342
+ const byte1 = buf.readUInt8(1);
343
+ const byte2 = buf.readUInt8(2);
344
+ const byte3 = buf.readUInt8(3);
345
+ const byte4 = buf.readUInt8(4);
346
+
347
+ const calibration = byte1 & 0b01000000 ? true : false; // Whether the calibration is completed
348
+ const battery = byte2 & 0b01111111; // %
349
+ const inMotion = byte3 & 0b10000000 ? true : false;
350
+ const currPosition = byte3 & 0b01111111; // current positon %
351
+ const lightLevel = (byte4 >> 4) & 0b00001111; // light sensor level (1-10)
352
+ const deviceChain = byte4 & 0b00000111;
353
+
354
+ const data = {
356
355
  model: "c",
357
356
  modelName: "WoCurtain",
358
357
  calibration: calibration,
@@ -375,33 +374,35 @@ class SwitchbotAdvertising {
375
374
  }
376
375
  return null;
377
376
  }
378
- let byte1 = buf.readUInt8(1);//power and light status
379
- let byte2 = buf.readUInt8(2);//bulb brightness
380
- let byte3 = buf.readUInt8(3);//bulb R
381
- let byte4 = buf.readUInt8(4);//bulb G
382
- let byte5 = buf.readUInt8(5);//bulb B
383
- let byte6 = buf.readUInt8(6);//bulb temperature
384
- let byte7 = buf.readUInt8(7);
385
- let byte8 = buf.readUInt8(8);
386
- let byte9 = buf.readUInt8(9);
387
- let byte10 = buf.readUInt8(10);//bulb mode
388
-
389
- let red = byte3;
390
- let green = byte4;
391
- let blue = byte5;
392
- let color_temperature = byte6;
393
- let state = byte7 & 0b01111111 ? true : false;
394
- let brightness = byte7 & 0b01111111;
395
- let delay = byte8 & 0b10000000;
396
- let preset = byte8 & 0b00001000;
397
- let color_mode = byte8 & 0b00000111;
398
- let speed = byte9 & 0b01111111;
399
- let loop_index = byte10 & 0b11111110;
400
-
401
- let data = {
377
+ const byte1 = buf.readUInt8(1);//power and light status
378
+ const byte2 = buf.readUInt8(2);//bulb brightness
379
+ const byte3 = buf.readUInt8(3);//bulb R
380
+ const byte4 = buf.readUInt8(4);//bulb G
381
+ const byte5 = buf.readUInt8(5);//bulb B
382
+ const byte6 = buf.readUInt8(6);//bulb temperature
383
+ const byte7 = buf.readUInt8(7);
384
+ const byte8 = buf.readUInt8(8);
385
+ const byte9 = buf.readUInt8(9);
386
+ const byte10 = buf.readUInt8(10);//bulb mode
387
+
388
+ const power = byte1;
389
+ const red = byte3;
390
+ const green = byte4;
391
+ const blue = byte5;
392
+ const color_temperature = byte6;
393
+ const state = byte7 & 0b01111111 ? true : false;
394
+ const brightness = byte7 & 0b01111111;
395
+ const delay = byte8 & 0b10000000;
396
+ const preset = byte8 & 0b00001000;
397
+ const color_mode = byte8 & 0b00000111;
398
+ const speed = byte9 & 0b01111111;
399
+ const loop_index = byte10 & 0b11111110;
400
+
401
+ const data = {
402
402
  model: "u",
403
403
  modelName: "WoBulb",
404
404
  color_temperature: color_temperature,
405
+ power: power,
405
406
  state: state,
406
407
  red: red,
407
408
  green: green,
@@ -426,22 +427,22 @@ class SwitchbotAdvertising {
426
427
  }
427
428
  return null;
428
429
  }
429
- let byte9 = buf.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on
430
- let byte10 = buf.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time
431
- let byte11 = buf.readUInt8(11); // byte11: wifi rssi
432
- let byte12 = buf.readUInt8(12); // byte12: bit7: overload?
433
- let byte13 = buf.readUInt8(13); // byte12[bit0~6] + byte13: current power value
434
-
435
- let state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null;
436
- let delay = !!(byte10 & 0b00000001);
437
- let timer = !!(byte10 & 0b00000010);
438
- let syncUtcTime = !!(byte10 & 0b00000100);
439
- let wifiRssi = byte11;
440
- let overload = !!(byte12 & 0b10000000);
441
- let currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt
430
+ const byte9 = buf.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on
431
+ const byte10 = buf.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time
432
+ const byte11 = buf.readUInt8(11); // byte11: wifi rssi
433
+ const byte12 = buf.readUInt8(12); // byte12: bit7: overload?
434
+ const byte13 = buf.readUInt8(13); // byte12[bit0~6] + byte13: current power value
435
+
436
+ const state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null;
437
+ const delay = !!(byte10 & 0b00000001);
438
+ const timer = !!(byte10 & 0b00000010);
439
+ const syncUtcTime = !!(byte10 & 0b00000100);
440
+ const wifiRssi = byte11;
441
+ const overload = !!(byte12 & 0b10000000);
442
+ const currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt
442
443
  // TODO: voltage ???
443
444
 
444
- let data = {
445
+ const data = {
445
446
  model: "g",
446
447
  modelName: "WoPlugMini",
447
448
  state: state,
@@ -465,22 +466,22 @@ class SwitchbotAdvertising {
465
466
  }
466
467
  return null;
467
468
  }
468
- let byte9 = buf.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on
469
- let byte10 = buf.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time
470
- let byte11 = buf.readUInt8(11); // byte11: wifi rssi
471
- let byte12 = buf.readUInt8(12); // byte12: bit7: overload?
472
- let byte13 = buf.readUInt8(13); // byte12[bit0~6] + byte13: current power value
473
-
474
- let state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null;
475
- let delay = !!(byte10 & 0b00000001);
476
- let timer = !!(byte10 & 0b00000010);
477
- let syncUtcTime = !!(byte10 & 0b00000100);
478
- let wifiRssi = byte11;
479
- let overload = !!(byte12 & 0b10000000);
480
- let currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt
469
+ const byte9 = buf.readUInt8(9); // byte9: plug mini state; 0x00=off, 0x80=on
470
+ const byte10 = buf.readUInt8(10); // byte10: bit0: 0=no delay,1=delay, bit1:0=no timer, 1=timer; bit2:0=no sync time, 1=sync'ed time
471
+ const byte11 = buf.readUInt8(11); // byte11: wifi rssi
472
+ const byte12 = buf.readUInt8(12); // byte12: bit7: overload?
473
+ const byte13 = buf.readUInt8(13); // byte12[bit0~6] + byte13: current power value
474
+
475
+ const state = byte9 === 0x00 ? "off" : byte9 === 0x80 ? "on" : null;
476
+ const delay = !!(byte10 & 0b00000001);
477
+ const timer = !!(byte10 & 0b00000010);
478
+ const syncUtcTime = !!(byte10 & 0b00000100);
479
+ const wifiRssi = byte11;
480
+ const overload = !!(byte12 & 0b10000000);
481
+ const currentPower = (((byte12 & 0b01111111) << 8) + byte13) / 10; // in watt
481
482
  // TODO: voltage ???
482
483
 
483
- let data = {
484
+ const data = {
484
485
  model: "j",
485
486
  modelName: "WoPlugMini",
486
487
  state: state,
@@ -504,13 +505,13 @@ class SwitchbotAdvertising {
504
505
  }
505
506
  return null;
506
507
  }
507
- let byte1 = buf.readUInt8(1);
508
- let byte2 = buf.readUInt8(2);
508
+ const byte1 = buf.readUInt8(1);
509
+ const byte2 = buf.readUInt8(2);
509
510
 
510
- let movement = (byte1 & 0b01000000) ? true : false; // 1 - Movement detected
511
- let battery = byte2 & 0b01111111; // %
511
+ const movement = (byte1 & 0b01000000) ? true : false; // 1 - Movement detected
512
+ const battery = byte2 & 0b01111111; // %
512
513
 
513
- let data = {
514
+ const data = {
514
515
  model: "o",
515
516
  modelName: "WoSmartLock",
516
517
  battery: battery,
@@ -529,17 +530,17 @@ class SwitchbotAdvertising {
529
530
  }
530
531
  return null;
531
532
  }
532
- let byte2 = buf.readUInt8(2);
533
- let byte3 = buf.readUInt8(3);
534
- let byte4 = buf.readUInt8(4);
535
- let byte5 = buf.readUInt8(5);
536
-
537
- let temp_sign = byte4 & 0b10000000 ? 1 : -1;
538
- let temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10);
539
- let temp_f = (temp_c * 9 / 5) + 32;
533
+ const byte2 = buf.readUInt8(2);
534
+ const byte3 = buf.readUInt8(3);
535
+ const byte4 = buf.readUInt8(4);
536
+ const byte5 = buf.readUInt8(5);
537
+
538
+ const temp_sign = byte4 & 0b10000000 ? 1 : -1;
539
+ const temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 & 0b00001111) / 10);
540
+ const temp_f = (temp_c * 9 / 5) + 32;
540
541
  temp_f = Math.round(temp_f * 10) / 10;
541
542
 
542
- let data = {
543
+ const data = {
543
544
  model: "i",
544
545
  modelName: "WoSensorTHPlus",
545
546
  temperature: {
@@ -564,28 +565,28 @@ class SwitchbotAdvertising {
564
565
  return null;
565
566
  }
566
567
 
567
- let byte1 = buf.readUInt8(1);//power and light status
568
- let byte2 = buf.readUInt8(2);//bulb brightness
569
- let byte3 = buf.readUInt8(3);//bulb R
570
- let byte4 = buf.readUInt8(4);//bulb G
571
- let byte5 = buf.readUInt8(5);//bulb B
572
- let byte7 = buf.readUInt8(7);
573
- let byte8 = buf.readUInt8(8);
574
- let byte9 = buf.readUInt8(9);
575
- let byte10 = buf.readUInt8(10);
576
-
577
- let state = byte7 & 0b10000000 ? true : false;
578
- let brightness = byte7 & 0b01111111;
579
- let red = byte3;
580
- let green = byte4;
581
- let blue = byte5;
582
- let delay = byte8 & 0b10000000;
583
- let preset = byte8 & 0b00001000;
584
- let color_mode = byte8 & 0b00000111;
585
- let speed = byte9 & 0b01111111;
586
- let loop_index = byte10 & 0b11111110;
587
-
588
- let data = {
568
+ const byte1 = buf.readUInt8(1);//power and light status
569
+ const byte2 = buf.readUInt8(2);//bulb brightness
570
+ const byte3 = buf.readUInt8(3);//bulb R
571
+ const byte4 = buf.readUInt8(4);//bulb G
572
+ const byte5 = buf.readUInt8(5);//bulb B
573
+ const byte7 = buf.readUInt8(7);
574
+ const byte8 = buf.readUInt8(8);
575
+ const byte9 = buf.readUInt8(9);
576
+ const byte10 = buf.readUInt8(10);
577
+
578
+ const state = byte7 & 0b10000000 ? true : false;
579
+ const brightness = byte7 & 0b01111111;
580
+ const red = byte3;
581
+ const green = byte4;
582
+ const blue = byte5;
583
+ const delay = byte8 & 0b10000000;
584
+ const preset = byte8 & 0b00001000;
585
+ const color_mode = byte8 & 0b00000111;
586
+ const speed = byte9 & 0b01111111;
587
+ const loop_index = byte10 & 0b11111110;
588
+
589
+ const data = {
589
590
  model: "r",
590
591
  modelName: "WoStrip",
591
592
  state: state,
@@ -155,7 +155,7 @@ class SwitchbotDeviceWoBulb extends SwitchbotDevice {
155
155
  .then((res_bytes) => {
156
156
  const res_buf = Buffer.from(res_bytes);
157
157
  if (res_buf.length === 2) {
158
- let code = res_buf.readUInt8(1);
158
+ const code = res_buf.readUInt8(1);
159
159
  if (code === 0x00 || code === 0x80) {
160
160
  const is_on = code === 0x80;
161
161
  resolve(is_on);
@@ -93,10 +93,10 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
93
93
 
94
94
  _operateCurtain(bytes) {
95
95
  return new Promise((resolve, reject) => {
96
- let req_buf = Buffer.from(bytes);
96
+ const req_buf = Buffer.from(bytes);
97
97
  this._command(req_buf)
98
98
  .then((res_buf) => {
99
- let code = res_buf.readUInt8(0);
99
+ const code = res_buf.readUInt8(0);
100
100
  if (res_buf.length === 3 && code === 0x01) {
101
101
  resolve();
102
102
  } else {
@@ -79,10 +79,10 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {
79
79
 
80
80
  _operateBot(bytes) {
81
81
  return new Promise((resolve, reject) => {
82
- let req_buf = Buffer.from(bytes);
82
+ const req_buf = Buffer.from(bytes);
83
83
  this._command(req_buf)
84
84
  .then((res_buf) => {
85
- let code = res_buf.readUInt8(0);
85
+ const code = res_buf.readUInt8(0);
86
86
  if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) {
87
87
  resolve();
88
88
  } else {
@@ -79,10 +79,10 @@ class SwitchbotDeviceWoHumi extends SwitchbotDevice {
79
79
 
80
80
  _operateBot(bytes) {
81
81
  return new Promise((resolve, reject) => {
82
- let req_buf = Buffer.from(bytes);
82
+ const req_buf = Buffer.from(bytes);
83
83
  this._command(req_buf)
84
84
  .then((res_buf) => {
85
- let code = res_buf.readUInt8(0);
85
+ const code = res_buf.readUInt8(0);
86
86
  if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) {
87
87
  resolve();
88
88
  } else {
@@ -51,7 +51,7 @@ class SwitchbotDeviceWoPlugMini extends SwitchbotDevice {
51
51
  .then((res_bytes) => {
52
52
  const res_buf = Buffer.from(res_bytes);
53
53
  if (res_buf.length === 2) {
54
- let code = res_buf.readUInt8(1);
54
+ const code = res_buf.readUInt8(1);
55
55
  if (code === 0x00 || code === 0x80) {
56
56
  const is_on = code === 0x80;
57
57
  resolve(is_on);
@@ -149,7 +149,7 @@ class SwitchbotDeviceWoStrip extends SwitchbotDevice {
149
149
  .then((res_bytes) => {
150
150
  const res_buf = Buffer.from(res_bytes);
151
151
  if (res_buf.length === 2) {
152
- let code = res_buf.readUInt8(1);
152
+ const code = res_buf.readUInt8(1);
153
153
  if (code === 0x00 || code === 0x80) {
154
154
  const is_on = code === 0x80;
155
155
  resolve(is_on);
@@ -26,7 +26,7 @@ class SwitchbotDevice {
26
26
  this._COMMAND_TIMEOUT_MSEC = 3000;
27
27
 
28
28
  // Save the device information
29
- let ad = switchbotAdvertising.parse(peripheral);
29
+ const ad = switchbotAdvertising.parse(peripheral);
30
30
  this._id = ad.id;
31
31
  this._address = ad.address;
32
32
  this._model = ad.serviceData.model;
@@ -105,7 +105,7 @@ class SwitchbotDevice {
105
105
  }
106
106
 
107
107
  // Check the connection state
108
- let state = this.connectionState;
108
+ const state = this.connectionState;
109
109
  if (state === "connected") {
110
110
  resolve();
111
111
  return;
@@ -155,7 +155,7 @@ class SwitchbotDevice {
155
155
  _getCharacteristics() {
156
156
  return new Promise((resolve, reject) => {
157
157
  // Set timeout timer
158
- let timer = setTimeout(() => {
158
+ const timer = setTimeout(() => {
159
159
  this._ondisconnect_internal = () => {};
160
160
  timer = null;
161
161
  reject(
@@ -179,20 +179,20 @@ class SwitchbotDevice {
179
179
 
180
180
  // Discover services and characteristics
181
181
  (async () => {
182
- let service_list = await this._discoverServices();
182
+ const service_list = await this._discoverServices();
183
183
  if (!timer) {
184
184
  throw new Error("");
185
185
  }
186
186
 
187
- let chars = {
187
+ const chars = {
188
188
  write: null,
189
189
  notify: null,
190
190
  device: null,
191
191
  };
192
192
 
193
- for (let service of service_list) {
194
- let char_list = await this._discoverCharacteristics(service);
195
- for (let char of char_list) {
193
+ for (const service of service_list) {
194
+ const char_list = await this._discoverCharacteristics(service);
195
+ for (const char of char_list) {
196
196
  if (char.uuid === this._CHAR_UUID_WRITE) {
197
197
  chars.write = char;
198
198
  } else if (char.uuid === this._CHAR_UUID_NOTIFY) {
@@ -230,8 +230,8 @@ class SwitchbotDevice {
230
230
  return;
231
231
  }
232
232
 
233
- let service = null;
234
- for (let s of service_list) {
233
+ const service = null;
234
+ for (const s of service_list) {
235
235
  if (s.uuid === this._SERV_UUID_PRIMARY) {
236
236
  service = s;
237
237
  break;
@@ -260,7 +260,7 @@ class SwitchbotDevice {
260
260
 
261
261
  _subscribe() {
262
262
  return new Promise((resolve, reject) => {
263
- let char = this._chars.notify;
263
+ const char = this._chars.notify;
264
264
  if (!char) {
265
265
  reject(new Error("No notify characteristic was found."));
266
266
  return;
@@ -280,7 +280,7 @@ class SwitchbotDevice {
280
280
 
281
281
  _unsubscribe() {
282
282
  return new Promise((resolve) => {
283
- let char = this._chars.notify;
283
+ const char = this._chars.notify;
284
284
  if (!char) {
285
285
  resolve();
286
286
  return;
@@ -307,7 +307,7 @@ class SwitchbotDevice {
307
307
  return new Promise((resolve, reject) => {
308
308
  this._was_connected_explicitly = false;
309
309
  // Check the connection state
310
- let state = this._peripheral.state;
310
+ const state = this._peripheral.state;
311
311
  if (state === "disconnected") {
312
312
  resolve();
313
313
  return;
@@ -351,7 +351,7 @@ class SwitchbotDevice {
351
351
  * ---------------------------------------------------------------- */
352
352
  getDeviceName() {
353
353
  return new Promise((resolve, reject) => {
354
- let name = "";
354
+ const name = "";
355
355
  this._connect()
356
356
  .then(() => {
357
357
  if (!this._chars.device) {
@@ -392,7 +392,7 @@ class SwitchbotDevice {
392
392
  setDeviceName(name) {
393
393
  return new Promise((resolve, reject) => {
394
394
  // Check the parameters
395
- let valid = parameterChecker.check(
395
+ const valid = parameterChecker.check(
396
396
  { name: name },
397
397
  {
398
398
  name: { required: true, type: "string", minBytes: 1, maxBytes: 100 },
@@ -404,7 +404,7 @@ class SwitchbotDevice {
404
404
  return;
405
405
  }
406
406
 
407
- let buf = Buffer.from(name, "utf8");
407
+ const buf = Buffer.from(name, "utf8");
408
408
  this._connect()
409
409
  .then(() => {
410
410
  if (!this._chars.device) {
@@ -439,7 +439,7 @@ class SwitchbotDevice {
439
439
  return;
440
440
  }
441
441
 
442
- let res_buf = null;
442
+ const res_buf = null;
443
443
 
444
444
  this._connect()
445
445
  .then(() => {
@@ -463,7 +463,7 @@ class SwitchbotDevice {
463
463
 
464
464
  _waitCommandResponse() {
465
465
  return new Promise((resolve, reject) => {
466
- let timer = setTimeout(() => {
466
+ const timer = setTimeout(() => {
467
467
  timer = null;
468
468
  this._onnotify_internal = () => {};
469
469
  reject(new Error("COMMAND_TIMEOUT"));
@@ -484,7 +484,7 @@ class SwitchbotDevice {
484
484
  _read(char) {
485
485
  return new Promise((resolve, reject) => {
486
486
  // Set a timeout timer
487
- let timer = setTimeout(() => {
487
+ const timer = setTimeout(() => {
488
488
  reject("READ_TIMEOUT");
489
489
  }, this._READ_TIMEOUT_MSEC);
490
490
 
@@ -507,7 +507,7 @@ class SwitchbotDevice {
507
507
  _write(char, buf) {
508
508
  return new Promise((resolve, reject) => {
509
509
  // Set a timeout timer
510
- let timer = setTimeout(() => {
510
+ const timer = setTimeout(() => {
511
511
  reject("WRITE_TIMEOUT");
512
512
  }, this._WRITE_TIMEOUT_MSEC);
513
513
 
package/lib/switchbot.js CHANGED
@@ -26,7 +26,7 @@ class Switchbot {
26
26
  * ---------------------------------------------------------------- */
27
27
  constructor(params) {
28
28
  // Check parameters
29
- let noble = null;
29
+ const noble = null;
30
30
  if (params && params.noble) {
31
31
  noble = params.noble;
32
32
  } else {
@@ -83,9 +83,9 @@ class Switchbot {
83
83
  * `SwitchbotDevice` objects representing the found devices.
84
84
  * ---------------------------------------------------------------- */
85
85
  discover(params = {}) {
86
- let promise = new Promise((resolve, reject) => {
86
+ const promise = new Promise((resolve, reject) => {
87
87
  // Check the parameters
88
- let valid = parameterChecker.check(
88
+ const valid = parameterChecker.check(
89
89
  params,
90
90
  {
91
91
  duration: { required: false, type: "integer", min: 1, max: 60000 },
@@ -110,7 +110,7 @@ class Switchbot {
110
110
  }
111
111
 
112
112
  // Determine the values of the parameters
113
- let p = {
113
+ const p = {
114
114
  duration: params.duration || this._DEFAULT_DISCOVERY_DURATION,
115
115
  model: params.model || "",
116
116
  id: params.id || "",
@@ -120,16 +120,16 @@ class Switchbot {
120
120
  // Initialize the noble object
121
121
  this._init()
122
122
  .then(() => {
123
- let peripherals = {};
124
- let timer = null;
125
- let finishDiscovery = () => {
123
+ const peripherals = {};
124
+ const timer = null;
125
+ const finishDiscovery = () => {
126
126
  if (timer) {
127
127
  clearTimeout(timer);
128
128
  }
129
129
  this.noble.removeAllListeners("discover");
130
130
  this.noble.stopScanning();
131
- let device_list = [];
132
- for (let addr in peripherals) {
131
+ const device_list = [];
132
+ for (const addr in peripherals) {
133
133
  device_list.push(peripherals[addr]);
134
134
  }
135
135
  resolve(device_list);
@@ -137,11 +137,11 @@ class Switchbot {
137
137
 
138
138
  // Set a handler for the 'discover' event
139
139
  this.noble.on("discover", (peripheral) => {
140
- let device = this._getDeviceObject(peripheral, p.id, p.model);
140
+ const device = this._getDeviceObject(peripheral, p.id, p.model);
141
141
  if (!device) {
142
142
  return;
143
143
  }
144
- let id = device.id;
144
+ const id = device.id;
145
145
  peripherals[id] = device;
146
146
 
147
147
  if (this.ondiscover && typeof this.ondiscover === "function") {
@@ -177,13 +177,13 @@ class Switchbot {
177
177
  }
178
178
 
179
179
  _init() {
180
- let promise = new Promise((resolve, reject) => {
180
+ const promise = new Promise((resolve, reject) => {
181
181
  switch (this.noble.state) {
182
182
  case "poweredOn":
183
183
  resolve();
184
184
  return;
185
185
  case ("unsupported", "unauthorized", "poweredOff"):
186
- let err = new Error(
186
+ const err = new Error(
187
187
  "Failed to initialize the Noble object: " + this.noble.state
188
188
  );
189
189
  reject(err);
@@ -193,7 +193,7 @@ class Switchbot {
193
193
  if (state === "poweredOn") {
194
194
  resolve();
195
195
  } else {
196
- let err = new Error(
196
+ const err = new Error(
197
197
  "Failed to initialize the Noble object: " + state
198
198
  );
199
199
  reject(err);
@@ -205,9 +205,9 @@ class Switchbot {
205
205
  }
206
206
 
207
207
  _getDeviceObject(peripheral, id, model) {
208
- let ad = switchbotAdvertising.parse(peripheral, this.onlog);
208
+ const ad = switchbotAdvertising.parse(peripheral, this.onlog);
209
209
  if (this._filterAdvertising(ad, id, model)) {
210
- let device = null;
210
+ const device = null;
211
211
  switch (ad.serviceData.model) {
212
212
  case "H":
213
213
  device = new SwitchbotDeviceWoHand(peripheral, this.noble);
@@ -258,7 +258,7 @@ class Switchbot {
258
258
  }
259
259
  if (id) {
260
260
  id = id.toLowerCase().replace(/\:/g, "");
261
- let ad_id = ad.address.toLowerCase().replace(/[^a-z0-9]/g, "");
261
+ const ad_id = ad.address.toLowerCase().replace(/[^a-z0-9]/g, "");
262
262
  if (ad_id !== id) {
263
263
  return false;
264
264
  }
@@ -324,9 +324,9 @@ class Switchbot {
324
324
  * Nothing will be passed to the `resolve()`.
325
325
  * ---------------------------------------------------------------- */
326
326
  startScan(params) {
327
- let promise = new Promise((resolve, reject) => {
327
+ const promise = new Promise((resolve, reject) => {
328
328
  // Check the parameters
329
- let valid = parameterChecker.check(
329
+ const valid = parameterChecker.check(
330
330
  params,
331
331
  {
332
332
  model: {
@@ -352,14 +352,14 @@ class Switchbot {
352
352
  this._init()
353
353
  .then(() => {
354
354
  // Determine the values of the parameters
355
- let p = {
355
+ const p = {
356
356
  model: params.model || "",
357
357
  id: params.id || "",
358
358
  };
359
359
 
360
360
  // Set a handler for the 'discover' event
361
361
  this.noble.on("discover", (peripheral) => {
362
- let ad = switchbotAdvertising.parse(peripheral, this.onlog);
362
+ const ad = switchbotAdvertising.parse(peripheral, this.onlog);
363
363
  if (this._filterAdvertising(ad, p.id, p.model)) {
364
364
  if (
365
365
  this.onadvertisement &&
@@ -419,7 +419,7 @@ class Switchbot {
419
419
  wait(msec) {
420
420
  return new Promise((resolve, reject) => {
421
421
  // Check the parameters
422
- let valid = parameterChecker.check(
422
+ const valid = parameterChecker.check(
423
423
  { msec: msec },
424
424
  {
425
425
  msec: { required: true, type: "integer", min: 0 },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-switchbot",
3
- "version": "1.6.0-beta.11",
3
+ "version": "1.6.0-beta.13",
4
4
  "description": "The node-switchbot is a Node.js module which allows you to control your Switchbot Devices through Bluetooth (BLE).",
5
5
  "main": "./lib/switchbot.js",
6
6
  "files": [