Skip to content

Commit 720c80b

Browse files
maszaajuliuscc
authored andcommitted
feat: Added support for user defined host UDP port and IP address
Modified discover function signatures so that `options` is the first parameter and it can also be a number (`timeout`) to provide backwards compatibility fix #20
1 parent 79a3877 commit 720c80b

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,26 @@ The `heos` object has two ways of finding devices and one way to connect to a de
7676
- `heos.discoverOneDevice()`
7777
- `heos.connect()`
7878

79-
#### heos.discoverDevices(timeout, onDiscover[, onTimeout])
79+
#### heos.discoverDevices(options, onDiscover[, onTimeout])
8080

81-
- `timeout`: number
81+
- `options`: { timeout?: number, port?: number, address?: string } || number
8282
- `onDiscover`: (address: string) => void
8383
- `onTimeout`: (addresses: string[]) => void
8484

85-
Tries to discover all available HEOS devices in the network. When `timeout` milliseconds have passed the search will end. Every time a HEOS device is discovered `onDiscover(address)` will be triggered, where `address` is the ip-address of the device found. When the search ends `onTimeout(addresses[])` will be triggered with an array with all the devices found.
85+
Tries to discover all available HEOS devices in the network. `port` and `address` of `options` is for connecting to a user specified network interface. When `options.timeout` or `options` (type `number`) milliseconds have passed the search will end. Every time a HEOS device is discovered `onDiscover(address)` will be triggered, where `address` is the ip-address of the device found. When the search ends `onTimeout(addresses[])` will be triggered with an array with all the devices found.
8686

8787
The function does not return a value.
8888

8989
```js
90-
heos.discoverDevices(5000, console.log)
91-
// Logs out the addresses of every HEOS device in the network
90+
heos.discoverDevices({ timeout: 3000 }, console.log, () => {})
91+
// Logs out the addresses of every HEOS device in the network, and will end search after 3 seconds
9292
```
9393

94-
#### heos.discoverOneDevice([timeout])
94+
#### heos.discoverOneDevice([options])
9595

96-
- `timeout`: number
96+
- `options`: { timeout?: number, port?: number, address?: string } || number
9797

98-
Finds one HEOS device in the network. A promise is returned that will resolve when the first device is found, or reject if no devices are found before `timeout` milliseconds have passed. If the function resolves it will resolve with the address of the HEOS device found.
98+
Finds one HEOS device in the network. `port` and `address` of `options` is for connecting to a user specified network interface. A promise is returned that will resolve when the first device is found, or reject if no devices are found before `options.timeout` or `options` (type `number`) milliseconds have passed. If the function resolves it will resolve with the address of the HEOS device found.
9999

100100
`heos.discoverDevices()` is used under the hood
101101

@@ -104,11 +104,11 @@ heos.discoverOneDevice().then(console.log)
104104
// Logs out the address of a HEOS device
105105
```
106106

107-
#### heos.discoverAndConnect([timeout])
107+
#### heos.discoverAndConnect([options])
108108

109-
- `timeout`: number
109+
- `options`: { timeout?: number, port?: number, address?: string } || number
110110

111-
Finds one HEOS device in the network, and connects to it. A promise is returned that will resolve when the first device is found, or reject if no devices are found before `timeout` milliseconds have passed. If the function resolves it will resolve with a HeosConnection.
111+
Finds one HEOS device in the network, and connects to it. `port` and `address` of `options` is for connecting to a user specified network interface. A promise is returned that will resolve when the first device is found, or reject if no devices are found before `options.timeout` or `options` (type `number`) milliseconds have passed. If the function resolves it will resolve with a HeosConnection.
112112

113113
```js
114114
heos.discoverAndConnect().then(console.log)

__tests__/connection/discover.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ describe('discoverOneDevice()', () => {
1111
return expect(discoverOneDevice()).resolves.toEqual('192.168.0.5')
1212
})
1313

