@switchbot/homebridge-switchbot 2.6.0-beta.4 → 2.6.0

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.
Files changed (143) hide show
  1. package/.vscode/tasks.json +13 -0
  2. package/CHANGELOG.md +4 -2
  3. package/README.md +18 -9
  4. package/dist/device/blindtilt.d.ts +1 -5
  5. package/dist/device/blindtilt.d.ts.map +1 -1
  6. package/dist/device/blindtilt.js +80 -140
  7. package/dist/device/blindtilt.js.map +1 -1
  8. package/dist/device/bot.d.ts +1 -5
  9. package/dist/device/bot.d.ts.map +1 -1
  10. package/dist/device/bot.js +79 -138
  11. package/dist/device/bot.js.map +1 -1
  12. package/dist/device/ceilinglight.d.ts +2 -6
  13. package/dist/device/ceilinglight.d.ts.map +1 -1
  14. package/dist/device/ceilinglight.js +157 -304
  15. package/dist/device/ceilinglight.js.map +1 -1
  16. package/dist/device/colorbulb.d.ts +2 -6
  17. package/dist/device/colorbulb.d.ts.map +1 -1
  18. package/dist/device/colorbulb.js +164 -311
  19. package/dist/device/colorbulb.js.map +1 -1
  20. package/dist/device/contact.d.ts +1 -5
  21. package/dist/device/contact.d.ts.map +1 -1
  22. package/dist/device/contact.js +36 -72
  23. package/dist/device/contact.js.map +1 -1
  24. package/dist/device/curtain.d.ts +1 -5
  25. package/dist/device/curtain.d.ts.map +1 -1
  26. package/dist/device/curtain.js +78 -138
  27. package/dist/device/curtain.js.map +1 -1
  28. package/dist/device/hub.d.ts +2 -6
  29. package/dist/device/hub.d.ts.map +1 -1
  30. package/dist/device/hub.js +30 -63
  31. package/dist/device/hub.js.map +1 -1
  32. package/dist/device/humidifier.d.ts +1 -5
  33. package/dist/device/humidifier.d.ts.map +1 -1
  34. package/dist/device/humidifier.js +115 -228
  35. package/dist/device/humidifier.js.map +1 -1
  36. package/dist/device/iosensor.d.ts +1 -5
  37. package/dist/device/iosensor.d.ts.map +1 -1
  38. package/dist/device/iosensor.js +38 -74
  39. package/dist/device/iosensor.js.map +1 -1
  40. package/dist/device/{striplight.d.ts → lightstrip.d.ts} +2 -10
  41. package/dist/device/lightstrip.d.ts.map +1 -0
  42. package/dist/device/{striplight.js → lightstrip.js} +138 -194
  43. package/dist/device/lightstrip.js.map +1 -0
  44. package/dist/device/lock.d.ts +1 -5
  45. package/dist/device/lock.d.ts.map +1 -1
  46. package/dist/device/lock.js +66 -126
  47. package/dist/device/lock.js.map +1 -1
  48. package/dist/device/meter.d.ts +1 -5
  49. package/dist/device/meter.d.ts.map +1 -1
  50. package/dist/device/meter.js +38 -74
  51. package/dist/device/meter.js.map +1 -1
  52. package/dist/device/meterplus.d.ts +1 -5
  53. package/dist/device/meterplus.d.ts.map +1 -1
  54. package/dist/device/meterplus.js +39 -75
  55. package/dist/device/meterplus.js.map +1 -1
  56. package/dist/device/motion.d.ts +1 -5
  57. package/dist/device/motion.d.ts.map +1 -1
  58. package/dist/device/motion.js +35 -72
  59. package/dist/device/motion.js.map +1 -1
  60. package/dist/device/plug.d.ts +1 -5
  61. package/dist/device/plug.d.ts.map +1 -1
  62. package/dist/device/plug.js +65 -126
  63. package/dist/device/plug.js.map +1 -1
  64. package/dist/device/robotvacuumcleaner.d.ts +1 -5
  65. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  66. package/dist/device/robotvacuumcleaner.js +79 -151
  67. package/dist/device/robotvacuumcleaner.js.map +1 -1
  68. package/dist/irdevice/airconditioner.d.ts +3 -7
  69. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  70. package/dist/irdevice/airconditioner.js +34 -62
  71. package/dist/irdevice/airconditioner.js.map +1 -1
  72. package/dist/irdevice/airpurifier.d.ts +3 -7
  73. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  74. package/dist/irdevice/airpurifier.js +31 -59
  75. package/dist/irdevice/airpurifier.js.map +1 -1
  76. package/dist/irdevice/camera.d.ts +1 -5
  77. package/dist/irdevice/camera.d.ts.map +1 -1
  78. package/dist/irdevice/camera.js +30 -57
  79. package/dist/irdevice/camera.js.map +1 -1
  80. package/dist/irdevice/fan.d.ts +2 -6
  81. package/dist/irdevice/fan.d.ts.map +1 -1
  82. package/dist/irdevice/fan.js +39 -66
  83. package/dist/irdevice/fan.js.map +1 -1
  84. package/dist/irdevice/light.d.ts +1 -5
  85. package/dist/irdevice/light.d.ts.map +1 -1
  86. package/dist/irdevice/light.js +32 -59
  87. package/dist/irdevice/light.js.map +1 -1
  88. package/dist/irdevice/other.d.ts +1 -5
  89. package/dist/irdevice/other.d.ts.map +1 -1
  90. package/dist/irdevice/other.js +30 -57
  91. package/dist/irdevice/other.js.map +1 -1
  92. package/dist/irdevice/tv.d.ts +3 -7
  93. package/dist/irdevice/tv.d.ts.map +1 -1
  94. package/dist/irdevice/tv.js +46 -76
  95. package/dist/irdevice/tv.js.map +1 -1
  96. package/dist/irdevice/vacuumcleaner.d.ts +2 -6
  97. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  98. package/dist/irdevice/vacuumcleaner.js +30 -58
  99. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  100. package/dist/irdevice/waterheater.d.ts +7 -23
  101. package/dist/irdevice/waterheater.d.ts.map +1 -1
  102. package/dist/irdevice/waterheater.js +41 -74
  103. package/dist/irdevice/waterheater.js.map +1 -1
  104. package/dist/platform.d.ts +14 -5
  105. package/dist/platform.d.ts.map +1 -1
  106. package/dist/platform.js +156 -149
  107. package/dist/platform.js.map +1 -1
  108. package/dist/settings.d.ts +4 -0
  109. package/dist/settings.d.ts.map +1 -1
  110. package/dist/settings.js +5 -1
  111. package/dist/settings.js.map +1 -1
  112. package/nodemon.json +1 -1
  113. package/package.json +10 -10
  114. package/src/device/blindtilt.ts +122 -181
  115. package/src/device/bot.ts +96 -149
  116. package/src/device/ceilinglight.ts +172 -315
  117. package/src/device/colorbulb.ts +178 -324
  118. package/src/device/contact.ts +48 -78
  119. package/src/device/curtain.ts +93 -148
  120. package/src/device/hub.ts +34 -63
  121. package/src/device/humidifier.ts +135 -243
  122. package/src/device/iosensor.ts +49 -79
  123. package/src/device/{striplight.ts → lightstrip.ts} +148 -200
  124. package/src/device/lock.ts +78 -134
  125. package/src/device/meter.ts +48 -78
  126. package/src/device/meterplus.ts +50 -80
  127. package/src/device/motion.ts +46 -77
  128. package/src/device/plug.ts +76 -134
  129. package/src/device/robotvacuumcleaner.ts +91 -159
  130. package/src/irdevice/airconditioner.ts +56 -81
  131. package/src/irdevice/airpurifier.ts +41 -67
  132. package/src/irdevice/camera.ts +35 -60
  133. package/src/irdevice/fan.ts +46 -71
  134. package/src/irdevice/light.ts +38 -63
  135. package/src/irdevice/other.ts +35 -60
  136. package/src/irdevice/tv.ts +52 -80
  137. package/src/irdevice/vacuumcleaner.ts +36 -62
  138. package/src/irdevice/waterheater.ts +46 -76
  139. package/src/platform.ts +144 -158
  140. package/src/settings.ts +5 -0
  141. package/.github/npm-version-script.js +0 -81
  142. package/dist/device/striplight.d.ts.map +0 -1
  143. package/dist/device/striplight.js.map +0 -1
