node-switchbot 1.2.1-beta.9 → 1.4.1

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.
@@ -1,9 +1,6 @@
1
- 'use strict';
2
- const SwitchbotDevice = require('./switchbot-device.js');
1
+ "use strict";
2
+ const SwitchbotDevice = require("./switchbot-device.js");
3
3
 
4
- class SwitchbotDeviceWoContact extends SwitchbotDevice {
4
+ class SwitchbotDeviceWoContact extends SwitchbotDevice {}
5
5
 
6
-
7
- }
8
-
9
- module.exports = SwitchbotDeviceWoContact;
6
+ module.exports = SwitchbotDeviceWoContact;
@@ -1,102 +1,117 @@
1
- 'use strict';
2
- const SwitchbotDevice = require('./switchbot-device.js');
1
+ "use strict";
2
+ const SwitchbotDevice = require("./switchbot-device.js");
3
3
 
4
4
  class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
5
+ /* ------------------------------------------------------------------
6
+ * open()
7
+ * - Open the curtain
8
+ *
9
+ * [Arguments]
10
+ * - none
11
+ *
12
+ * [Return value]
13
+ * - Promise object
14
+ * Nothing will be passed to the `resolve()`.
15
+ * ---------------------------------------------------------------- */
16
+ open() {
17
+ return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x00]);
18
+ }
5
19
 
6
- /* ------------------------------------------------------------------
7
- * open()
8
- * - Open the curtain
9
- *
10
- * [Arguments]
11
- * - none
12
- *
13
- * [Return value]
14
- * - Promise object
15
- * Nothing will be passed to the `resolve()`.
16
- * ---------------------------------------------------------------- */
17
- open() {
18
- return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x00]);
19
- }
20
-
21
- /* ------------------------------------------------------------------
22
- * close()
23
- * - close the curtain
24
- *
25
- * [Arguments]
26
- * - none
27
- *
28
- * [Return value]
29
- * - Promise object
30
- * Nothing will be passed to the `resolve()`.
31
- * ---------------------------------------------------------------- */
32
- close() {
33
- return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x64]);
34
- }
35
-
36
- /* ------------------------------------------------------------------
37
- * pause()
38
- * - pause the curtain
39
- *
40
- * [Arguments]
41
- * - none
42
- *
43
- * [Return value]
44
- * - Promise object
45
- * Nothing will be passed to the `resolve()`.
46
- * ---------------------------------------------------------------- */
47
- pause() {
48
- return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x00, 0xff]);
49
- }
20
+ /* ------------------------------------------------------------------
21
+ * close()
22
+ * - close the curtain
23
+ *
24
+ * [Arguments]
25
+ * - none
26
+ *
27
+ * [Return value]
28
+ * - Promise object
29
+ * Nothing will be passed to the `resolve()`.
30
+ * ---------------------------------------------------------------- */
31
+ close() {
32
+ return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x64]);
33
+ }
50
34
 
51
- /* ------------------------------------------------------------------
52
- * runToPos()
53
- * - run to the targe position
54
- *
55
- * [Arguments]
56
- * - percent | number | Required | the percentage of target position
57
- *
58
- * [Return value]
59
- * - Promise object
60
- * Nothing will be passed to the `resolve()`.
61
- * ---------------------------------------------------------------- */
62
- runToPos(percent, mode) {
35
+ /* ------------------------------------------------------------------
36
+ * pause()
37
+ * - pause the curtain
38
+ *
39
+ * [Arguments]
40
+ * - none
41
+ *
42
+ * [Return value]
43
+ * - Promise object
44
+ * Nothing will be passed to the `resolve()`.
45
+ * ---------------------------------------------------------------- */
46
+ pause() {
47
+ return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x00, 0xff]);
48
+ }
63
49
 
