You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Monitoring the advertising packets, you can find your devices and know the latest state of each device. The packet contains the settings of the device, the arm position of the Bot, the temperature and humidity of the Meter, and so on.
110
110
111
-
```JavaScript
111
+
```Typescript
112
112
// Load the node-switchbot and get a `Switchbot` constructor object
113
-
constSwitchbot=require('node-switchbot');
113
+
import { SwitchBot } from'node-switchbot';
114
114
// Create an `Switchbot` object
115
-
constswitchbot=newSwitchbot();
115
+
const switchbot =newSwitchBot();
116
116
117
117
(async () => {
118
118
// Start to monitor advertisement packets
@@ -135,7 +135,7 @@ The [`wait()`](#Switchbot-wait-method) method is just a utility method, which wa
135
135
136
136
The [`startScan()`](#startscan-method) and [`wait()`](#Switchbot-wait-method) methods are asynchronous, they return a `Promise` object. You can write code in promise style as well. What the code below does is as same as what the code above does:
137
137
138
-
```javascript
138
+
```Typescript
139
139
// Load the node-switchbot and get a `Switchbot` constructor object
140
140
const Switchbot =require("node-switchbot");
141
141
// Create an `Switchbot` object
@@ -211,19 +211,19 @@ See the section "[Advertisement data](#Advertisement-data)" for the details of t
211
211
212
212
This sample discovers a Bot (WoHand), then put the Bot's arm down, finally put it up in 5 seconds.
213
213
214
-
```javascript
214
+
```Typescript
215
215
// Load the node-switchbot and get a `Switchbot` constructor object
// The `SwitchbotDeviceWoHand` object representing the found Bot.
226
+
// The `WoHand` object representing the found Bot.
227
227
const device =bot_list[0];
228
228
// Put the Bot's arm down (stretch the arm)
229
229
awaitdevice.down();
@@ -237,25 +237,25 @@ const switchbot = new Switchbot();
237
237
238
238
In order to manipulate the arm of your Bot, you have to discover your Bot using the [`discover()`](#Switchbot-discover-method) method. The object `{ model: 'H' }` passed to the method means that only Bots will be discovered. That is, Meters will be ignored.
239
239
240
-
In this code, you can get a [`SwitchbotDeviceWoHand`](#SwitchbotDeviceWoHand-object) object representing the found Bot. Using the [`down()`](#SwitchbotDeviceWoHand-down-method) and [`up()`](#SwitchbotDeviceWoHand-up-method) methods of the object, you can move the arm. In addition to these methods, you can use the [`press()`](#SwitchbotDeviceWoHand-press-method), [`turnOn()`](#SwitchbotDeviceWoHand-turnOn-method), and [`turnOff()`](#SwitchbotDeviceWoHand-turnOff-method) methods as well.
240
+
In this code, you can get a [`WoHand`](#SwitchbotDeviceWoHand-object) object representing the found Bot. Using the [`down()`](#SwitchbotDeviceWoHand-down-method) and [`up()`](#SwitchbotDeviceWoHand-up-method) methods of the object, you can move the arm. In addition to these methods, you can use the [`press()`](#SwitchbotDeviceWoHand-press-method), [`turnOn()`](#SwitchbotDeviceWoHand-turnOn-method), and [`turnOff()`](#SwitchbotDeviceWoHand-turnOff-method) methods as well.
241
241
242
242
---
243
243
244
244
## `Switchbot` object
245
245
246
246
In order to use the node-switchbot, you have to load the node-switchbot module as follows:
247
247
248
-
```JavaScript
249
-
constSwitchbot=require('node-switchbot');
248
+
```Typescript
249
+
import { SwitchBot } from'node-switchbot';
250
250
```
251
251
252
-
You can get an `Switchbot` constructor from the code above. Then you have to create an `Switchbot` object from the `Switchbot` constructor as follows:
252
+
You can get an `SwitchBot` constructor from the code above. Then you have to create an `SwitchBot` object from the `SwitchBot` constructor as follows:
253
253
254
-
```javascript
255
-
constswitchbot=newSwitchbot();
254
+
```typescript
255
+
const switchbot =newSwitchBot();
256
256
```
257
257
258
-
The `Switchbot` constructor takes an argument optionally. It must be a hash object containing the properties as follows:
258
+
The `SwitchBot` constructor takes an argument optionally. It must be a hash object containing the properties as follows:
@@ -265,16 +265,16 @@ The node-switchbot module uses the [`@abandonware/noble`](https://github.com/aba
265
265
266
266
The sample code below shows how to pass a `Noble` object to the `Switchbot` constructor.
267
267
268
-
```JavaScript
268
+
```Typescript
269
269
// Create a Noble object
270
270
const noble =require('@abandonware/noble');
271
271
272
272
// Create a Switchbot object
273
-
constSwitchbot=require('node-switchbot');
274
-
constswitchbot=newSwitchbot({'noble': noble});
273
+
import { SwitchBot } from'node-switchbot';
274
+
const switchbot =newSwitchBot({ 'noble': noble })
275
275
```
276
276
277
-
In the code snippet above, the variable `switchbot` is an `Switchbot` object. The `Switchbot` object has a lot of methods as described in sections below.
277
+
In the code snippet above, the variable `switchbot` is an `SwitchBot` object. The `SwitchBot` object has a lot of methods as described in sections below.
278
278
279
279
### `discover()` method
280
280
@@ -289,7 +289,7 @@ The `discover` method finds devices. This method returns a `Promise` object. Thi
289
289
290
290
In the code snippet below, no parameter is passed to the method:
291
291
292
-
```JavaScript
292
+
```Typescript
293
293
switchbot.discover().then((device_list) => {
294
294
// Do something...
295
295
}).catch((error) => {
@@ -301,7 +301,7 @@ If no parameter is passed to the method as the code above, an `Array` object wil
301
301
302
302
If you want a quick response, you can set the `quick` property to `true`.
303
303
304
-
```JavaScript
304
+
```Typescript
305
305
switchbot.discover({
306
306
duration: 5000,
307
307
quick: true
@@ -319,7 +319,7 @@ As the `quick` property is set to `true`, the `resolve()` function will be calle
319
319
320
320
The `ondiscover` property on the [`Switchbot`](#Switchbot-object) object is an event handler called whenever a device is newly found in the discovery process. A [`SwitchbotDevice`](#SwitchbotDevice-object) object is passed to the callback function set to the `ondiscover` property.
321
321
322
-
```JavaScript
322
+
```Typescript
323
323
switchbot.ondiscover= (device) => {
324
324
console.log(device.id+' ('+device.modelName+')');
325
325
};
@@ -350,7 +350,7 @@ The `startScan()` method starts to scan advertising packets coming from devices.
350
350
351
351
Whenever a packet is received, the callback function set to the [`onadvertisement`](#Switchbot-onadvertisement-event-handler) property of the [`Switchbot`](#Switchbot-object) object will be called. When a packet is received, a hash object representing the packet will be passed to the callback function.
352
352
353
-
```JavaScript
353
+
```Typescript
354
354
// Set a callback function called when a packet is received
355
355
switchbot.onadvertisement= (ad) => {
356
356
console.log(ad);
@@ -414,9 +414,9 @@ This method has nothing to do with Switchbot devices. It's just a utility method
414
414
415
415
The `SwitchbotDevice` object represents a Switchbot device (Bot, Meter, Curtain, Contact or Motion), which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
416
416
417
-
Actually, the `SwitchbotDevice` object is a super class of the [`SwitchbotDeviceWoHand`](#SwitchbotDeviceWoHand-object) and `SwitchbotDeviceWoSensorTH` objects. The [`SwitchbotDeviceWoHand`](#SwitchbotDeviceWoHand-object) object represents a Bot, the `SwitchbotDeviceWoSensorTH` object represents a Meter.
417
+
Actually, the `SwitchbotDevice` object is a super class of the [`WoHand`](#SwitchbotDeviceWoHand-object) and `WoSensorTH` objects. The [`WoHand`](#SwitchbotDeviceWoHand-object) object represents a Bot, the `WoSensorTH` object represents a Meter.
418
418
419
-
You can use the properties and methods described in this section on Bot, Meter, Curtain, Contact and Motion. See the section "[`SwitchbotDeviceWoHand` object](#SwitchbotDeviceWoHand-object)" for the details of the functionalities available only on Bot. For now, `SwitchbotDeviceWoSensorTH` object has no additional functionality.
419
+
You can use the properties and methods described in this section on Bot, Meter, Curtain, Contact and Motion. See the section "[`WoHand` object](#SwitchbotDeviceWoHand-object)" for the details of the functionalities available only on Bot. For now, `WoSensorTH` object has no additional functionality.
420
420
421
421
### Properties
422
422
@@ -440,7 +440,7 @@ If no connection is established with the device, this method automatically estab
440
440
441
441
If the device name is fetched successfully, the device name will be passed to the `resolve()`.
442
442
443
-
```javascript
443
+
```Typescript
444
444
switchbot
445
445
.discover({ model: "H", quick: true })
446
446
.then((device_list) => {
@@ -458,7 +458,7 @@ switchbot
458
458
459
459
The code above will output the result as follows:
460
460
461
-
```javascript
461
+
```Typescript
462
462
WoHand;
463
463
```
464
464
@@ -470,7 +470,7 @@ If no connection is established with the device, this method automatically estab
470
470
471
471
The character set of the device name saved in the device is UTF-8. The byte length of the name must be less than or equal to 20 bytes. If the name consists of only ASCII characters, up to 20 characters would be allowed. But if the name consists of multibyte characters, the upper limit of characters would be fewer than half. For example, Japanese characters could be saved at most 6 characters because most of Japanese characters consume 3 byte per each character.
472
472
473
-
```javascript
473
+
```Typescript
474
474
switchbot
475
475
.discover({ model: "H", quick: true })
476
476
.then((device_list) => {
@@ -496,7 +496,7 @@ The connection established using the `connect()` method is not disconnected auto
496
496
497
497
The code snippet below establishes a connection with the Bot using the `connect()` method, then puts the Bot's arm down, then waits for 5 seconds, then puts the arm down, finally disconnects the device using the [`disconnect()`](#SwitchbotDevice-disconnect-method) method:
498
498
499
-
```javascript
499
+
```Typescript
500
500
let device =null;
501
501
502
502
switchbot
@@ -561,7 +561,7 @@ The `onconnect` event handler will be called when the connection with the device
561
561
562
562
The code below calls the [`press()`](#SwitchbotDeviceWoHand-press-method) method, while callback functions are attached to the `onconnect` and `ondisconnect`.
563
563
564
-
```javascript
564
+
```Typescript
565
565
switchbot
566
566
.discover({ model: "H", quick: true })
567
567
.then((device_list) => {
@@ -611,19 +611,19 @@ The `ondisconnect` event handler will be called when the connection with the dev
611
611
612
612
---
613
613
614
-
## `SwitchbotDeviceWoHand` object
614
+
## `WoHand` object
615
615
616
-
The `SwitchbotDeviceWoHand` object represents a Bot, which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
616
+
The `WoHand` object represents a Bot, which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
617
617
618
-
Actually, the `SwitchbotDeviceWoHand` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
618
+
Actually, the `WoHand` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
619
619
620
620
### `press()` method
621
621
622
622
The `press()` method sends a press command to the Bot. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
623
623
624
624
If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
625
625
626
-
```javascript
626
+
```Typescript
627
627
switchbot
628
628
.discover({ model: "H", quick: true })
629
629
.then((device_list) => {
@@ -653,7 +653,7 @@ When the Bot receives this command, the Bot's arm will be put down (stretched) o
653
653
| Switch mode | Disabled | Down (stretched) |
654
654
| | Enabled | Up (retracted) |
655
655
656
-
```javascript
656
+
```Typescript
657
657
switchbot
658
658
.discover({ model: "H", quick: true })
659
659
.then((device_list) => {
@@ -681,7 +681,7 @@ When the Bot receives this command, the Bot's arm will be put down (stretched) o
681
681
| Switch mode | Disabled | Up (retracted) |
682
682
| | Enabled | Down (stretched) |
683
683
684
-
```javascript
684
+
```Typescript
685
685
switchbot
686
686
.discover({ model: "H", quick: true })
687
687
.then((device_list) => {
@@ -703,7 +703,7 @@ If no connection is established with the device, this method automatically estab
703
703
704
704
When the Bot receives this command, the Bot's arm will be put down (stretched) regardless of the mode setting.
705
705
706
-
```javascript
706
+
```Typescript
707
707
switchbot
708
708
.discover({ model: "H", quick: true })
709
709
.then((device_list) => {
@@ -725,7 +725,7 @@ If no connection is established with the device, this method automatically estab
725
725
726
726
When the Bot receives this command, the Bot's arm will be put up (retracted) regardless of the mode setting.
727
727
728
-
```javascript
728
+
```Typescript
729
729
switchbot
730
730
.discover({ model: "H", quick: true })
731
731
.then((device_list) => {
@@ -741,11 +741,11 @@ switchbot
741
741
742
742
---
743
743
744
-
## `SwitchbotDeviceWoCurtain` object
744
+
## `WoCurtain` object
745
745
746
-
The `SwitchbotDeviceWoCurtain` object represents a Curtain, which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
746
+
The `WoCurtain` object represents a Curtain, which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
747
747
748
-
Actually, the `SwitchbotDeviceWoCurtain` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
748
+
Actually, the `WoCurtain` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
749
749
750
750
### `open()` method
751
751
@@ -757,7 +757,7 @@ When the Curtain receives this command, the Curtain will open the curtain (0% po
757
757
758
758
The `open()` method receives an optional `mode` parameter. (See [`runToPos()`](#runtopos-method))
759
759
760
-
```javascript
760
+
```Typescript
761
761
switchbot
762
762
.discover({ model: "c", quick: true })
763
763
.then((device_list) => {
@@ -781,7 +781,7 @@ When the Curtain receives this command, the Curtain will close the curtain (100%
781
781
782
782
The `close()` method receives an optional `mode` parameter. (See [`runToPos()`](#runtopos-method))
783
783
784
-
```javascript
784
+
```Typescript
785
785
switchbot
786
786
.discover({ model: "c", quick: true })
787
787
.then((device_list) => {
@@ -803,7 +803,7 @@ If no connection is established with the device, this method automatically estab
803
803
804
804
When the Curtain receives this command, the Curtain will pause.
805
805
806
-
```javascript
806
+
```Typescript
807
807
switchbot
808
808
.discover({ model: "c", quick: true })
809
809
.then((device_list) => {
@@ -836,7 +836,7 @@ When the Curtain receives this command, the Curtain will open the curtain (0% po
836
836
|`percent`| Integer | Required | The percentage of target position (`0-100`). (e.g., `50`) |
837
837
|`mode`| Integer | Optional | The running mode of Curtain. <br/>`0x00` - Performance mode.<br/> `0x01` - Silent mode. <br/>`0xff` - Default. Unspecified, from Curtain's settings. |
838
838
839
-
```javascript
839
+
```Typescript
840
840
switchbot
841
841
.discover({ model: "c", quick: true })
842
842
.then((device_list) => {
@@ -851,11 +851,11 @@ switchbot
851
851
```
852
852
853
853
---
854
-
## `SwitchbotDeviceWoPlugMini` object
854
+
## `WoPlugMini` object
855
855
856
-
The `SwitchbotDeviceWoPlugMini` object represents a PlugMini, which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
856
+
The `WoPlugMini` object represents a PlugMini, which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
857
857
858
-
Actually, the `SwitchbotDeviceWoPlugMini` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
858
+
Actually, the `WoPlugMini` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
0 commit comments