@@ -1,14 +1,11 @@
1
- import https from 'https';
2
- import crypto from 'crypto';
3
1
  import { Context } from 'vm';
4
- import { IncomingMessage } from 'http';
2
+ import { request } from 'undici';
3
+ import { sleep } from '../utils';
5
4
  import { interval, Subject } from 'rxjs';
6
- import superStringify from 'super-stringify';
7
5
  import { SwitchBotPlatform } from '../platform';
8
6
  import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
9
7
  import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
10
- import { device, devicesConfig, deviceStatus, ad, serviceData, switchbot, HostDomain, DevicePath } from '../settings';
11
- import { sleep } from '../utils';
8
+ import { device, devicesConfig, deviceStatus, ad, serviceData, switchbot, Devices } from '../settings';
12
9
 
13
10
  export class RobotVacuumCleaner {
14
11
  // Services
@@ -130,13 +127,13 @@ export class RobotVacuumCleaner {
130
127
  if (this.On !== this.accessory.context.On) {
131
128
  await this.pushChanges();
132
129
  }
133
- if (this.On && (this.Brightness !== this.accessory.context.Brightness) ) {
130
+ if (this.On && (this.Brightness !== this.accessory.context.Brightness)) {
134
131
  await this.openAPIpushBrightnessChanges();
135
132
  }
136
133
  } catch (e: any) {
137
134
  this.apiError(e);
138
135
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
139
- + ` Error Message: ${superStringify(e.message)}`);
136
+ + ` Error Message: ${JSON.stringify(e.message)}`);
140
137
  }