64
- if (typeof percent != 'number') {
65
- return new Promise((resolve, reject) => {
66
- reject(new Error('The type of target position percentage is incorrent: ' + typeof percent));
67
- });
68
- }
69
- if (mode == null) {
70
- mode = 0xff;
71
- }
72
- else {
73
- if (typeof mode != 'number') {
74
- return new Promise((resolve, reject) => {
75
- reject(new Error('The type of running mode is incorrent: ' + typeof mode));
76
- });
77
- }
78
- if (mode > 1) { mode = 0xff; }
79
- }
80
- if (percent > 100) { percent = 100; }
81
- else if (percent < 0) { percent = 0; }
82
- return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, mode, percent]);
50
+ /* ------------------------------------------------------------------
51
+ * runToPos()
52
+ * - run to the targe position
53
+ *
54
+ * [Arguments]
55
+ * - percent | number | Required | the percentage of target position
56
+ *
57
+ * [Return value]
58
+ * - Promise object
59
+ * Nothing will be passed to the `resolve()`.
60
+ * ---------------------------------------------------------------- */
61
+ runToPos(percent, mode) {
62
+ if (typeof percent != "number") {
63
+ return new Promise((resolve, reject) => {
64
+ reject(
65
+ new Error(
66
+ "The type of target position percentage is incorrent: " +
67
+ typeof percent
68
+ )
69
+ );
70
+ });
83
71
  }
84
-
85
- _operateCurtain(bytes) {
72
+ if (mode == null) {
73
+ mode = 0xff;
74
+ } else {
75
+ if (typeof mode != "number") {
86
76
  return new Promise((resolve, reject) => {
87
- let req_buf = Buffer.from(bytes);
88
- this._command(req_buf).then((res_buf) => {
89
- let code = res_buf.readUInt8(0);
90
- if (res_buf.length === 3 && code === 0x01) {
91
- resolve();
92
- } else {
93
- reject(new Error('The device returned an error: 0x' + res_buf.toString('hex')));
94
- }
95
- }).catch((error) => {
96
- reject(error);
97
- });
77
+ reject(
78
+ new Error("The type of running mode is incorrent: " + typeof mode)
79
+ );
98
80
  });
81
+ }
82
+ if (mode > 1) {
83
+ mode = 0xff;
84
+ }
85
+ }
86
+ if (percent > 100) {
87
+ percent = 100;
88
+ } else if (percent < 0) {
89
+ percent = 0;
99
90
  }
91
+ return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, mode, percent]);
92
+ }
93
+
94
+ _operateCurtain(bytes) {
95
+ return new Promise((resolve, reject) => {
96
+ let req_buf = Buffer.from(bytes);
97
+ this._command(req_buf)
98
+ .then((res_buf) => {
99
+ let code = res_buf.readUInt8(0);
100
+ if (res_buf.length === 3 && code === 0x01) {
101
+ resolve();
102
+ } else {
103
+ reject(
104
+ new Error(
105
+ "The device returned an error: 0x" + res_buf.toString("hex")
106
+ )
107
+ );
108
+ }
109
+ })
110
+ .catch((error) => {
111
+ reject(error);
112
+ });
113
+ });
114
+ }
100
115
  }
101
116
 
102
- module.exports = SwitchbotDeviceWoCurtain;
117
+ module.exports = SwitchbotDeviceWoCurtain;
@@ -1,79 +1,78 @@
1
- 'use strict';
2
- const SwitchbotDevice = require('./switchbot-device.js');
1
+ "use strict";
2
+ const SwitchbotDevice = require("./switchbot-device.js");
3
3
 
