node-switchbot 1.1.3-beta.4 → 1.1.3-beta.5

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,7 +1,9 @@
1
+ 'use strict';
2
+
1
3
  class SwitchbotAdvertising {
2
- constructor() { }
4
+ constructor() { }
3
5
 
4
- /* ------------------------------------------------------------------
6
+ /* ------------------------------------------------------------------
5
7
  * parse(peripheral)
6
8
  * - Parse advertising packets coming from switchbot devices
7
9
  *
@@ -58,222 +60,222 @@ class SwitchbotAdvertising {
58
60
  * If the specified `Peripheral` does not represent any switchbot
59
61
  * device, this method will return `null`.
60
62
  * ---------------------------------------------------------------- */
61
- parse(peripheral) {
62
- let ad = peripheral.advertisement;
63
- if (!ad || !ad.serviceData) {
64
- return null;
65
- }
66
- if (ad.serviceData[0].uuid !== "0d00") {
67
- return null;
68
- }
69
- let buf = ad.serviceData[0].data;
70
- if (!buf || !Buffer.isBuffer(buf) || buf.length < 3) {
71
- return null;
72
- }
63
+ parse(peripheral) {
64
+ let ad = peripheral.advertisement;
65
+ if (!ad || !ad.serviceData) {
66
+ return null;
67
+ }
68
+ if (ad.serviceData[0].uuid !== '0d00') {
69
+ return null;
70
+ }
71
+ let buf = ad.serviceData[0].data;
72
+ if (!buf || !Buffer.isBuffer(buf) || buf.length < 3) {
73
+ return null;
74
+ }
73
75
 
74
- let model = buf.slice(0, 1).toString("utf8");
75
- let sd = null;
76
+ let model = buf.slice(0, 1).toString('utf8');
77
+ let sd = null;
76
78
 
77
- if (model === "H") { // WoHand
78
- sd = this._parseServiceDataForWoHand(buf);
79
- } else if (model === "e") { // WoHumi
80
- sd = this._parseServiceDataForWoHumi(buf);
81
- } else if (model === "T") { // WoSensorTH
82
- sd = this._parseServiceDataForWoSensorTH(buf);
83
- } else if (model === "c") { // WoCurtain
84
- sd = this._parseServiceDataForWoCurtain(buf);
85
- } else if (model === "s") { // WoMotion
86
- sd = this._parseServiceDataForWoPresence(buf);
87
- } else if (model === "d") { // WoContact
88
- sd = this._parseServiceDataForWoContact(buf);
89
- } else {
90
- return null;
91
- }
79
+ if (model === 'H') { // WoHand
80
+ sd = this._parseServiceDataForWoHand(buf);
81
+ } else if (model === 'e') { // WoHumi
82
+ sd = this._parseServiceDataForWoHumi(buf);
83
+ } else if (model === 'T') { // WoSensorTH
84
+ sd = this._parseServiceDataForWoSensorTH(buf);
85
+ } else if (model === 'c') { // WoCurtain
86
+ sd = this._parseServiceDataForWoCurtain(buf);
87
+ } else if (model === 's') { // WoMotion
88
+ sd = this._parseServiceDataForWoPresence(buf);
89
+ } else if (model === 'd') { // WoContact
90
+ sd = this._parseServiceDataForWoContact(buf);
91
+ } else {
92
+ return null;
93
+ }
92
94
 
93
- if (!sd) {
94
- return null;
95
- }
96
- let address = peripheral.address || "";
97
- if (address === "") {
98
- address = peripheral.advertisement.manufacturerData || "";
99
- if (address !== "") {
100
- const str = peripheral.advertisement.manufacturerData.toString("hex").slice(4);
101
- address = str.substr(0, 2);
102
- for (var i = 2; i < str.length; i += 2) {
103
- address = address + ":" + str.substr(i, 2);
104
- }
105
- // console.log("address", typeof(address), address);
106
- }
107
- }
108
- else {
109
- address = address.replace(/-/g, ":");
110
- }
111
- let data = {
112
- id: peripheral.id,
113
- address: address,
114
- rssi: peripheral.rssi,
115
- serviceData: sd
116
- };
117
- return data;
118
- }
95
+ if (!sd) {
96
+ return null;
97
+ }
98
+ let address = peripheral.address || '';
99
+ if (address === '') {
100
+ address = peripheral.advertisement.manufacturerData || '';
101
+ if (address !== '') {
102
+ const str = peripheral.advertisement.manufacturerData.toString('hex').slice(4);
103
+ address = str.substr(0, 2);
104
+ for (var i = 2; i < str.length; i += 2) {
105
+ address = address + ":" + str.substr(i, 2);
106
+ }
107
+ // console.log("address", typeof(address), address);
108
+ }
109
+ }
110
+ else {
111
+ address = address.replace(/-/g, ':');
112
+ }
113
+ let data = {
114
+ id: peripheral.id,
115
+ address: address,
116
+ rssi: peripheral.rssi,
117
+ serviceData: sd
118
+ };
119
+ return data;
120
+ }
119
121
 
120
- _parseServiceDataForWoHand(buf) {
121
- if (buf.length !== 3) {
122
- return null;
123
- }
124
- let byte1 = buf.readUInt8(1);
125
- let byte2 = buf.readUInt8(2);
122
+ _parseServiceDataForWoHand(buf) {
123
+ if (buf.length !== 3) {
124
+ return null;
125
+ }
126
+ let byte1 = buf.readUInt8(1);
127
+ let byte2 = buf.readUInt8(2);
126
128
 
127
- let mode = (byte1 & 0b10000000) ? true : false; // Whether the light switch Add-on is used or not
128
- let state = (byte1 & 0b01000000) ? true : false; // Whether the switch status is ON or OFF
129
- let battery = byte2 & 0b01111111; // %
129
+ let mode = (byte1 & 0b10000000) ? true : false; // Whether the light switch Add-on is used or not
130
+ let state = (byte1 & 0b01000000) ? true : false; // Whether the switch status is ON or OFF
131
+ let battery = byte2 & 0b01111111; // %
130
132
 
131
- let data = {
132
- model: "H",
133
- modelName: "WoHand",
134
- mode: mode,
135
- state: state,
136
- battery: battery
137
- };
133
+ let data = {
134
+ model: 'H',
135
+ modelName: 'WoHand',
136
+ mode: mode,
137
+ state: state,
138
+ battery: battery
139
+ };
138
140
 
139
- return data;
140
- }
141
+ return data;
142
+ }
141
143
 
142
- _parseServiceDataForWoHumi(buf) {
143
- if (buf.length !== 8) {
144
- return null;
145
- }
146
- let byte1 = buf.readUInt8(1);
147
- // let byte2 = buf.readUInt8(2);
148
- let byte3 = buf.readUInt8(3);
149
- let byte4 = buf.readUInt8(4);
150
- let byte5 = buf.readUInt8(5);
144
+ _parseServiceDataForWoHumi(buf) {
145
+ if (buf.length !== 8) {
146
+ return null;
147
+ }
148
+ let byte1 = buf.readUInt8(1);
149
+ // let byte2 = buf.readUInt8(2);
150
+ let byte3 = buf.readUInt8(3);
151
+ let byte4 = buf.readUInt8(4);
152
+ let byte5 = buf.readUInt8(5);
151
153
 
152
- let onState = (byte1 & 0b10000000) ? true : false; // 1 - on
153
- let autoMode = (byte4 & 0b10000000) ? true : false; // 1 - auto
154
- let percentage = byte4 & 0b01111111; // 0-100%, 101/102/103 - Quick gear 1/2/3
154
+ let onState = (byte1 & 0b10000000) ? true : false; // 1 - on
155
+ let autoMode = (byte4 & 0b10000000) ? true : false; // 1 - auto
156
+ let percentage = byte4 & 0b01111111; // 0-100%, 101/102/103 - Quick gear 1/2/3
155
157
 
156
- let data = {
157
- model: "e",
158
- modelName: "WoHumi",
159
- onState: onState,
160
- autoMode: autoMode,
161
- percentage: autoMode ? 0 : percentage,
162
- };
158
+ let data = {
159
+ model: 'e',
160
+ modelName: 'WoHumi',
161
+ onState: onState,
162
+ autoMode: autoMode,
163
+ percentage: autoMode ? 0 : percentage,
164
+ };
163
165
 
164
- return data;
165
- }
166
+ return data;
167
+ }
166
168
 
167
- _parseServiceDataForWoSensorTH(buf) {
168
- if (buf.length !== 6) {
169
- return null;
170
- }
171
- let byte2 = buf.readUInt8(2);
172
- let byte3 = buf.readUInt8(3);
173
- let byte4 = buf.readUInt8(4);
174
- let byte5 = buf.readUInt8(5);
169
+ _parseServiceDataForWoSensorTH(buf) {
170
+ if (buf.length !== 6) {
171
+ return null;
172
+ }
173
+ let byte2 = buf.readUInt8(2);
174
+ let byte3 = buf.readUInt8(3);
175
+ let byte4 = buf.readUInt8(4);
176
+ let byte5 = buf.readUInt8(5);
175
177
 
176
- let temp_sign = (byte4 & 0b10000000) ? 1 : -1;
177
- let temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 / 10));
178
- let temp_f = (temp_c * 9 / 5) + 32;
179
- temp_f = Math.round(temp_f * 10) / 10;
178
+ let temp_sign = (byte4 & 0b10000000) ? 1 : -1;
179
+ let temp_c = temp_sign * ((byte4 & 0b01111111) + (byte3 / 10));
180
+ let temp_f = (temp_c * 9 / 5) + 32;
181
+ temp_f = Math.round(temp_f * 10) / 10;
180
182
 
181
- let data = {
182
- model: "T",
183
- modelName: "WoSensorTH",
184
- temperature: {
185
- c: temp_c,
186
- f: temp_f
187
- },
188
- fahrenheit: (byte5 & 0b10000000) ? true : false,
189
- humidity: byte5 & 0b01111111,
190
- battery: (byte2 & 0b01111111)
191
- };
183
+ let data = {
184
+ model: 'T',
185
+ modelName: 'WoSensorTH',
186
+ temperature: {
187
+ c: temp_c,
188
+ f: temp_f
189
+ },
190
+ fahrenheit: (byte5 & 0b10000000) ? true : false,
191
+ humidity: byte5 & 0b01111111,
192
+ battery: (byte2 & 0b01111111)
193
+ };
192
194
 
193
- return data;
194
- }
195
+ return data;
196
+ }
195
197
 
