node-switchbot 2.5.0-beta.12 → 2.5.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.
- package/dist/switchbot.d.ts +17 -52
- package/dist/switchbot.d.ts.map +1 -1
- package/dist/switchbot.js +106 -177
- package/dist/switchbot.js.map +1 -1
- package/package.json +1 -1
package/dist/switchbot.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import type * as Noble from '@stoprocent/noble';
|
|
1
2
|
import type { Ad, Params } from './types/types.js';
|
|
2
|
-
import * as Noble from '@stoprocent/noble';
|
|
3
3
|
import { SwitchbotDevice } from './device.js';
|
|
4
4
|
/**
|
|
5
|
-
* SwitchBot class to interact with SwitchBot devices.
|
|
5
|
+
* SwitchBot class to interact with SwitchBot devices via BLE.
|
|
6
6
|
*/
|
|
7
7
|
export declare class SwitchBot {
|
|
8
8
|
private ready;
|
|
@@ -12,72 +12,37 @@ export declare class SwitchBot {
|
|
|
12
12
|
onlog?: (message: string) => void;
|
|
13
13
|
DEFAULT_DISCOVERY_DURATION: number;
|
|
14
14
|
PRIMARY_SERVICE_UUID_LIST: string[];
|
|
15
|
-
/**
|
|
16
|
-
* Constructor
|
|
17
|
-
*
|
|
18
|
-
* @param {Params} [params] - Optional parameters
|
|
19
|
-
* @param {typeof Noble} [params.noble] - Optional noble instance
|
|
20
|
-
*/
|
|
21
15
|
constructor(params?: Params);
|
|
22
16
|
/**
|
|
23
17
|
* Initializes the noble object.
|
|
24
|
-
*
|
|
25
|
-
* @param {Params} [params] - Optional parameters
|
|
26
|
-
* @param {typeof Noble} [params.noble] - Optional noble instance
|
|
27
|
-
* @returns {Promise<void>} - Resolves when initialization is complete
|
|
28
|
-
*/
|
|
29
|
-
init(params?: Params): Promise<void>;
|
|
30
|
-
/**
|
|
31
|
-
* Discover SwitchBot devices based on the provided parameters.
|
|
32
|
-
*
|
|
33
|
-
* @param {Params} params - The parameters for discovery.
|
|
34
|
-
* @returns {Promise<SwitchbotDevice[]>} - A promise that resolves with a list of discovered devices.
|
|
18
|
+
* @param params Optional parameters.
|
|
35
19
|
*/
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Initializes the noble object and waits for it to be powered on.
|
|
39
|
-
*
|
|
40
|
-
* @returns {Promise<void>} - Resolves when the noble object is powered on.
|
|
41
|
-
*/
|
|
42
|
-
_init(): Promise<void>;
|
|
20
|
+
private init;
|
|
43
21
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @param {Noble.Peripheral} peripheral - The peripheral object.
|
|
47
|
-
* @param {string} id - The device id.
|
|
48
|
-
* @param {string} model - The device model.
|
|
49
|
-
* @returns {Promise<SwitchbotDevice | null>} - The device object or null.
|
|
22
|
+
* Ensures the noble object is powered on.
|
|
50
23
|
*/
|
|
51
|
-
|
|
24
|
+
private ensureNobleReady;
|
|
52
25
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @param {Ad} ad - The advertising data.
|
|
56
|
-
* @param {string} id - The device id.
|
|
57
|
-
* @param {string} model - The device model.
|
|
58
|
-
* @returns {boolean} - True if the advertising data matches the id and model, false otherwise.
|
|
26
|
+
* Discover SwitchBot devices.
|
|
27
|
+
* @param params Optional parameters for discovery.
|
|
59
28
|
*/
|
|
60
|
-
|
|
29
|
+
discover(params?: Params): Promise<SwitchbotDevice[]>;
|
|
61
30
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* @param {Params} [params] - Optional parameters.
|
|
65
|
-
* @returns {Promise<void>} - Resolves when scanning starts successfully.
|
|
31
|
+
* Start monitoring advertising packets from SwitchBot devices.
|
|
32
|
+
* @param params Optional parameters for scanning.
|
|
66
33
|
*/
|
|
67
34
|
startScan(params?: Params): Promise<void>;
|
|
68
35
|
/**
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* @returns {Promise<void>} - Resolves when scanning stops successfully.
|
|
36
|
+
* Stop monitoring advertising packets from SwitchBot devices.
|
|
72
37
|
*/
|
|
73
|
-
stopScan():
|
|
38
|
+
stopScan(): void;
|
|
74
39
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* @param {number} msec - The time to wait in milliseconds.
|
|
78
|
-
* @returns {Promise<void>} - Resolves after the specified time.
|
|
40
|
+
* Wait for the specified time.
|
|
41
|
+
* @param msec Time in milliseconds.
|
|
79
42
|
*/
|
|
80
43
|
wait(msec: number): Promise<void>;
|
|
44
|
+
private getDeviceObject;
|
|
45
|
+
private filterAdvertising;
|
|
81
46
|
}
|
|
82
47
|
export { SwitchbotDevice };
|
|
83
48
|
//# sourceMappingURL=switchbot.d.ts.map
|
package/dist/switchbot.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"switchbot.d.ts","sourceRoot":"","sources":["../src/switchbot.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"switchbot.d.ts","sourceRoot":"","sources":["../src/switchbot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,KAAK,MAAM,mBAAmB,CAAA;AAM/C,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAGlD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAmB7C;;GAEG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,KAAK,CAAe;IAC5B,KAAK,EAAG,OAAO,KAAK,CAAA;IACpB,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,KAAK,IAAI,CAAA;IAC9C,eAAe,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,KAAK,IAAI,CAAA;IAClC,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;IACjC,0BAA0B,SAAO;IACjC,yBAAyB,EAAE,MAAM,EAAE,CAAK;gBAE5B,MAAM,CAAC,EAAE,MAAM;IAI3B;;;OAGG;YACW,IAAI;IAIlB;;OAEG;YACW,gBAAgB;IAiB9B;;;OAGG;IACG,QAAQ,CAAC,MAAM,GAAE,MAAW,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IA4C/D;;;OAGG;IACG,SAAS,CAAC,MAAM,GAAE,MAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAgCnD;;OAEG;IACH,QAAQ,IAAI,IAAI;IAOhB;;;OAGG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAQnB,eAAe;IA6B7B,OAAO,CAAC,iBAAiB;CAY1B;AAED,OAAO,EAAE,eAAe,EAAE,CAAA"}
|
package/dist/switchbot.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as Noble from '@stoprocent/noble';
|
|
2
1
|
import { Advertising } from './advertising.js';
|
|
3
2
|
import { SwitchbotDevice } from './device.js';
|
|
4
3
|
import { WoBlindTilt } from './device/woblindtilt.js';
|
|
@@ -19,7 +18,7 @@ import { WoStrip } from './device/wostrip.js';
|
|
|
19
18
|
import { parameterChecker } from './parameter-checker.js';
|
|
20
19
|
import { SwitchBotBLEModel } from './types/types.js';
|
|
21
20
|
/**
|
|
22
|
-
* SwitchBot class to interact with SwitchBot devices.
|
|
21
|
+
* SwitchBot class to interact with SwitchBot devices via BLE.
|
|
23
22
|
*/
|
|
24
23
|
export class SwitchBot {
|
|
25
24
|
ready;
|
|
@@ -29,33 +28,38 @@ export class SwitchBot {
|
|
|
29
28
|
onlog;
|
|
30
29
|
DEFAULT_DISCOVERY_DURATION = 5000;
|
|
31
30
|
PRIMARY_SERVICE_UUID_LIST = [];
|
|
32
|
-
/**
|
|
33
|
-
* Constructor
|
|
34
|
-
*
|
|
35
|
-
* @param {Params} [params] - Optional parameters
|
|
36
|
-
* @param {typeof Noble} [params.noble] - Optional noble instance
|
|
37
|
-
*/
|
|
38
31
|
constructor(params) {
|
|
39
32
|
this.ready = this.init(params);
|
|
40
33
|
}
|
|
41
34
|
/**
|
|
42
35
|
* Initializes the noble object.
|
|
43
|
-
*
|
|
44
|
-
* @param {Params} [params] - Optional parameters
|
|
45
|
-
* @param {typeof Noble} [params.noble] - Optional noble instance
|
|
46
|
-
* @returns {Promise<void>} - Resolves when initialization is complete
|
|
36
|
+
* @param params Optional parameters.
|
|
47
37
|
*/
|
|
48
38
|
async init(params) {
|
|
49
|
-
this.noble = params?.noble ??
|
|
50
|
-
|
|
51
|
-
|
|
39
|
+
this.noble = params?.noble ?? await import('@stoprocent/noble');
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Ensures the noble object is powered on.
|
|
43
|
+
*/
|
|
44
|
+
async ensureNobleReady() {
|
|
45
|
+
await this.ready;
|
|
46
|
+
if (this.noble._state === 'poweredOn') {
|
|
47
|
+
return;
|
|
52
48
|
}
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
this.noble.once('stateChange', (state) => {
|
|
51
|
+
if (state === 'poweredOn') {
|
|
52
|
+
resolve();
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
reject(new Error(`Noble state: ${state}`));
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
53
59
|
}
|
|
54
60
|
/**
|
|
55
|
-
* Discover SwitchBot devices
|
|
56
|
-
*
|
|
57
|
-
* @param {Params} params - The parameters for discovery.
|
|
58
|
-
* @returns {Promise<SwitchbotDevice[]>} - A promise that resolves with a list of discovered devices.
|
|
61
|
+
* Discover SwitchBot devices.
|
|
62
|
+
* @param params Optional parameters for discovery.
|
|
59
63
|
*/
|
|
60
64
|
async discover(params = {}) {
|
|
61
65
|
const valid = parameterChecker.check(params, {
|
|
@@ -68,198 +72,123 @@ export class SwitchBot {
|
|
|
68
72
|
throw new Error(parameterChecker.error.message);
|
|
69
73
|
}
|
|
70
74
|
const { duration = this.DEFAULT_DISCOVERY_DURATION, model = '', id = '', quick = false } = params;
|
|
71
|
-
await this.
|
|
72
|
-
if (!this.noble) {
|
|
73
|
-
throw new Error('noble failed to initialize');
|
|
74
|
-
}
|
|
75
|
+
await this.ensureNobleReady();
|
|
75
76
|
const peripherals = {};
|
|
76
|
-
let timer;
|
|
77
|
-
const finishDiscovery = () => {
|
|
78
|
-
clearTimeout(timer);
|
|
79
|
-
this.noble.removeAllListeners('discover');
|
|
80
|
-
this.noble.stopScanning();
|
|
81
|
-
return Object.values(peripherals);
|
|
82
|
-
};
|
|
83
77
|
return new Promise((resolve, reject) => {
|
|
84
|
-
|
|
85
|
-
this.noble.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
78
|
+
const finishDiscovery = () => {
|
|
79
|
+
this.noble.removeAllListeners('discover');
|
|
80
|
+
this.noble.stopScanning();
|
|
81
|
+
resolve(Object.values(peripherals));
|
|
82
|
+
};
|
|
83
|
+
this.noble.on('discover', async (peripheral) => {
|
|
84
|
+
const device = await this.getDeviceObject(peripheral, id, model);
|
|
85
|
+
if (device) {
|
|
86
|
+
peripherals[device.id] = device;
|
|
87
|
+
this.ondiscover?.(device);
|
|
88
|
+
if (quick) {
|
|
89
|
+
finishDiscovery();
|
|
95
90
|
}
|
|
96
|
-
});
|
|
97
|
-
this.noble.startScanningAsync(this.PRIMARY_SERVICE_UUID_LIST, false)
|
|
98
|
-
.then(() => {
|
|
99
|
-
timer = setTimeout(() => {
|
|
100
|
-
resolve(finishDiscovery());
|
|
101
|
-
}, duration);
|
|
102
|
-
})
|
|
103
|
-
.catch((error) => {
|
|
104
|
-
reject(new Error(error.message));
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
reject(new Error(String(error)));
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Initializes the noble object and waits for it to be powered on.
|
|
114
|
-
*
|
|
115
|
-
* @returns {Promise<void>} - Resolves when the noble object is powered on.
|
|
116
|
-
*/
|
|
117
|
-
async _init() {
|
|
118
|
-
await this.ready;
|
|
119
|
-
if (this.noble._state === 'poweredOn') {
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
return new Promise((resolve, reject) => {
|
|
123
|
-
if (typeof this.noble.once !== 'function') {
|
|
124
|
-
return reject(new Error('noble.once is not a function'));
|
|
125
|
-
}
|
|
126
|
-
this.noble.once('stateChange', (state) => {
|
|
127
|
-
if (state === 'poweredOn') {
|
|
128
|
-
resolve();
|
|
129
|
-
}
|
|
130
|
-
else if (['unsupported', 'unauthorized', 'poweredOff'].includes(state)) {
|
|
131
|
-
reject(new Error(`Failed to initialize the Noble object: ${state}`));
|
|
132
91
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
reject(new Error(`Unknown state: ${state}`));
|
|
92
|
+
});
|
|
93
|
+
this.noble.startScanning(this.PRIMARY_SERVICE_UUID_LIST, false, (error) => {
|
|
94
|
+
if (error) {
|
|
95
|
+
return reject(error);
|
|
138
96
|
}
|
|
97
|
+
setTimeout(finishDiscovery, duration);
|
|
139
98
|
});
|
|
140
99
|
});
|
|
141
100
|
}
|
|
142
101
|
/**
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
* @param {Noble.Peripheral} peripheral - The peripheral object.
|
|
146
|
-
* @param {string} id - The device id.
|
|
147
|
-
* @param {string} model - The device model.
|
|
148
|
-
* @returns {Promise<SwitchbotDevice | null>} - The device object or null.
|
|
149
|
-
*/
|
|
150
|
-
async getDeviceObject(peripheral, id, model) {
|
|
151
|
-
const ad = await Advertising.parse(peripheral, this.onlog);
|
|
152
|
-
if (!ad || !this.filterAdvertising(ad, id, model)) {
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
const modelMapping = {
|
|
156
|
-
[SwitchBotBLEModel.Bot]: WoHand,
|
|
157
|
-
[SwitchBotBLEModel.Curtain]: WoCurtain,
|
|
158
|
-
[SwitchBotBLEModel.Curtain3]: WoCurtain,
|
|
159
|
-
[SwitchBotBLEModel.Humidifier]: WoHumi,
|
|
160
|
-
[SwitchBotBLEModel.Meter]: WoSensorTH,
|
|
161
|
-
[SwitchBotBLEModel.MeterPlus]: WoSensorTH,
|
|
162
|
-
[SwitchBotBLEModel.Hub2]: WoHub2,
|
|
163
|
-
[SwitchBotBLEModel.OutdoorMeter]: WoIOSensorTH,
|
|
164
|
-
[SwitchBotBLEModel.MotionSensor]: WoPresence,
|
|
165
|
-
[SwitchBotBLEModel.ContactSensor]: WoContact,
|
|
166
|
-
[SwitchBotBLEModel.ColorBulb]: WoBulb,
|
|
167
|
-
[SwitchBotBLEModel.CeilingLight]: WoCeilingLight,
|
|
168
|
-
[SwitchBotBLEModel.CeilingLightPro]: WoCeilingLight,
|
|
169
|
-
[SwitchBotBLEModel.StripLight]: WoStrip,
|
|
170
|
-
[SwitchBotBLEModel.PlugMiniUS]: WoPlugMini,
|
|
171
|
-
[SwitchBotBLEModel.PlugMiniJP]: WoPlugMini,
|
|
172
|
-
[SwitchBotBLEModel.Lock]: WoSmartLock,
|
|
173
|
-
[SwitchBotBLEModel.LockPro]: WoSmartLockPro,
|
|
174
|
-
[SwitchBotBLEModel.BlindTilt]: WoBlindTilt,
|
|
175
|
-
};
|
|
176
|
-
const DeviceClass = ad?.serviceData?.model ? modelMapping[ad.serviceData.model] : SwitchbotDevice;
|
|
177
|
-
return new DeviceClass(peripheral, this.noble);
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Filters advertising data based on id and model.
|
|
181
|
-
*
|
|
182
|
-
* @param {Ad} ad - The advertising data.
|
|
183
|
-
* @param {string} id - The device id.
|
|
184
|
-
* @param {string} model - The device model.
|
|
185
|
-
* @returns {boolean} - True if the advertising data matches the id and model, false otherwise.
|
|
186
|
-
*/
|
|
187
|
-
filterAdvertising(ad, id, model) {
|
|
188
|
-
if (!ad) {
|
|
189
|
-
return false;
|
|
190
|
-
}
|
|
191
|
-
if (id) {
|
|
192
|
-
const normalizedId = id.toLowerCase().replace(/:/g, '');
|
|
193
|
-
const adId = ad.address.toLowerCase().replace(/[^a-z0-9]/g, '');
|
|
194
|
-
if (adId !== normalizedId) {
|
|
195
|
-
return false;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
if (model && ad.serviceData.model !== model) {
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
return true;
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Starts scanning for SwitchBot devices.
|
|
205
|
-
*
|
|
206
|
-
* @param {Params} [params] - Optional parameters.
|
|
207
|
-
* @returns {Promise<void>} - Resolves when scanning starts successfully.
|
|
102
|
+
* Start monitoring advertising packets from SwitchBot devices.
|
|
103
|
+
* @param params Optional parameters for scanning.
|
|
208
104
|
*/
|
|
209
105
|
async startScan(params = {}) {
|
|
210
106
|
const valid = parameterChecker.check(params, {
|
|
211
|
-
model: {
|
|
212
|
-
required: false,
|
|
213
|
-
type: 'string',
|
|
214
|
-
enum: Object.values(SwitchBotBLEModel),
|
|
215
|
-
},
|
|
107
|
+
model: { required: false, type: 'string', enum: Object.values(SwitchBotBLEModel) },
|
|
216
108
|
id: { required: false, type: 'string', min: 12, max: 17 },
|
|
217
109
|
}, false);
|
|
218
110
|
if (!valid) {
|
|
219
111
|
throw new Error(parameterChecker.error.message);
|
|
220
112
|
}
|
|
221
|
-
await this._init();
|
|
222
|
-
if (!this.noble) {
|
|
223
|
-
throw new Error('noble object failed to initialize');
|
|
224
|
-
}
|
|
225
113
|
const { model = '', id = '' } = params;
|
|
114
|
+
await this.ensureNobleReady();
|
|
226
115
|
this.noble.on('discover', async (peripheral) => {
|
|
227
116
|
const ad = await Advertising.parse(peripheral, this.onlog);
|
|
228
117
|
if (ad && this.filterAdvertising(ad, id, model)) {
|
|
229
|
-
|
|
230
|
-
this.onadvertisement(ad);
|
|
231
|
-
}
|
|
118
|
+
this.onadvertisement?.(ad);
|
|
232
119
|
}
|
|
233
120
|
});
|
|
234
|
-
|
|
121
|
+
return new Promise((resolve, reject) => {
|
|
122
|
+
this.noble.startScanning(this.PRIMARY_SERVICE_UUID_LIST, true, (error) => {
|
|
123
|
+
if (error) {
|
|
124
|
+
reject(error);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
resolve();
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
});
|
|
235
131
|
}
|
|
236
132
|
/**
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
* @returns {Promise<void>} - Resolves when scanning stops successfully.
|
|
133
|
+
* Stop monitoring advertising packets from SwitchBot devices.
|
|
240
134
|
*/
|
|
241
|
-
|
|
242
|
-
if (
|
|
243
|
-
|
|
135
|
+
stopScan() {
|
|
136
|
+
if (this.noble) {
|
|
137
|
+
this.noble.removeAllListeners('discover');
|
|
138
|
+
this.noble.stopScanning();
|
|
244
139
|
}
|
|
245
|
-
this.noble.removeAllListeners('discover');
|
|
246
|
-
await this.noble.stopScanningAsync();
|
|
247
140
|
}
|
|
248
141
|
/**
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
* @param {number} msec - The time to wait in milliseconds.
|
|
252
|
-
* @returns {Promise<void>} - Resolves after the specified time.
|
|
142
|
+
* Wait for the specified time.
|
|
143
|
+
* @param msec Time in milliseconds.
|
|
253
144
|
*/
|
|
254
|
-
|
|
255
|
-
const valid = parameterChecker.check({ msec }, {
|
|
256
|
-
msec: { required: true, type: 'integer', min: 0 },
|
|
257
|
-
}, true);
|
|
145
|
+
wait(msec) {
|
|
146
|
+
const valid = parameterChecker.check({ msec }, { msec: { required: true, type: 'integer', min: 0 } }, true);
|
|
258
147
|
if (!valid) {
|
|
259
|
-
|
|
148
|
+
return Promise.reject(new Error(parameterChecker.error.message));
|
|
260
149
|
}
|
|
261
150
|
return new Promise(resolve => setTimeout(resolve, msec));
|
|
262
151
|
}
|
|
152
|
+
async getDeviceObject(peripheral, id, model) {
|
|
153
|
+
const ad = await Advertising.parse(peripheral, this.onlog);
|
|
154
|
+
if (ad && this.filterAdvertising(ad, id, model)) {
|
|
155
|
+
switch (ad.serviceData.model) {
|
|
156
|
+
case SwitchBotBLEModel.Bot: return new WoHand(peripheral, this.noble);
|
|
157
|
+
case SwitchBotBLEModel.Curtain:
|
|
158
|
+
case SwitchBotBLEModel.Curtain3: return new WoCurtain(peripheral, this.noble);
|
|
159
|
+
case SwitchBotBLEModel.Humidifier: return new WoHumi(peripheral, this.noble);
|
|
160
|
+
case SwitchBotBLEModel.Meter:
|
|
161
|
+
case SwitchBotBLEModel.MeterPlus: return new WoSensorTH(peripheral, this.noble);
|
|
162
|
+
case SwitchBotBLEModel.Hub2: return new WoHub2(peripheral, this.noble);
|
|
163
|
+
case SwitchBotBLEModel.OutdoorMeter: return new WoIOSensorTH(peripheral, this.noble);
|
|
164
|
+
case SwitchBotBLEModel.MotionSensor: return new WoPresence(peripheral, this.noble);
|
|
165
|
+
case SwitchBotBLEModel.ContactSensor: return new WoContact(peripheral, this.noble);
|
|
166
|
+
case SwitchBotBLEModel.ColorBulb: return new WoBulb(peripheral, this.noble);
|
|
167
|
+
case SwitchBotBLEModel.CeilingLight:
|
|
168
|
+
case SwitchBotBLEModel.CeilingLightPro: return new WoCeilingLight(peripheral, this.noble);
|
|
169
|
+
case SwitchBotBLEModel.StripLight: return new WoStrip(peripheral, this.noble);
|
|
170
|
+
case SwitchBotBLEModel.PlugMiniUS:
|
|
171
|
+
case SwitchBotBLEModel.PlugMiniJP: return new WoPlugMini(peripheral, this.noble);
|
|
172
|
+
case SwitchBotBLEModel.Lock: return new WoSmartLock(peripheral, this.noble);
|
|
173
|
+
case SwitchBotBLEModel.LockPro: return new WoSmartLockPro(peripheral, this.noble);
|
|
174
|
+
case SwitchBotBLEModel.BlindTilt: return new WoBlindTilt(peripheral, this.noble);
|
|
175
|
+
default: return new SwitchbotDevice(peripheral, this.noble);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
filterAdvertising(ad, id, model) {
|
|
181
|
+
if (!ad) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
if (id && ad.address.toLowerCase().replace(/[^a-z0-9]/g, '') !== id.toLowerCase().replace(/:/g, '')) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
if (model && ad.serviceData.model !== model) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
263
192
|
}
|
|
264
193
|
export { SwitchbotDevice };
|
|
265
194
|
//# sourceMappingURL=switchbot.js.map
|
package/dist/switchbot.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"switchbot.js","sourceRoot":"","sources":["../src/switchbot.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"switchbot.js","sourceRoot":"","sources":["../src/switchbot.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAC3C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AACvD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA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I,cAAc,CAAC,UAAU,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;gBACjF,KAAK,iBAAiB,CAAC,SAAS,CAAC,CAAC,OAAO,IAAI,WAAW,CAAC,UAAU,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;gBAChF,OAAO,CAAC,CAAC,OAAO,IAAI,eAAe,CAAC,UAAU,EAAE,IAAI,CAAC,KAAK,CAAC,CAAA;YAC7D,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;IAEO,iBAAiB,CAAC,EAAM,EAAE,EAAU,EAAE,KAAa;QACzD,IAAI,CAAC,EAAE,EAAE,CAAC;YACR,OAAO,KAAK,CAAA;QACd,CAAC;QACD,IAAI,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,KAAK,EAAE,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,CAAC;YACpG,OAAO,KAAK,CAAA;QACd,CAAC;QACD,IAAI,KAAK,IAAI,EAAE,CAAC,WAAW,CAAC,KAAK,KAAK,KAAK,EAAE,CAAC;YAC5C,OAAO,KAAK,CAAA;QACd,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;CACF;AAED,OAAO,EAAE,eAAe,EAAE,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-switchbot",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.5.0-beta.
|
|
4
|
+
"version": "2.5.0-beta.13",
|
|
5
5
|
"description": "The node-switchbot is a Node.js module which allows you to control your Switchbot Devices through Bluetooth (BLE).",
|
|
6
6
|
"author": "OpenWonderLabs (https://github.com/OpenWonderLabs)",
|
|
7
7
|
"license": "MIT",
|