4
4
  class SwitchbotDeviceWoHand extends SwitchbotDevice {
5
-
6
5
  /* ------------------------------------------------------------------
7
- * press()
8
- * - Press
9
- *
10
- * [Arguments]
11
- * - none
12
- *
13
- * [Return value]
14
- * - Promise object
15
- * Nothing will be passed to the `resolve()`.
16
- * ---------------------------------------------------------------- */
6
+ * press()
7
+ * - Press
8
+ *
9
+ * [Arguments]
10
+ * - none
11
+ *
12
+ * [Return value]
13
+ * - Promise object
14
+ * Nothing will be passed to the `resolve()`.
15
+ * ---------------------------------------------------------------- */
17
16
  press() {
18
17
  return this._operateBot([0x57, 0x01, 0x00]);
19
18
  }
20
19
 
21
20
  /* ------------------------------------------------------------------
22
- * turnOn()
23
- * - Turn on
24
- *
25
- * [Arguments]
26
- * - none
27
- *
28
- * [Return value]
29
- * - Promise object
30
- * Nothing will be passed to the `resolve()`.
31
- * ---------------------------------------------------------------- */
21
+ * turnOn()
22
+ * - Turn on
23
+ *
24
+ * [Arguments]
25
+ * - none
26
+ *
27
+ * [Return value]
28
+ * - Promise object
29
+ * Nothing will be passed to the `resolve()`.
30
+ * ---------------------------------------------------------------- */
32
31
  turnOn() {
33
32
  return this._operateBot([0x57, 0x01, 0x01]);
34
33
  }
35
34
 
36
35
  /* ------------------------------------------------------------------
37
- * turnOff()
38
- * - Turn off
39
- *
40
- * [Arguments]
41
- * - none
42
- *
43
- * [Return value]
44
- * - Promise object
45
- * Nothing will be passed to the `resolve()`.
46
- * ---------------------------------------------------------------- */
36
+ * turnOff()
37
+ * - Turn off
38
+ *
39
+ * [Arguments]
40
+ * - none
41
+ *
42
+ * [Return value]
43
+ * - Promise object
44
+ * Nothing will be passed to the `resolve()`.
45
+ * ---------------------------------------------------------------- */
47
46
  turnOff() {
48
47
  return this._operateBot([0x57, 0x01, 0x02]);
49
48
  }
50
49
 
51
50
  /* ------------------------------------------------------------------
52
- * down()
53
- * - Down
54
- *
55
- * [Arguments]
56
- * - none
57
- *
58
- * [Return value]
59
- * - Promise object
60
- * Nothing will be passed to the `resolve()`.
61
- * ---------------------------------------------------------------- */
51
+ * down()
52
+ * - Down
53
+ *
54
+ * [Arguments]
55
+ * - none
56
+ *
57
+ * [Return value]
58
+ * - Promise object
59
+ * Nothing will be passed to the `resolve()`.
60
+ * ---------------------------------------------------------------- */
62
61
  down() {
63
62
  return this._operateBot([0x57, 0x01, 0x03]);
64
63
  }
65
64
 
66
65
  /* ------------------------------------------------------------------
67
- * up()
68
- * - Up
69
- *
70
- * [Arguments]
71
- * - none
72
- *
73
- * [Return value]
74
- * - Promise object
75
- * Nothing will be passed to the `resolve()`.
76
- * ---------------------------------------------------------------- */
66
+ * up()
67
+ * - Up
68
+ *
69
+ * [Arguments]
70
+ * - none
71
+ *
72
+ * [Return value]
73
+ * - Promise object
74
+ * Nothing will be passed to the `resolve()`.
75
+ * ---------------------------------------------------------------- */
77
76
  up() {
78
77
  return this._operateBot([0x57, 0x01, 0x04]);
79
78
  }
@@ -81,19 +80,24 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {
81
80
  _operateBot(bytes) {
82
81
  return new Promise((resolve, reject) => {
83
82
  let req_buf = Buffer.from(bytes);
84
- this._command(req_buf).then((res_buf) => {
85
- let code = res_buf.readUInt8(0);
86
- if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) {
87
- resolve();
88
- } else {
89
- reject(new Error('The device returned an error: 0x' + res_buf.toString('hex')));
90
- }
91
- }).catch((error) => {
92
- reject(error);
93
- });
83
+ this._command(req_buf)
84
+ .then((res_buf) => {
85
+ let code = res_buf.readUInt8(0);
86
+ if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) {
87
+ resolve();
88
+ } else {
89
+ reject(
90
+ new Error(
91
+ "The device returned an error: 0x" + res_buf.toString("hex")
92
+ )
93
+ );
94
+ }
95
+ })
96
+ .catch((error) => {
97
+ reject(error);
98
+ });
94
99
  });
95
100
  }
96
-
97
101
  }
98
102
 
99
- module.exports = SwitchbotDeviceWoHand;
103
+ module.exports = SwitchbotDeviceWoHand;
@@ -1,79 +1,78 @@
1
- 'use strict';
2
- const SwitchbotDevice = require('./switchbot-device.js');
1
+ "use strict";
2
+ const SwitchbotDevice = require("./switchbot-device.js");
3
3
 
