Skip to content

Commit 3155d19

Browse files
committed
Fixes
Fixes from: https://github.com/zizi4n5/node-switchbot fix/not_work_after_switching_bluetooth_on_off Fixed would not work properly after switching Bluetooth off and on.
1 parent 6427e72 commit 3155d19

File tree

10 files changed

+1151
-105
lines changed

10 files changed

+1151
-105
lines changed

.github/workflows/nodejs-beta.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- uses: actions/[email protected]
16-
- uses: actions/setup-node@v2.1.4
16+
- uses: actions/setup-node@v2.3.0
1717
with:
1818
node-version: 14
1919
- name: npm install
@@ -31,7 +31,7 @@ jobs:
3131

3232
steps:
3333
- uses: actions/[email protected]
34-
- uses: actions/setup-node@v2.1.4
34+
- uses: actions/setup-node@v2.3.0
3535
with:
3636
node-version: 14
3737
registry-url: https://registry.npmjs.org/

.github/workflows/nodejs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
steps:
2020
- uses: actions/[email protected]
2121
- name: Use Node.js ${{ matrix.node-version }}
22-
uses: actions/setup-node@v2.1.4
22+
uses: actions/setup-node@v2.3.0
2323
with:
2424
node-version: ${{ matrix.node-version }}
2525
- name: npm install
@@ -38,7 +38,7 @@ jobs:
3838

3939
steps:
4040
- uses: actions/[email protected]
41-
- uses: actions/setup-node@v2.1.4
41+
- uses: actions/setup-node@v2.3.0
4242
with:
4343
node-version: 14 # use the minimum required version
4444
registry-url: https://registry.npmjs.org/

.github/workflows/stale.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
env:
1313
ACTIONS_STEP_DEBUG: true
1414
steps:
15-
- uses: actions/stale@v3.0.17
15+
- uses: actions/stale@v4
1616
with:
1717
repo-token: ${{ secrets.GITHUB_TOKEN }}
1818
stale-issue-message: 'This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.'

lib/switchbot-device-wocurtain.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Date: 2020-10-26
77
* ---------------------------------------------------------------- */
88
'use strict';
9-
const SwitchbotDevice = require('./switchbot-device.js');
9+
import SwitchbotDevice from './switchbot-device.js';
1010

1111
class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
1212

@@ -106,4 +106,4 @@ class SwitchbotDeviceWoCurtain extends SwitchbotDevice {
106106
}
107107
}
108108

109-
module.exports = SwitchbotDeviceWoCurtain;
109+
export default SwitchbotDeviceWoCurtain;

lib/switchbot-device-wohand.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Date: 2020-02-10
77
* ---------------------------------------------------------------- */
88
'use strict';
9-
const SwitchbotDevice = require('./switchbot-device.js');
9+
import SwitchbotDevice from './switchbot-device.js';
1010