141
138
  this.robotVacuumCleanerUpdateInProgress = false;
142
139
  });
@@ -155,7 +152,7 @@ export class RobotVacuumCleaner {
155
152
  } else {
156
153
  await this.offlineOff();
157
154
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
158
- + ` ${this.device.connectionType}, parseStatus will not happen.`);
155
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
159
156
  }
160
157
  }
161
158
 
@@ -197,7 +194,7 @@ export class RobotVacuumCleaner {
197
194
  } else {
198
195
  await this.offlineOff();
199
196
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
200
- + ` ${this.device.connectionType}, refreshStatus will not happen.`);
197
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
201
198
  }
202
199
  }
203
200
 
@@ -223,14 +220,14 @@ export class RobotVacuumCleaner {
223
220
  switchbot.onadvertisement = async (ad: any) => {
224
221
  this.address = ad.address;
225
222
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},`
226
- + ` BLE Address Found: ${this.address}`);
223
+ + ` BLE Address Found: ${this.address}`);
227
224
  this.serviceData = ad.serviceData;
228
225
  this.state = ad.serviceData.state;
229
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${superStringify(ad.serviceData)}`);
226
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
230
227
  this.debugLog(
231
228
  `${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
232
- `delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
233
- `wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
229
+ `delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
230
+ `wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
234
231
  );
235
232
 
236
233
  if (this.serviceData) {
@@ -252,7 +249,7 @@ export class RobotVacuumCleaner {
252
249
  .catch(async (e: any) => {
253
250
  this.apiError(e);
254
251
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
255
- + ` Connection, Error Message: ${superStringify(e.message)}`);
252
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
256
253
  await this.BLERefreshConnection(switchbot);
257
254
  });
258
255
  } else {
@@ -263,53 +260,24 @@ export class RobotVacuumCleaner {
263
260
  async openAPIRefreshStatus() {
264
261
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
265
262
  try {
266
- const t = Date.now();
267
- const nonce = 'requestID';
268
- const data = this.platform.config.credentials?.token + t + nonce;
269
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
270
- const sign = signTerm.toString('base64');
271
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
272
- const options = {
273
- hostname: HostDomain,
274
- port: 443,
275
- path: `${DevicePath}/${this.device.deviceId}/status`,
276
- method: 'GET',
277
- headers: {
278
- Authorization: this.platform.config.credentials?.token,
279
- sign: sign,
280
- nonce: nonce,
281
- t: t,
282
- 'Content-Type': 'application/json',
283
- },
284
- };
285
- const req = https.request(options, (res) => {
286
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
287
- this.statusCode({ res });
288
- let rawData = '';
289
- res.on('data', (d) => {
290
- rawData += d;
291
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
292
- });
293
- res.on('end', () => {
294
- try {
295
- this.deviceStatus = JSON.parse(rawData);
296
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${superStringify(this.deviceStatus)}`);
297
- this.power = this.deviceStatus.body.power;
298
- this.openAPIparseStatus();
299
- this.updateHomeKitCharacteristics();
300
- } catch (e: any) {
301
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
302
- }
303
- });
304
- });
305
- req.on('error', (e: any) => {
306
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
263
+ const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/status`, {
264
+ headers: this.platform.generateHeaders(),
307
265
  });
308
- req.end();
266
+ const deviceStatus = await body.json();
267
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
268
+ this.statusCode(statusCode);
269
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
270
+ this.debugLog(
271
+ `${this.device.deviceType}: ${this.accessory.displayName
272
+ } refreshStatus: ${JSON.stringify(deviceStatus)}`,
273
+ );
274
+ this.power = deviceStatus.body.power;
275
+ this.openAPIparseStatus();
276
+ this.updateHomeKitCharacteristics();
309
277
  } catch (e: any) {
310
278
  this.apiError(e);
311
279
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
312
- + ` Connection, Error Message: ${superStringify(e.message)}`);
280
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
313
281
  }
314
282
  }
315
283
 
@@ -332,7 +300,7 @@ export class RobotVacuumCleaner {
332
300
  } else {
333
301
  await this.offlineOff();
334
302
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
335
- + ` ${this.device.connectionType}, pushChanges will not happen.`);
303
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
336
304
  }
337
305
  // Refresh the status from the API
338
306
  interval(15000)
@@ -379,111 +347,70 @@ export class RobotVacuumCleaner {
379
347
  .catch(async (e: any) => {
380
348
  this.apiError(e);
381
349
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
382
- + ` Connection, Error Message: ${superStringify(e.message)}`);
350
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`);
383
351
  await this.BLEPushConnection();
384
352
  });