196
- _parseServiceDataForWoPresence(buf) {
197
- if (buf.length !== 6) {
198
- return null;
199
- }
200
- let byte1 = buf.readUInt8(1);
201
- let byte2 = buf.readUInt8(2);
202
- // let byte3 = buf.readUInt8(3);
203
- // let byte4 = buf.readUInt8(4);
204
- let byte5 = buf.readUInt8(5);
198
+ _parseServiceDataForWoPresence(buf) {
199
+ if (buf.length !== 6) {
200
+ return null;
201
+ }
202
+ let byte1 = buf.readUInt8(1);
203
+ let byte2 = buf.readUInt8(2);
204
+ // let byte3 = buf.readUInt8(3);
205
+ // let byte4 = buf.readUInt8(4);
206
+ let byte5 = buf.readUInt8(5);
205
207
 
206
- let pirState = (byte1 & 0b01000000) ? true : false; // 1 - Movement detected
207
- let battery = byte2 & 0b01111111; // %
208
- let lightLevel = byte5 & 0b00000011;
208
+ let pirState = (byte1 & 0b01000000) ? true : false; // 1 - Movement detected
209
+ let battery = byte2 & 0b01111111; // %
210
+ let lightLevel = byte5 & 0b00000011;
209
211
 
210
- let data = {
211
- model: "s",
212
- modelName: "WoMotion",
213
- movement: pirState,
214
- battery: battery,
215
- lightLevel: (lightLevel == 1) ? "dark" : ((lightLevel == 2) ? "bright" : "unknown"),
216
- };
212
+ let data = {
213
+ model: 's',
214
+ modelName: 'WoMotion',
215
+ movement: pirState,
216
+ battery: battery,
217
+ lightLevel: (lightLevel == 1) ? 'dark' : ((lightLevel == 2) ? 'bright' : 'unknown'),
218
+ };
217
219
 
218
- return data;
219
- }
220
+ return data;
221
+ }
220
222
 