1111
class SwitchbotDeviceWoHand extends SwitchbotDevice {
1212

@@ -103,4 +103,4 @@ class SwitchbotDeviceWoHand extends SwitchbotDevice {
103103

104104
}
105105

106-
module.exports = SwitchbotDeviceWoHand;
106+
export default SwitchbotDeviceWoHand;

lib/switchbot-device-wosensorth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* Date: 2019-11-16
77
* ---------------------------------------------------------------- */
88
'use strict';
9-
const SwitchbotDevice = require('./switchbot-device.js');
9+
import SwitchbotDevice from './switchbot-device.js';
1010

1111
class SwitchbotDeviceWoSensorTH extends SwitchbotDevice {
1212

1313

1414
}
1515

16-
module.exports = SwitchbotDeviceWoSensorTH;
16+
export default SwitchbotDeviceWoSensorTH;

lib/switchbot-device.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* Date: 2020-02-19
77
* ---------------------------------------------------------------- */
88
'use strict';
9-
const parameterChecker = require('./parameter-checker.js');
10-
const switchbotAdvertising = require('./switchbot-advertising.js');
9+
import { check, error as _error } from './parameter-checker.js';
10+
import { parse } from './switchbot-advertising.js';
1111

1212
class SwitchbotDevice {
1313
/* ------------------------------------------------------------------
@@ -17,8 +17,9 @@ class SwitchbotDevice {
1717
* - peripheral | Object | Required | The `peripheral` object of noble,
1818
* | | | which represents this device
1919
* ---------------------------------------------------------------- */
20-
constructor(peripheral) {
20+
constructor(peripheral, noble) {
2121
this._peripheral = peripheral;
22+
this._noble = noble;
2223
this._chars = null;
2324

2425
this._SERV_UUID_PRIMARY = 'cba20d00224d11e69fb80002a5d5c51b';
@@ -31,7 +32,7 @@ class SwitchbotDevice {
3132
this._COMMAND_TIMEOUT_MSEC = 3000;
3233

3334
// Save the device information
34-
let ad = switchbotAdvertising.parse(peripheral);
35+
let ad = parse(peripheral);
3536
this._id = ad.id;
3637
this._address = ad.address;
3738
this._model = ad.serviceData.model;
@@ -99,6 +100,12 @@ class SwitchbotDevice {
99100

100101
_connect() {
101102
return new Promise((resolve, reject) => {
103+
// Check the bluetooth state
104+
if (this._noble.state !== 'poweredOn') {
105+
reject(new Error('The Bluetooth status is ' + this._noble.state + ', not poweredOn.'));
106+
return;
107+
}
108+
102109
// Check the connection state
103110
let state = this.connectionState;
104111
if (state === 'connected') {
@@ -367,12 +374,12 @@ class SwitchbotDevice {
367374
setDeviceName(name) {
368375
return new Promise((resolve, reject) => {
369376
// Check the parameters
370-
let valid = parameterChecker.check({ name: name }, {
377+
let valid = check({ name: name }, {
371378
name: { required: true, type: 'string', minBytes: 1, maxBytes: 100 }
372379
});
373380

374381
if (!valid) {
375-
reject(new Error(parameterChecker.error.message));
382+
reject(new Error(_error.message));
376383
return;
377384
}
378385

@@ -487,4 +494,4 @@ class SwitchbotDevice {
487494

488495
}
489496

490-
module.exports = SwitchbotDevice;
497+
export default SwitchbotDevice;

lib/switchbot.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* Date: 2019-11-18
77
* ---------------------------------------------------------------- */
88
'use strict';
9-
const parameterChecker = require('./parameter-checker.js');
10-
const switchbotAdvertising = require('./switchbot-advertising.js');
9+
import { check, error as _error } from './parameter-checker.js';
10+
import { parse } from './switchbot-advertising.js';
1111

12-
const SwitchbotDevice = require('./switchbot-device.js');
13-
const SwitchbotDeviceWoHand = require('./switchbot-device-wohand.js');
14-
const SwitchbotDeviceWoSensorTH = require('./switchbot-device-wosensorth.js');
15-
const SwitchbotDeviceWoCurtain = require('./switchbot-device-wocurtain.js');
12+
import SwitchbotDevice from './switchbot-device.js';
13+
import SwitchbotDeviceWoHand from './switchbot-device-wohand.js';
14+
import SwitchbotDeviceWoSensorTH from './switchbot-device-wosensorth.js';
15+
import SwitchbotDeviceWoCurtain from './switchbot-device-wocurtain.js';
1616

1717
class Switchbot {
1818
/* ------------------------------------------------------------------
@@ -79,15 +79,15 @@ class Switchbot {
7979
discover(params = {}) {
8080
let promise = new Promise((resolve, reject) => {
8181
// Check the parameters
82-
let valid = parameterChecker.check(params, {
82+
let valid = check(params, {
8383
duration: { required: false, type: 'integer', min: 1, max: 60000 },
8484
model: { required: false, type: 'string', enum: ['H', 'T', 'c'] },
8585
id: { required: false, type: 'string', min: 12, max: 17 },
8686
quick: { required: false, type: 'boolean' }
8787
}, false);
8888

8989
if (!valid) {
90-
reject(new Error(parameterChecker.error.message));
90+
reject(new Error(_error.message));
9191
return;
9292
}
9393

@@ -158,30 +158,30 @@ class Switchbot {
158158

159159
_init() {
160160
let promise = new Promise((resolve, reject) => {
161-
if (this._initialized === true) {
162-
resolve();
163-
return;
164-
}
165-
if (this.noble.state === 'poweredOn') {
166-
this._initialized = true;
167-
resolve();
168-
} else {
169-
this.noble.once('stateChange', (state) => {
170-
if (state === 'poweredOn') {
171-
this._initialized = true;
172-
resolve();
173-
} else {
174-
let err = new Error('Failed to initialize the Noble object: ' + state);
175-
reject(err);
176-
}
177-
});
161+
switch (this.noble.state) {
162+
case 'poweredOn':
163+
resolve();
164+
return;
165+
case 'unsupported', 'unauthorized', 'poweredOff':
166+
let err = new Error('Failed to initialize the Noble object: ' + this.noble.state);
167+
reject(err);
168+
return;
169+
default: // 'resetting', 'unknown'
170+
this.noble.once('stateChange', (state) => {
171+
if (state === 'poweredOn') {
172+
resolve();
173+
} else {
174+
let err = new Error('Failed to initialize the Noble object: ' + state);
175+
reject(err);
176+
}
177+
});
178178
}
179179
});
180180
return promise;
181181
}
182182

183183
_getDeviceObject(peripheral, id, model) {
184-
let ad = switchbotAdvertising.parse(peripheral);
184+
let ad = parse(peripheral);
185185
if (this._filterAdvertising(ad, id, model)) {
186186
let device = null;
187187
if (ad.serviceData.model === 'H') {
@@ -248,13 +248,13 @@ class Switchbot {
248248
startScan(params) {
249249
let promise = new Promise((resolve, reject) => {
250250
// Check the parameters
251-
let valid = parameterChecker.check(params, {
251+
let valid = check(params, {
252252
model: { required: false, type: 'string', enum: ['H', 'T', 'c'] },
253253
id: { required: false, type: 'string', min: 12, max: 17 },
254254
}, false);
255255

256256
if (!valid) {
257-
reject(new Error(parameterChecker.error.message));
257+
reject(new Error(_error.message));
258258
return;
259259
}
260260

@@ -273,7 +273,7 @@ class Switchbot {
273273

274274
// Set an handler for the 'discover' event
275275
this.noble.on('discover', (peripheral) => {
276-
let ad = switchbotAdvertising.parse(peripheral);
276+
let ad = parse(peripheral);
277277
if (this._filterAdvertising(ad, p.id, p.model)) {
278278
if (this.onadvertisement && typeof (this.onadvertisement) === 'function') {
279279
this.onadvertisement(ad);
@@ -325,12 +325,12 @@ class Switchbot {
325325
wait(msec) {
326326
return new Promise((resolve, reject) => {
327327
// Check the parameters
328-
let valid = parameterChecker.check({ msec: msec }, {
328+
let valid = check({ msec: msec }, {
329329
msec: { required: true, type: 'integer', min: 0 }
330330
});
331331

332332
if (!valid) {
333-
reject(new Error(parameterChecker.error.message));
333+
reject(new Error(_error.message));
334334
return;
335335
}
336336
// Set a timer
@@ -340,4 +340,4 @@ class Switchbot {
340340

341341
}
342342

343-
module.exports = Switchbot;
343+
export default Switchbot;

0 commit comments

Comments
 (0)