385
353
  } else {
386
354
  this.debugLog(
387
355
  `${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
388
- +`OnCached: ${this.accessory.context.On}`,
356
+ + `OnCached: ${this.accessory.context.On}`,
389
357
  );
390
358
  }
391
359
  }
392
360
 
393
361
  async openAPIpushChanges() {
394
- try {
395
- // Make Push On request to the API
396
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
397
- const t = Date.now();
398
- const nonce = 'requestID';
399
- const data = this.platform.config.credentials?.token + t + nonce;
400
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
401
- .update(Buffer.from(data, 'utf-8'))
402
- .digest();
403
- const sign = signTerm.toString('base64');
404
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
405
- const body = await this.commands();
362
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
363
+ if (this.On !== this.accessory.context.On) {
364
+ let command = '';
365
+ if (this.On) {
366
+ command = 'turnOn';
367
+ } else {
368
+ command = 'turnOff';
369
+ }
370
+ const body = JSON.stringify({
371
+ 'command': `${command}`,
372
+ 'parameter': 'default',
373
+ 'commandType': 'command',
374
+ });
406
375
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
407
- const options = {
408
- hostname: HostDomain,
409
- port: 443,
410
- path: `${DevicePath}/${this.device.deviceId}/commands`,
411
- method: 'POST',
412
- headers: {
413
- 'Authorization': this.platform.config.credentials?.token,
414
- 'sign': sign,
415
- 'nonce': nonce,
416
- 't': t,
417
- 'Content-Type': 'application/json',
418
- 'Content-Length': body.length,
419
- },
420
- };
421
- const req = https.request(options, res => {
422
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
423
- this.statusCode({ res });
424
- res.on('data', d => {
425
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
376
+ try {
377
+ const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/commands`, {
378
+ method: 'POST',
379
+ headers: this.platform.generateHeaders(),
426
380
  });
427
- });
428
- req.on('error', (e: any) => {
429
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
430
- });
431
- req.write(body);
432
- req.end();
433
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${superStringify(req)}`);
434
- } catch (e: any) {
435
- this.apiError(e);
436
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
437
- + ` Connection, Error Message: ${superStringify(e.message)}`,
438
- );
381
+ const deviceStatus = await body.json();
382
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
383
+ this.statusCode(statusCode);
384
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
385
+ } catch (e: any) {
386
+ this.apiError(e);
387
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
388
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`,
389
+ );
390
+ }
391
+ } else {
392
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
393
+ + `OnCached: ${this.accessory.context.On}`);
439
394
  }
440
395
  }
441
396
 