221
- _parseServiceDataForWoContact(buf) {
222
- if (buf.length !== 9) {
223
- return null;
224
- }
223
+ _parseServiceDataForWoContact(buf) {
224
+ if (buf.length !== 9) {
225
+ return null;
226
+ }
225
227
 
226
- let byte1 = buf.readUInt8(1);
227
- let byte2 = buf.readUInt8(2);
228
- let byte3 = buf.readUInt8(3);
229
- // let byte4 = buf.readUInt8(4);
230
- // let byte5 = buf.readUInt8(5);
231
- // let byte6 = buf.readUInt8(6);
232
- // let byte7 = buf.readUInt8(7);
233
- // let byte8 = buf.readUInt8(8);
228
+ let byte1 = buf.readUInt8(1);
229
+ let byte2 = buf.readUInt8(2);
230
+ let byte3 = buf.readUInt8(3);
231
+ // let byte4 = buf.readUInt8(4);
232
+ // let byte5 = buf.readUInt8(5);
233
+ // let byte6 = buf.readUInt8(6);
234
+ // let byte7 = buf.readUInt8(7);
235
+ // let byte8 = buf.readUInt8(8);
234
236
 
235
- let pirState = (byte1 & 0b01000000) ? true : false; // 1 - Movement detected
236
- let battery = byte2 & 0b01111111; // %
237
- let hallState = (byte3 >> 1) & 0b00000011;
238
- let lightLevel = byte3 & 0b00000001;
237
+ let pirState = (byte1 & 0b01000000) ? true : false; // 1 - Movement detected
238
+ let battery = byte2 & 0b01111111; // %
239
+ let hallState = (byte3 >> 1) & 0b00000011;
240
+ let lightLevel = byte3 & 0b00000001;
239
241
 
240
- let data = {
241
- model: "d",
242
- modelName: "WoContact",
243
- movement: pirState,
244
- battery: battery,
245
- doorState: (hallState == 0) ? "close" : ((hallState == 1) ? "open" : "timeout no closed"),
246
- lightLevel: (lightLevel == 0) ? "dark" : "bright",
247
- };
242
+ let data = {
243
+ model: 'd',
244
+ modelName: 'WoContact',
245
+ movement: pirState,
246
+ battery: battery,
247
+ doorState: (hallState == 0) ? 'close' : ((hallState == 1) ? 'open' : 'timeout no closed'),
248
+ lightLevel: (lightLevel == 0) ? 'dark' : 'bright',
249
+ };
248
250
 
249
- return data;
250
- }
251
+ return data;
252
+ }
251
253
 