14+
test('One device can be discovered with options', () => {
15+
expect.assertions(1)
16+
return expect(discoverOneDevice({ address: '192.168.0.1', timeout: 10 })).resolves.toEqual(
17+
'192.168.0.5'
18+
)
19+
})
20+
1421
test('Discover fails when no devices are found', () => {
1522
expect.assertions(1)
1623
// @ts-ignore
1724
dgram.__setOn(() => {})
1825

19-
return expect(discoverOneDevice(10)).rejects.toEqual('No devices found')
26+
return expect(discoverOneDevice({ timeout: 10 })).rejects.toEqual('No devices found')
2027
})
2128

2229
test('discoverOneDevice only returns one address even if multiple devices are found', () => {

src/connection/discover.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,31 @@ type RInfo = {
2020
size: number
2121
}
2222

23+
export type DiscoverOptions = {
24+
timeout?: number
25+
port?: number
26+
address?: string
27+
}
28+
2329
const defaultTimeout = 5000
2430

2531
/**
2632
* Tries to discover all available HEOS devices in the network.
27-
* @param timeout Will stop searching for HEOS devices when `timeout` milliseconds has ellapsed.
33+
* @param options Options for discovering devices.
2834
* @param onDiscover Will trigger every time a HEOS device is discovered.
2935
* @param onTimeout Will trigger when `timeout` has ellapsed.
3036
*/
3137
export function discoverDevices(
32-
timeout: number = defaultTimeout,
38+
options: DiscoverOptions | number,
3339
onDiscover: (address: string) => void,
3440
onTimeout?: (addresses: string[]) => void
3541
): () => void {
42+
const timeout: number =
43+
typeof options === 'number' ? options || defaultTimeout : options.timeout || defaultTimeout
44+
3645
const socket = createSocket('udp4')
37-
socket.bind()
46+
47+
typeof options !== 'number' ? socket.bind(options.port, options.address) : socket.bind()
3848

3949
socket.on('listening', () => {
4050
socket.send(message, 1900, '239.255.255.250')
@@ -65,10 +75,12 @@ export function discoverDevices(
6575

6676
/**
6777
* Finds one HEOS device in the network.
68-
* @param timeout Will stop searching for a HEOS device when `timeout` milliseconds has ellapsed.
78+
* @param options Options for discovering a device.
6979
* @returns A promise that will resolve when the first device is found, or reject if no devices are found before `timeout` milliseconds have passed. If the function resolves it will resolve with the address of the HEOS device found.
7080
*/
71-
export function discoverOneDevice(timeout: number = defaultTimeout): Promise<string> {
81+
export function discoverOneDevice(
82+
options: DiscoverOptions | number = defaultTimeout
83+
): Promise<string> {
7284
return new Promise((resolve, reject) => {
7385
let oneDiscovered: boolean = false
7486

@@ -88,18 +100,20 @@ export function discoverOneDevice(timeout: number = defaultTimeout): Promise<str
88100
}
89101
}
90102

91-
const quit = discoverDevices(timeout, onDiscover, onTimeout)
103+
const quit = discoverDevices(options, onDiscover, onTimeout)
92104
})
93105
}
94106

95107
/**
96108
* Finds one HEOS device in the network, and connects to it.
97-
* @param timeout Will stop searching for a HEOS device when `timeout` milliseconds has ellapsed.
109+
* @param options Options for discovering a device.
98110
* @returns A promise that will resolve when the first device is found, or reject if no devices are found before `timeout` milliseconds have passed. If the function resolves it will resolve with a HeosConnection.
99111
*/
100-
export function discoverAndConnect(timeout: number = defaultTimeout): Promise<HeosConnection> {
112+
export function discoverAndConnect(
113+
options: DiscoverOptions | number = defaultTimeout
114+
): Promise<HeosConnection> {
101115
return new Promise((resolve, reject) => {
102-
discoverOneDevice(timeout)
116+
discoverOneDevice(options)
103117
.then(connect)
104118
.then(resolve)
105119
.catch(reject)

0 commit comments

Comments
 (0)