442
397
  async openAPIpushBrightnessChanges() {
398
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushBrightnessChanges`);
399
+ const body = await this.brightnessCommands();
400
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
443
401
  try {
444
- // Make Push On request to the API
445
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
446
- const t = Date.now();
447
- const nonce = 'requestID';
448
- const data = this.platform.config.credentials?.token + t + nonce;
449
- const signTerm = crypto.createHmac('sha256', this.platform.config.credentials?.secret)
450
- .update(Buffer.from(data, 'utf-8'))
451
- .digest();
452
- const sign = signTerm.toString('base64');
453
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
454
- const body = await this.brightnessCommands();
455
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${body},`);
456
- const options = {
457
- hostname: HostDomain,
458
- port: 443,
459
- path: `${DevicePath}/${this.device.deviceId}/commands`,
402
+ const { body, statusCode, headers } = await request(`${Devices}/${this.device.deviceId}/commands`, {
460
403
  method: 'POST',
461
- headers: {
462
- 'Authorization': this.platform.config.credentials?.token,
463
- 'sign': sign,
464
- 'nonce': nonce,
465
- 't': t,
466
- 'Content-Type': 'application/json',
467
- 'Content-Length': body.length,
468
- },
469
- };
470
- const req = https.request(options, res => {
471
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
472
- this.statusCode({ res });
473
- res.on('data', d => {
474
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
475
- });
476
- });
477
- req.on('error', (e: any) => {
478
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
404
+ headers: this.platform.generateHeaders(),
479
405
  });
480
- req.write(body);
481
- req.end();
482
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${superStringify(req)}`);
406
+ const deviceStatus = await body.json();
407
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Devices: ${JSON.stringify(deviceStatus.body)}`);
408
+ this.statusCode(statusCode);
409
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Headers: ${JSON.stringify(headers)}`);
483
410
  } catch (e: any) {
484
411
  this.apiError(e);
485
412
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
486
- + ` Connection, Error Message: ${superStringify(e.message)}`,
413
+ + ` Connection, Error Message: ${JSON.stringify(e.message)}`,
487
414
  );
488
415
  }
489
416
  }
@@ -498,7 +425,7 @@ export class RobotVacuumCleaner {
498
425
  command = 'dock';
499
426
  parameter = 'default';
500
427
  }
501
- const body = superStringify({
428
+ const body = JSON.stringify({
502
429
  'command': `${command}`,
503
430
  'parameter': `${parameter}`,
504
431
  'commandType': 'command',
@@ -525,7 +452,7 @@ export class RobotVacuumCleaner {
525
452
  command = 'dock';
526
453
  parameter = 'default';
527
454
  }
528
- const body = superStringify({
455
+ const body = JSON.stringify({
529
456
  'command': `${command}`,
530
457
  'parameter': `${parameter}`,
531
458
  'commandType': 'command',
@@ -621,7 +548,7 @@ export class RobotVacuumCleaner {
621
548
  });
622
549
  // Set an event handler
623
550
  switchbot.onadvertisement = (ad: any) => {
624
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${superStringify(ad, null, ' ')}`);
551
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
625
552
  };
626
553
  await sleep(10000);
627
554
  // Stop to monitor
@@ -691,36 +618,41 @@ export class RobotVacuumCleaner {
691
618
  }
692
619
  }
693
620
 
694
- async statusCode({ res }: { res: IncomingMessage }): Promise<void> {
695
- switch (res.statusCode) {
621
+ async statusCode(statusCode: number): Promise<void> {
622
+ switch (statusCode) {
696
623
  case 151:
697
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
624
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
698
625
  break;
699
626
  case 152:
700
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
627
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
701
628
  break;
702
629
  case 160:
703
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
630
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
704
631
  break;
705
632
  case 161:
706
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
707
- await this.offlineOff();
633
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
634
+ this.offlineOff();
708
635
  break;
709
636
  case 171:
710
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline. Hub: ${this.device.hubDeviceId}`);
711
- await this.offlineOff();
637
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. `
638
+ + `Hub: ${this.device.hubDeviceId}`);
639
+ this.offlineOff();
712
640
  break;
713
641
  case 190:
714
642
  this.errorLog(
715
643
  `${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
716
- ` Or command: ${superStringify(res)} format is invalid`,
644
+ ` Or command format is invalid, statusCode: ${statusCode}`,
717
645
  );
718
646
  break;
719
647
  case 100:
720
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
648
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
649
+ break;
650
+ case 200:
651
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
721
652
  break;
722
653
  default:
723
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
654
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: `
655
+ + `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
724
656
  }
725
657
  }
726
658
 
@@ -738,7 +670,7 @@ export class RobotVacuumCleaner {
738
670
  FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
739
671
  let FirmwareRevision: string;
740
672
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
741
- +` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
673
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
742
674
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
743
675
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
744
676
  if (accessory.context.FirmwareRevision) {
@@ -801,7 +733,7 @@ export class RobotVacuumCleaner {
801
733
  config['maxRetry'] = device.maxRetry;
802
734
  }
803
735
  if (Object.entries(config).length !== 0) {
804
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${superStringify(config)}`);
736
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
805
737
  }
806
738
  }
807
739