252
- _parseServiceDataForWoCurtain(buf) {
253
- if (buf.length !== 5) {
254
- return null;
255
- }
256
- let byte1 = buf.readUInt8(1);
257
- let byte2 = buf.readUInt8(2);
258
- let byte3 = buf.readUInt8(3);
259
- let byte4 = buf.readUInt8(4);
254
+ _parseServiceDataForWoCurtain(buf) {
255
+ if (buf.length !== 5) {
256
+ return null;
257
+ }
258
+ let byte1 = buf.readUInt8(1);
259
+ let byte2 = buf.readUInt8(2);
260
+ let byte3 = buf.readUInt8(3);
261
+ let byte4 = buf.readUInt8(4);
260
262
 
261
- let calibration = (byte1 & 0b01000000) ? true : false; // Whether the calibration is completed
262
- let battery = byte2 & 0b01111111; // %
263
- let currPosition = byte3 & 0b01111111; // current positon %
264
- let lightLevel = (byte4 >> 4) & 0b00001111; // light sensor level (1-10)
263
+ let calibration = (byte1 & 0b01000000) ? true : false; // Whether the calibration is completed
264
+ let battery = byte2 & 0b01111111; // %
265
+ let currPosition = byte3 & 0b01111111; // current positon %
266
+ let lightLevel = (byte4 >> 4) & 0b00001111; // light sensor level (1-10)
265
267
 
266
- let data = {
267
- model: "c",
268
- modelName: "WoCurtain",
269
- calibration: calibration,
270
- battery: battery,
271
- position: currPosition,
272
- lightLevel: lightLevel
273
- };
268
+ let data = {
269
+ model: 'c',
270
+ modelName: 'WoCurtain',
271
+ calibration: calibration,
272
+ battery: battery,
273
+ position: currPosition,
274
+ lightLevel: lightLevel
275
+ };
274
276
 
275
- return data;
276
- }
277
+ return data;
278
+ }
277
279
  }
278
280
 
279
281
  module.exports = new SwitchbotAdvertising();