4
4
  class SwitchbotDeviceWoHumi extends SwitchbotDevice {
5
-
6
5
  /* ------------------------------------------------------------------
7
- * press()
8
- * - Press
9
- *
10
- * [Arguments]
11
- * - none
12
- *
13
- * [Return value]
14
- * - Promise object
15
- * Nothing will be passed to the `resolve()`.
16
- * ---------------------------------------------------------------- */
6
+ * press()
7
+ * - Press
8
+ *
9
+ * [Arguments]
10
+ * - none
11
+ *
12
+ * [Return value]
13
+ * - Promise object
14
+ * Nothing will be passed to the `resolve()`.
15
+ * ---------------------------------------------------------------- */
17
16
  press() {
18
17
  return this._operateBot([0x57, 0x01, 0x00]);
19
18
  }
20
19
 
21
20
  /* ------------------------------------------------------------------
22
- * turnOn()
23
- * - Turn on
24
- *
25
- * [Arguments]
26
- * - none
27
- *
28
- * [Return value]
29
- * - Promise object
30
- * Nothing will be passed to the `resolve()`.
31
- * ---------------------------------------------------------------- */
21
+ * turnOn()
22
+ * - Turn on
23
+ *
24
+ * [Arguments]
25
+ * - none
26
+ *
27
+ * [Return value]
28
+ * - Promise object
29
+ * Nothing will be passed to the `resolve()`.
30
+ * ---------------------------------------------------------------- */
32
31
  turnOn() {
33
32
  return this._operateBot([0x57, 0x01, 0x01]);
34
33
  }
35
34
 
36
35
  /* ------------------------------------------------------------------
37
- * turnOff()
38
- * - Turn off
39
- *
40
- * [Arguments]
41
- * - none
42
- *
43
- * [Return value]
44
- * - Promise object
45
- * Nothing will be passed to the `resolve()`.
46
- * ---------------------------------------------------------------- */
36
+ * turnOff()
37
+ * - Turn off
38
+ *
39
+ * [Arguments]
40
+ * - none
41
+ *
42
+ * [Return value]
43
+ * - Promise object
44
+ * Nothing will be passed to the `resolve()`.
45
+ * ---------------------------------------------------------------- */
47
46
  turnOff() {
48
47
  return this._operateBot([0x57, 0x01, 0x02]);
49
48
  }
50
49
 
51
50
  /* ------------------------------------------------------------------
52
- * down()
53
- * - Down
54
- *
55
- * [Arguments]
56
- * - none
57
- *
58
- * [Return value]
59
- * - Promise object
60
- * Nothing will be passed to the `resolve()`.
61
- * ---------------------------------------------------------------- */
51
+ * down()
52
+ * - Down
53
+ *
54
+ * [Arguments]
55
+ * - none
56
+ *
57
+ * [Return value]
58
+ * - Promise object
59
+ * Nothing will be passed to the `resolve()`.
60
+ * ---------------------------------------------------------------- */
62
61
  down() {
63
62
  return this._operateBot([0x57, 0x01, 0x03]);
64
63
  }
65
64
 
66
65
  /* ------------------------------------------------------------------
67
- * up()
68
- * - Up
69
- *
70
- * [Arguments]
71
- * - none
72
- *
73
- * [Return value]
74
- * - Promise object
75
- * Nothing will be passed to the `resolve()`.
76
- * ---------------------------------------------------------------- */
66
+ * up()
67
+ * - Up
68
+ *
69
+ * [Arguments]
70
+ * - none
71
+ *
72
+ * [Return value]
73
+ * - Promise object
74
+ * Nothing will be passed to the `resolve()`.
75
+ * ---------------------------------------------------------------- */
77
76
  up() {
78
77
  return this._operateBot([0x57, 0x01, 0x04]);
79
78
  }
@@ -81,19 +80,24 @@ class SwitchbotDeviceWoHumi extends SwitchbotDevice {
81
80
  _operateBot(bytes) {
82
81
  return new Promise((resolve, reject) => {
83
82
  let req_buf = Buffer.from(bytes);
84
- this._command(req_buf).then((res_buf) => {
85
- let code = res_buf.readUInt8(0);
86
- if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) {
87
- resolve();
88
- } else {
89
- reject(new Error('The device returned an error: 0x' + res_buf.toString('hex')));
90
- }
91
- }).catch((error) => {
92
- reject(error);
93
- });
83
+ this._command(req_buf)
84
+ .then((res_buf) => {
85
+ let code = res_buf.readUInt8(0);
86
+ if (res_buf.length === 3 && (code === 0x01 || code === 0x05)) {
87
+ resolve();
88
+ } else {
89
+ reject(
90
+ new Error(
91
+ "The device returned an error: 0x" + res_buf.toString("hex")
92
+ )
93
+ );
94
+ }
95
+ })
96
+ .catch((error) => {
97
+ reject(error);
98
+ });
94
99
  });
95
100
  }
96
-
97
101
  }
