Skip to content

Commit

Permalink
feat(ping): Allow options to ping to set broadcast address
Browse files Browse the repository at this point in the history
This allows users to specify the broadcast address to send the ping to.

This allows KraigM/homebridge-harmonyhub#105 to be addressed.
  • Loading branch information
stblassitude committed Dec 19, 2017
1 parent 2a8f130 commit c929b7b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ discover.start()
// discover.stop()
```

## Options to HarmonyHubDiscover

HarmonyHubDiscover takes an optional `options` argument that allows you to specify parameters to the discovery packet that is sent out:
* `address`: the broadcast address to send the discovery packet to, defaults to `255.255.255.255`. On systems with multiple interfaces, you need to specify the broadcast address of the network your Hub is connected to. For example, if the Hub is connected to 192.168.1.0/24, specify address `192.168.1.255`.
* `interval`: the number of milliseconds between sending out discovery packets. Defaults to `1000`.
* `port`: The port to send the discovery packet to. Defaults to `5224`.

Example:
```javascript
var discover = new HarmonyHubDiscover(61991, { 'address': '192.168.1.255' })
```

## Control your hub
After looking up your Harmony hub, use [harmonyhubjs-client](https://github.com/swissmanu/harmonyhubjs-client) to control it.

Expand Down
4 changes: 2 additions & 2 deletions lib/explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ function executeCleanUp () {
})
}

function Explorer (port) {
function Explorer (port, pingOptions) {
debug('Explorer(' + port + ')')
EventEmitter.call(this)

this.knownHubs = {}
this.port = port
this.ping = new Ping(this.port)
this.ping = new Ping(this.port, pingOptions)
}
util.inherits(Explorer, EventEmitter)

Expand Down
13 changes: 7 additions & 6 deletions lib/ping.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
var debug = require('debug')('harmonyhubjs:discover:ping')
var dgram = require('dgram')

function Ping (portToAnnounce) {
debug('Ping(' + portToAnnounce + ')')
function Ping (portToAnnounce, options) {
options = options || {}
debug('Ping(' + portToAnnounce + ', ' + JSON.stringify(options) + ')')

this.socket = dgram.createSocket('udp4')
this.port = 5224
this.address = '255.255.255.255'
this.interval = 1000
this.port = options.port || 5224
this.address = options.address || '255.255.255.255'
this.interval = options.interval || 1000
this.message = '_logitech-reverse-bonjour._tcp.local.\n' + portToAnnounce
this.messageBuffer = new Buffer(this.message)

Expand Down Expand Up @@ -37,7 +38,7 @@ Ping.prototype.start = function start () {

Ping.prototype.stop = function stop () {
debug('stop()')
clearTimeout(this.intervalToken)
clearInterval(this.intervalToken)
this.intervalToken = undefined
this.socket.close()
}
Expand Down

0 comments on commit c929b7b

Please sign in to comment.