Skip to content

Commit 9aaf2d0

Browse files
committed
added a timeout to discovery, extended documentation
1 parent c8ece50 commit 9aaf2d0

4 files changed

Lines changed: 92 additions & 23 deletions

File tree

README.md

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@ Library to talk to IKEA Trådfri Gateways without external binaries
77
*Requires NodeJS >= 6.x*
88

99
## Example usage
10-
### Discover Tradfri Gateway
1110

12-
You can discover one (currently its not possible to discover multiple gateways) Tradfri Gateways.
13-
14-
```TS
11+
### Discover a Gateway
12+
```js
1513
import { discoverGateway } from "node-tradfri-client";
1614

17-
discoverGateway().then(result => console.log(result));
15+
// later:
16+
const result = await discoverGateway();
1817
```
1918

20-
If the search is successful it will return
21-
22-
```
23-
{ name: 'gw-a0cc2bf8535d',
24-
version: '1.3.14',
25-
addresses: [ '10.0.0.94', 'fe80::a2cc:2bff:fef8:535d' ] }
19+
The `result` variable has the following properties:
20+
```js
21+
{
22+
name: 'gw-abcdef012345',
23+
version: '1.3.14',
24+
addresses: [
25+
// array of strings with IP addresses
26+
]
27+
}
2628
```
2729

2830

@@ -99,12 +101,44 @@ tradfri.destroy();
99101

100102
## Detailed usage
101103

104+
### Import the necessary methods
102105
```TS
103106
const tradfriLib = require("node-tradfri-client");
107+
// for normal usage:
104108
const TradfriClient = tradfriLib.TradfriClient;
109+
// for discovery:
110+
const discoverGateway = tradfriLib.discoverGateway;
111+
105112
// or with the new import syntax
106-
import { TradfriClient /*, more imports */ } from "node-tradfri-client";
113+
import { discoverGateway, TradfriClient /*, more imports */ } from "node-tradfri-client";
114+
```
115+
116+
### Auto-detect your gateway
117+
You can automatically discover a Trådfri gateway on the local network with the `discoverGateway` method. Discovery will return the first gateway found, finding multiple ones is not possible yet.
107118

119+
The method has the following signatures:
120+
```TS
121+
const discovered = await discoverGateway();
122+
const discovered = await discoverGateway(timeout: number);
123+
const discovered = await discoverGateway(false);
124+
```
125+
The timeout parameter is the time in milliseconds (default 10000) the discovery will run before returning `null` to indicate that no gateway was found. By passing a negative value or `false` you can instruct the discovery to run *forever*.
126+
127+
The return value is of the type `DiscoveredGateway` which looks as follows:
128+
```ts
129+
{
130+
// hostname of the gateway, has the form "gw-abcdef012345"
131+
name: string,
132+
// firmware version of the gateway
133+
version: string,
134+
// array of IP addresses the gateway responds to
135+
addresses: string[],
136+
}
137+
```
138+
139+
140+
### Create a client instance
141+
```TS
108142
// one of the following
109143
const tradfri = new TradfriClient(hostname: string);
110144
const tradfri = new TradfriClient(hostname: string, customLogger: LoggerFunction);

build/lib/discovery.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ export interface DiscoveredGateway {
33
version: string;
44
addresses: string[];
55
}
6-
export declare function discoverGateway(): Promise<DiscoveredGateway>;
6+
/**
7+
* Auto-discover a tradfri gateway on the network.
8+
* @param timeout (optional) Time in milliseconds to wait for a response. Default 10000.
9+
* Pass false or a negative number to explicitly wait forever.
10+
*/
11+
export declare function discoverGateway(timeout?: number | false): Promise<DiscoveredGateway>;

build/lib/discovery.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
const bonjourPackage = require("bonjour");
4-
const bonjour = bonjourPackage();
5-
function discoverGateway() {
4+
let bonjour;
5+
/**
6+
* Auto-discover a tradfri gateway on the network.
7+
* @param timeout (optional) Time in milliseconds to wait for a response. Default 10000.
8+
* Pass false or a negative number to explicitly wait forever.
9+
*/
10+
function discoverGateway(timeout = 10000) {
11+
if (bonjour == null)
12+
bonjour = bonjourPackage();
13+
let timer;
614
return new Promise((resolve, reject) => {
715
const mdnsBrowser = bonjour.findOne({ type: "coap", protocol: "udp" }, (service) => {
8-
if (!service || !service.txt || !service.name.startsWith("gw-")) {
16+
if (!service || !service.txt || !service.name.startsWith("gw-"))
917
return;
10-
}
18+
if (timer != null)
19+
clearTimeout(timer);
1120
const foundDevice = {
1221
name: service.name,
1322
version: service.txt.version,
1423
addresses: service.addresses,
1524
};
16-
mdnsBrowser.stop();
1725
resolve(foundDevice);
1826
});
27+
if (typeof timeout === "number" && timeout > 0) {
28+
timer = setTimeout(() => {
29+
if (mdnsBrowser != null)
30+
mdnsBrowser.stop();
31+
resolve(null);
32+
}, timeout);
33+
}
1934
mdnsBrowser.start();
2035
});
2136
}

src/lib/discovery.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import * as bonjourPackage from "bonjour";
2+
let bonjour: bonjourPackage.Bonjour;
23

34
export interface DiscoveredGateway {
45
name: string;
56
version: string;
67
addresses: string[];
78
}
89

9-
export function discoverGateway(): Promise<DiscoveredGateway> {
10-
const bonjour = bonjourPackage();
10+
/**
11+
* Auto-discover a tradfri gateway on the network.
12+
* @param timeout (optional) Time in milliseconds to wait for a response. Default 10000.
13+
* Pass false or a negative number to explicitly wait forever.
14+
*/
15+
export function discoverGateway(timeout: number | false = 10000): Promise<DiscoveredGateway> {
16+
if (bonjour == null) bonjour = bonjourPackage();
17+
let timer: NodeJS.Timer;
1118

1219
return new Promise((resolve, reject) => {
1320
const mdnsBrowser = bonjour.findOne(
1421
{ type: "coap", protocol: "udp" },
1522
(service: any) => {
16-
if (!service || !service.txt || !service.name.startsWith("gw-")) {
17-
return;
18-
}
23+
if (!service || !service.txt || !service.name.startsWith("gw-")) return;
24+
25+
if (timer != null) clearTimeout(timer);
1926
const foundDevice = {
2027
name: service.name,
2128
version: service.txt.version,
@@ -24,6 +31,14 @@ export function discoverGateway(): Promise<DiscoveredGateway> {
2431
resolve(foundDevice);
2532
},
2633
);
34+
35+
if (typeof timeout === "number" && timeout > 0) {
36+
timer = setTimeout(() => {
37+
if (mdnsBrowser != null) mdnsBrowser.stop();
38+
resolve(null);
39+
}, timeout);
40+
}
41+
2742
mdnsBrowser.start();
2843
});
2944
}

0 commit comments

Comments
 (0)