98
102
 
99
- module.exports = SwitchbotDeviceWoHumi;
103
+ module.exports = SwitchbotDeviceWoHumi;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ const SwitchbotDevice = require("./switchbot-device.js");
3
+
4
+ /**
5
+ * @see https://github.com/OpenWonderLabs/SwitchBotAPI-BLE/blob/latest/devicetypes/plugmini.md
6
+ */
7
+ class SwitchbotDeviceWoPlugMini extends SwitchbotDevice {
8
+ /**
9
+ * @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
10
+ */
11
+ readState() {
12
+ return this._operateBot([0x57, 0x0f, 0x51, 0x01]);
13
+ }
14
+
15
+ /**
16
+ * @private
17
+ */
18
+ _setState(reqByteArray) {
19
+ const base = [0x57, 0x0f, 0x50, 0x01];
20
+ return this._operateBot([].concat(base, reqByteArray));
21
+ }
22
+
23
+ /**
24
+ * @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
25
+ */
26
+ turnOn() {
27
+ return this._setState([0x01, 0x80]);
28
+ }
29
+
30
+ /**
31
+ * @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
32
+ */
33
+ turnOff() {
34
+ return this._setState([0x01, 0x00]);
35
+ }
36
+
37
+ /**
38
+ * @returns {Promise<boolean>} resolves with a boolean that tells whether the plug in ON(true) or OFF(false)
39
+ */
40
+ toggle() {
41
+ return this._setState([0x02, 0x80]);
42
+ }
43
+
44
+ /**
45
+ * @private
46
+ */
47
+ _operateBot(bytes) {
48
+ const req_buf = Buffer.from(bytes);
49
+ return new Promise((resolve, reject) => {
50
+ this._command(req_buf)
51
+ .then((res_bytes) => {
52
+ const res_buf = Buffer.from(res_bytes);
53
+ if (res_buf.length === 2) {
54
+ let code = res_buf.readUInt8(1);
55
+ if (code === 0x00 || code === 0x80) {
56
+ const is_on = code === 0x80;
57
+ resolve(is_on);
58
+ } else {
59
+ reject(
60
+ new Error(
61
+ "The device returned an error: 0x" + res_buf.toString("hex")
62
+ )
63
+ );
64
+ }
65
+ } else {
66
+ reject(
67
+ new Error(
68
+ "Expecting a 2-byte response, got instead: 0x" +
69
+ res_buf.toString("hex")
70
+ )
71
+ );
72
+ }
73
+ })
74
+ .catch((error) => {
75
+ reject(error);
76
+ });
77
+ });
78
+ }
79
+ }
80
+
81
+ module.exports = SwitchbotDeviceWoPlugMini;
@@ -1,9 +1,6 @@
1
- 'use strict';
2
- const SwitchbotDevice = require('./switchbot-device.js');
1
+ "use strict";
2
+ const SwitchbotDevice = require("./switchbot-device.js");
3
3
 
4
- class SwitchbotDeviceWoPresence extends SwitchbotDevice {
4
+ class SwitchbotDeviceWoPresence extends SwitchbotDevice {}
5
5
 
6
-
7
- }
8
-
9
- module.exports = SwitchbotDeviceWoPresence;
6
+ module.exports = SwitchbotDeviceWoPresence;
@@ -1,9 +1,6 @@
1
- 'use strict';
2
- const SwitchbotDevice = require('./switchbot-device.js');
1
+ "use strict";
2
+ const SwitchbotDevice = require("./switchbot-device.js");
3
3
 
4
- class SwitchbotDeviceWoSensorTH extends SwitchbotDevice {
4
+ class SwitchbotDeviceWoSensorTH extends SwitchbotDevice {}
5
5
 
6
-
7
- }
8
-
9
- module.exports = SwitchbotDeviceWoSensorTH;
6
+ module.exports = SwitchbotDeviceWoSensorTH;