node-switchbot 2.5.0-beta.3 → 2.5.0-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.
- package/dist/device.d.ts +86 -223
- package/dist/device.d.ts.map +1 -1
- package/dist/device.js +123 -407
- package/dist/device.js.map +1 -1
- package/dist/parameter-checker.d.ts +78 -18
- package/dist/parameter-checker.d.ts.map +1 -1
- package/dist/parameter-checker.js +139 -334
- package/dist/parameter-checker.js.map +1 -1
- package/dist/settings.d.ts +5 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +3 -2
- package/dist/settings.js.map +1 -1
- package/dist/types/types.d.ts +9 -0
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/device.d.ts
CHANGED
|
@@ -1,254 +1,117 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
2
|
import type * as Noble from '@stoprocent/noble';
|
|
3
3
|
import type { SwitchBotBLEModel, SwitchBotBLEModelName } from './types/types.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
device: Noble.Characteristic | null;
|
|
8
|
-
} | null;
|
|
4
|
+
/**
|
|
5
|
+
* Represents a Switchbot Device.
|
|
6
|
+
*/
|
|
9
7
|
export declare class SwitchbotDevice {
|
|
10
|
-
_noble
|
|
11
|
-
_peripheral
|
|
12
|
-
_characteristics
|
|
13
|
-
_id
|
|
14
|
-
_address
|
|
15
|
-
_model
|
|
16
|
-
_modelName
|
|
17
|
-
_explicitly
|
|
18
|
-
_connected
|
|
19
|
-
onnotify_internal
|
|
20
|
-
ondisconnect_internal
|
|
21
|
-
onconnect_internal
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* @param
|
|
26
|
-
* @param {Noble} noble - The Noble object created by the noble module.
|
|
27
|
-
*
|
|
28
|
-
* This constructor initializes a new instance of the Device class with the specified peripheral and noble objects.
|
|
8
|
+
private _noble;
|
|
9
|
+
private _peripheral;
|
|
10
|
+
private _characteristics;
|
|
11
|
+
private _id;
|
|
12
|
+
private _address;
|
|
13
|
+
private _model;
|
|
14
|
+
private _modelName;
|
|
15
|
+
private _explicitly;
|
|
16
|
+
private _connected;
|
|
17
|
+
private onnotify_internal;
|
|
18
|
+
private ondisconnect_internal;
|
|
19
|
+
private onconnect_internal;
|
|
20
|
+
/**
|
|
21
|
+
* Initializes a new instance of the SwitchbotDevice class.
|
|
22
|
+
* @param peripheral The peripheral object from noble.
|
|
23
|
+
* @param noble The Noble object.
|
|
29
24
|
*/
|
|
30
25
|
constructor(peripheral: Noble.Peripheral, noble: typeof Noble);
|
|
31
26
|
get id(): string;
|
|
32
27
|
get address(): string;
|
|
33
|
-
get model():
|
|
34
|
-
get modelName():
|
|
28
|
+
get model(): SwitchBotBLEModel;
|
|
29
|
+
get modelName(): SwitchBotBLEModelName;
|
|
35
30
|
get connectionState(): string;
|
|
36
|
-
get onconnect(): () => Promise<void
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* @param func A function that returns a Promise, representing the asynchronous operation of connecting.
|
|
45
|
-
* This function is expected to be called without any arguments.
|
|
46
|
-
* @throws Error if the provided argument is not a function, ensuring type safety.
|
|
47
|
-
*/
|
|
48
|
-
set onconnect(func: () => Promise<void> | void);
|
|
49
|
-
get ondisconnect(): () => Promise<void> | void;
|
|
50
|
-
/**
|
|
51
|
-
* Sets the asynchronous disconnection handler.
|
|
52
|
-
*
|
|
53
|
-
* This setter configures a function to act as the asynchronous disconnection handler. The handler
|
|
54
|
-
* should be a function that returns a Promise, which resolves when the disconnection process
|
|
55
|
-
* is complete. The resolution of the Promise does not carry any value.
|
|
56
|
-
*
|
|
57
|
-
* @param func A function that returns a Promise, representing the asynchronous operation of disconnecting.
|
|
58
|
-
* This function is expected to be called without any arguments.
|
|
59
|
-
* @throws Error if the provided argument is not a function, to ensure that the handler is correctly typed.
|
|
60
|
-
*/
|
|
61
|
-
set ondisconnect(func: () => Promise<void> | void);
|
|
62
|
-
/**
|
|
63
|
-
* Initiates an asynchronous connection process.
|
|
64
|
-
*
|
|
65
|
-
* This method marks the device as being connected explicitly by setting a flag, then proceeds
|
|
66
|
-
* to initiate the actual connection process by calling an internal asynchronous method `connect_internalAsync`.
|
|
67
|
-
* The `connect_internalAsync` method is responsible for handling the low-level connection logic.
|
|
68
|
-
*
|
|
69
|
-
* @returns A Promise that resolves when the connection process initiated by `connect_internalAsync` completes.
|
|
70
|
-
* The resolution of this Promise does not carry any value, indicating that the focus is on
|
|
71
|
-
* the completion of the connection process rather than the result of the connection itself.
|
|
31
|
+
get onconnect(): () => Promise<void>;
|
|
32
|
+
set onconnect(func: () => Promise<void>);
|
|
33
|
+
get ondisconnect(): () => Promise<void>;
|
|
34
|
+
set ondisconnect(func: () => Promise<void>);
|
|
35
|
+
/**
|
|
36
|
+
* Connects to the device.
|
|
37
|
+
* @returns A Promise that resolves when the connection is complete.
|
|
72
38
|
*/
|
|
73
39
|
connect(): Promise<void>;
|
|
74
40
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
*
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
*
|
|
102
|
-
|
|
103
|
-
discoverCharacteristics(service: Noble.Service): Promise<Noble.Characteristic[]>;
|
|
104
|
-
subscribe(): Promise<void>;
|
|
105
|
-
/**
|
|
106
|
-
* Asynchronously unsubscribes from the device's notification characteristic.
|
|
107
|
-
*
|
|
108
|
-
* This method checks if the notification characteristic object exists within the cached characteristics
|
|
109
|
-
* (`this.chars`). If the characteristic is found, it proceeds to remove all event listeners attached to it
|
|
110
|
-
* to prevent any further handling of incoming data notifications. Then, it asynchronously unsubscribes from
|
|
111
|
-
* the notification characteristic using `unsubscribeAsync()`, effectively stopping the device from sending
|
|
112
|
-
* notifications to the client.
|
|
113
|
-
*
|
|
114
|
-
* If the notification characteristic is not found, the method simply returns without performing any action.
|
|
115
|
-
*
|
|
116
|
-
* @return A Promise that resolves to `void` upon successful unsubscription or if the characteristic is not found.
|
|
41
|
+
* Internal method to handle the connection process.
|
|
42
|
+
* @returns A Promise that resolves when the connection is complete.
|
|
43
|
+
*/
|
|
44
|
+
private connect_internal;
|
|
45
|
+
/**
|
|
46
|
+
* Retrieves the device characteristics.
|
|
47
|
+
* @returns A Promise that resolves with the device characteristics.
|
|
48
|
+
*/
|
|
49
|
+
private getCharacteristics;
|
|
50
|
+
/**
|
|
51
|
+
* Discovers the device services.
|
|
52
|
+
* @returns A Promise that resolves with the list of services.
|
|
53
|
+
*/
|
|
54
|
+
private discoverServices;
|
|
55
|
+
/**
|
|
56
|
+
* Discovers the characteristics of a service.
|
|
57
|
+
* @param service The service to discover characteristics for.
|
|
58
|
+
* @returns A Promise that resolves with the list of characteristics.
|
|
59
|
+
*/
|
|
60
|
+
private discoverCharacteristics;
|
|
61
|
+
/**
|
|
62
|
+
* Subscribes to the notify characteristic.
|
|
63
|
+
* @returns A Promise that resolves when the subscription is complete.
|
|
64
|
+
*/
|
|
65
|
+
private subscribe;
|
|
66
|
+
/**
|
|
67
|
+
* Unsubscribes from the notify characteristic.
|
|
68
|
+
* @returns A Promise that resolves when the unsubscription is complete.
|
|
117
69
|
*/
|
|
118
70
|
unsubscribe(): Promise<void>;
|
|
119
71
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* This method handles the disconnection process by first checking the current
|
|
123
|
-
* connection state of the device. If the device is already disconnected, the
|
|
124
|
-
* method resolves immediately. If the device is in the process of connecting or
|
|
125
|
-
* disconnecting, it throws an error indicating that the operation should be retried
|
|
126
|
-
* after a brief wait. Otherwise, it proceeds to unsubscribe from any subscriptions
|
|
127
|
-
* and then initiates the disconnection process.
|
|
128
|
-
*
|
|
129
|
-
* Note: This method sets a flag to indicate that the disconnection was not initiated
|
|
130
|
-
* by the user explicitly.
|
|
131
|
-
*
|
|
132
|
-
* @returns A Promise that resolves when the disconnection process has completed.
|
|
133
|
-
* The Promise does not pass any value upon resolution.
|
|
72
|
+
* Disconnects from the device.
|
|
73
|
+
* @returns A Promise that resolves when the disconnection is complete.
|
|
134
74
|
*/
|
|
135
75
|
disconnect(): Promise<void>;
|
|
136
76
|
/**
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* This method checks the `explicitly` flag to determine if the connection was initiated by the user.
|
|
140
|
-
* If not, it proceeds to disconnect from the device by calling `this.disconnectAsync()`. After the disconnection,
|
|
141
|
-
* it sets `explicitly` to true to prevent future disconnections when the connection is user-initiated.
|
|
142
|
-
*
|
|
143
|
-
* This approach ensures that automatic disconnections only occur when the user has not explicitly initiated the connection,
|
|
144
|
-
* avoiding unnecessary disconnections in user-initiated sessions.
|
|
145
|
-
*
|
|
146
|
-
* @returns A Promise that resolves once the device has been successfully disconnected, applicable only when the
|
|
147
|
-
* connection was not user-initiated.
|
|
148
|
-
*/
|
|
149
|
-
disconnect_internal(): Promise<void>;
|
|
150
|
-
/**
|
|
151
|
-
* Asynchronously retrieves the device name.
|
|
152
|
-
* This method is designed to fetch the name of the device asynchronously and return it as a promise.
|
|
153
|
-
* It is marked as deprecated and will be removed in a future version. Use `getDeviceNameAsync` instead.
|
|
154
|
-
*
|
|
155
|
-
* @deprecated since version 2.4.0. Will be removed in version 3.0.0. Use `getDeviceNameAsync()` instead.
|
|
156
|
-
* @returns A Promise that resolves with the device name.
|
|
77
|
+
* Internal method to handle disconnection if not explicitly initiated.
|
|
78
|
+
* @returns A Promise that resolves when the disconnection is complete.
|
|
157
79
|
*/
|
|
158
|
-
|
|
80
|
+
private disconnect_internal;
|
|
159
81
|
/**
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
* reads the characteristic to obtain the device name, and then disconnects from the device.
|
|
163
|
-
* It ensures that the device supports the required characteristic for fetching the name. If the characteristic
|
|
164
|
-
* is not supported, it throws an error. The method encapsulates the entire process of connecting, reading,
|
|
165
|
-
* and disconnecting, making it convenient to get the device name with a single call.
|
|
166
|
-
*
|
|
167
|
-
* @returns A Promise that resolves with the device name as a string.
|
|
82
|
+
* Retrieves the device name.
|
|
83
|
+
* @returns A Promise that resolves with the device name.
|
|
168
84
|
*/
|
|
169
|
-
|
|
85
|
+
getDeviceName(): Promise<string>;
|
|
170
86
|
/**
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* Upon successful validation, the device name is updated, and the promise resolves without passing any value.
|
|
175
|
-
*
|
|
176
|
-
* @param name The new device name as a string. Must be 1 to 100 bytes in length.
|
|
177
|
-
* @returns A Promise that resolves to `void` upon successful update of the device name.
|
|
178
|
-
* @deprecated since version 2.4.0. Will be removed in version 3.0.0. Use `setDeviceNameAsync()` instead.
|
|
87
|
+
* Sets the device name.
|
|
88
|
+
* @param name The new device name.
|
|
89
|
+
* @returns A Promise that resolves when the name is set.
|
|
179
90
|
*/
|
|
180
91
|
setDeviceName(name: string): Promise<void>;
|
|
181
92
|
/**
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
* After passing validation, the method converts the name into a UTF-8 encoded buffer.
|
|
186
|
-
* It then initiates a connection to the device. If the device does not support the required characteristic UUID for setting the device name,
|
|
187
|
-
* an error is thrown indicating the lack of support.
|
|
188
|
-
* Upon successfully connecting and verifying support, the method writes the new name to the device using the appropriate characteristic.
|
|
189
|
-
* Finally, it disconnects from the device, completing the name update process.
|
|
190
|
-
*
|
|
191
|
-
* @param name The new device name as a string. Must be 1 to 100 bytes in length.
|
|
192
|
-
* @returns A Promise that resolves to `void` upon successful update of the device name.
|
|
193
|
-
*/
|
|
194
|
-
setDeviceNameAsync(name: string): Promise<void>;
|
|
195
|
-
/**
|
|
196
|
-
* Asynchronously sends a command to the device and awaits a response.
|
|
197
|
-
* This method encapsulates the process of sending a command encapsulated in a Buffer to the device,
|
|
198
|
-
* and then waiting for the device to respond. The method ensures that the device is connected before sending the command,
|
|
199
|
-
* writes the command to the device's write characteristic, waits for a response on the notify characteristic,
|
|
200
|
-
* and finally disconnects from the device. The response from the device is returned as a Buffer.
|
|
201
|
-
*
|
|
202
|
-
* @param req_buf A Buffer containing the command to be sent to the device.
|
|
203
|
-
* @returns A Promise that resolves with a Buffer containing the device's response to the command.
|
|
93
|
+
* Sends a command to the device and awaits a response.
|
|
94
|
+
* @param req_buf The command buffer.
|
|
95
|
+
* @returns A Promise that resolves with the response buffer.
|
|
204
96
|
*/
|
|
205
97
|
command(req_buf: Buffer): Promise<Buffer>;
|
|
206
98
|
/**
|
|
207
99
|
* Waits for a response from the device after sending a command.
|
|
208
|
-
*
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
*
|
|
213
|
-
* @
|
|
214
|
-
*
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
*
|
|
220
|
-
*
|
|
221
|
-
* A
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
* Once a response is received or a timeout occurs, the internal handler is reset to an empty function to prevent memory leaks.
|
|
225
|
-
*
|
|
226
|
-
* @returns A Promise that resolves with the received Buffer or rejects with an error if a timeout occurs.
|
|
227
|
-
*/
|
|
228
|
-
_waitCommandResponseAsync(): Promise<Buffer>;
|
|
229
|
-
/**
|
|
230
|
-
* Asynchronously reads data from the specified characteristic of the device with a timeout.
|
|
231
|
-
* This method attempts to read data from the device's characteristic and sets a timeout to handle cases where the read operation takes too long.
|
|
232
|
-
* If the read operation does not complete within the specified timeout period, an error is thrown.
|
|
233
|
-
* Once the read operation completes successfully, the timeout is cleared to prevent it from triggering.
|
|
234
|
-
*
|
|
235
|
-
* @param char The characteristic of the device from which data will be read.
|
|
236
|
-
* @returns A Promise that resolves with the data read from the characteristic or rejects with an error if a timeout occurs.
|
|
237
|
-
*
|
|
238
|
-
* @throws Error if the read operation fails or if a timeout occurs.
|
|
239
|
-
*/
|
|
240
|
-
read(char: Noble.Characteristic): Promise<Buffer>;
|
|
241
|
-
/**
|
|
242
|
-
* Asynchronously writes data to a specified characteristic of the device.
|
|
243
|
-
* This method sends a buffer of data to the device's characteristic and sets a timeout to handle cases where the write operation takes too long.
|
|
244
|
-
* If the write operation does not complete within the specified timeout period, an error is thrown.
|
|
245
|
-
* Once the write operation completes successfully, the timeout is cleared to prevent it from triggering.
|
|
246
|
-
*
|
|
247
|
-
* @param char The characteristic of the device to which the data will be written.
|
|
248
|
-
* @param buf A Buffer containing the data to be written to the device.
|
|
249
|
-
* @returns A Promise that resolves when the write operation completes successfully or rejects with an error if a timeout occurs.
|
|
250
|
-
*/
|
|
251
|
-
write(char: Noble.Characteristic, buf: Buffer): Promise<void | string>;
|
|
100
|
+
* @returns A Promise that resolves with the response buffer.
|
|
101
|
+
*/
|
|
102
|
+
private _waitCommandResponseAsync;
|
|
103
|
+
/**
|
|
104
|
+
* Reads data from a characteristic with a timeout.
|
|
105
|
+
* @param char The characteristic to read from.
|
|
106
|
+
* @returns A Promise that resolves with the data buffer.
|
|
107
|
+
*/
|
|
108
|
+
private read;
|
|
109
|
+
/**
|
|
110
|
+
* Writes data to a characteristic with a timeout.
|
|
111
|
+
* @param char The characteristic to write to.
|
|
112
|
+
* @param buf The data buffer.
|
|
113
|
+
* @returns A Promise that resolves when the write is complete.
|
|
114
|
+
*/
|
|
115
|
+
private write;
|
|
252
116
|
}
|
|
253
|
-
export {};
|
|
254
117
|
//# sourceMappingURL=device.d.ts.map
|
package/dist/device.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"device.d.ts","sourceRoot":"","sources":["../src/device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