@@ -1,8 +1,9 @@
1
- import SwitchbotDevice from "./switchbot-device.js";
1
+ 'use strict';
2
+ const SwitchbotDevice = require('./switchbot-device.js');
2
3
 
3
4
  class SwitchbotDeviceWoContact extends SwitchbotDevice {
4
5
 
5
6
 
6
7
  }
7
8
 
8
- export default SwitchbotDeviceWoContact;
9
+ module.exports = SwitchbotDeviceWoContact;
@@ -1,8 +1,9 @@
1
- import SwitchbotDevice from "./switchbot-device.js";
1
+ 'use strict';
2
+ const SwitchbotDevice = require('./switchbot-device.js');
2
3
 
3
4
  class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
4
5
 
5
- /* ------------------------------------------------------------------
6
+ /* ------------------------------------------------------------------
6
7
  * open()
7
8
  * - Open the curtain
8
9
  *
@@ -13,11 +14,11 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
13
14
  * - Promise object
14
15
  * Nothing will be passed to the `resolve()`.
15
16
  * ---------------------------------------------------------------- */
16
- open() {
17
- return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x00]);
18
- }
17
+ open() {
18
+ return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x00]);
19
+ }
19
20
 
20
- /* ------------------------------------------------------------------
21
+ /* ------------------------------------------------------------------
21
22
  * close()
22
23
  * - close the curtain
23
24
  *
@@ -28,11 +29,11 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
28
29
  * - Promise object
29
30
  * Nothing will be passed to the `resolve()`.
30
31
  * ---------------------------------------------------------------- */
31
- close() {
32
- return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x64]);
33
- }
32
+ close() {
33
+ return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, 0xff, 0x64]);
34
+ }
34
35
 
35
- /* ------------------------------------------------------------------
36
+ /* ------------------------------------------------------------------
36
37
  * pause()
37
38
  * - pause the curtain
38
39
  *
@@ -43,11 +44,11 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
43
44
  * - Promise object
44
45
  * Nothing will be passed to the `resolve()`.
45
46
  * ---------------------------------------------------------------- */
46
- pause() {
47
- return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x00, 0xff]);
48
- }
47
+ pause() {
48
+ return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x00, 0xff]);
49
+ }
49
50
 
50
- /* ------------------------------------------------------------------
51
+ /* ------------------------------------------------------------------
51
52
  * runToPos()
52
53
  * - run to the targe position
53
54
  *
@@ -58,44 +59,44 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
58
59
  * - Promise object
59
60
  * Nothing will be passed to the `resolve()`.
60
61
  * ---------------------------------------------------------------- */
61
- runToPos(percent, mode) {
62
+ runToPos(percent, mode) {
62
63
 
63
- if (typeof percent != "number") {
64
- return new Promise((resolve, reject) => {
65
- reject(new Error("The type of target position percentage is incorrent: " + typeof percent));
66
- });
67
- }
68
- if (mode == null) {
69
- mode = 0xff;
70
- }
71
- else {
72
- if (typeof mode != "number") {
73
- return new Promise((resolve, reject) => {
74
- reject(new Error("The type of running mode is incorrent: " + typeof mode));
75
- });
76
- }
77
- if (mode > 1) { mode = 0xff; }
78
- }
79
- if (percent > 100) { percent = 100; }
80
- else if (percent < 0) { percent = 0; }
81
- return this._operateCurtain([0x57, 0x0f, 0x45, 0x01, 0x05, mode, percent]);
82
- }
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]);
83
+ }
83
84
 
84
- _operateCurtain(bytes) {
85
- return new Promise((resolve, reject) => {
86
- let req_buf = Buffer.from(bytes);
87
- this._command(req_buf).then((res_buf) => {
88
- let code = res_buf.readUInt8(0);
89
- if (res_buf.length === 3 && code === 0x01) {
90
- resolve();
91
- } else {
92
- reject(new Error("The device returned an error: 0x" + res_buf.toString("hex")));
93
- }
94
- }).catch((error) => {
95
- reject(error);
96
- });
97
- });
98
- }
85
+ _operateCurtain(bytes) {
86
+ 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
+ });
98
+ });
99
+ }
99
100
  }
100
101
 
101
- export default SwitchbotDeviceWoCurtain;
102
+ module.exports = SwitchbotDeviceWoCurtain;