-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding mDNS discovery and removing UPnP as it is no longer available. F…
…ixes #221
- Loading branch information
1 parent
1103393
commit d54b973
Showing
10 changed files
with
908 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import EventEmitter from 'events'; | ||
|
||
const DEBUG: boolean = /node-hue-discovery/.test(process.env['NODE_DEBUG'] || ''); | ||
|
||
export class DiscoveryLogger { | ||
|
||
readonly name: string; | ||
|
||
constructor(name: string) { | ||
this.name = name; | ||
} | ||
|
||
static install(name: string, browser: EventEmitter) { | ||
if (DiscoveryLogger.isDebug()) { | ||
const logger = new DiscoveryLogger(name); | ||
logger.log(`Installing discovery logger`); | ||
|
||
browser.on('up', (val: any) => { | ||
logger.log(`**up: ${JSON.stringify(val)}`); | ||
}); | ||
|
||
browser.on('down', (val: any) => { | ||
logger.log(`**down: ${JSON.stringify(val)}`); | ||
}); | ||
|
||
browser.on('error', (val: any) => { | ||
logger.log(`**error: ${JSON.stringify(val)}`); | ||
}); | ||
} | ||
} | ||
|
||
static isDebug(): boolean { | ||
return DEBUG; | ||
} | ||
|
||
log(event: string, payload?: any) { | ||
if (DiscoveryLogger.isDebug()) { | ||
let detail = `${event}`; | ||
|
||
if (payload) { | ||
detail += `\n${JSON.stringify(payload)}`; | ||
} | ||
|
||
console.log(`DiscoveryLogger [${this.name}] :: ${detail}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { expect } from 'chai'; | ||
import { mDNSSearch } from './mDNS'; | ||
|
||
describe('mDNS', function () { | ||
|
||
this.timeout(40 * 1000); | ||
|
||
let mdns: mDNSSearch; | ||
|
||
beforeEach(() => { | ||
mdns = new mDNSSearch(); | ||
}); | ||
|
||
afterEach(() => { | ||
if (mdns) { | ||
mdns.finished(); | ||
} | ||
}) | ||
|
||
it('should discover a bridge on the network', async () => { | ||
// const results = await mdns.search(15 * 1000); | ||
const results = await mdns.search(); | ||
|
||
expect(results).to.be.instanceOf(Array); | ||
expect(results).to.have.length.greaterThan(0); | ||
expect(results[0]).to.have.property('id'); | ||
expect(results[0]).to.have.property('internalipaddress'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { DiscoveryBridgeDefinition } from './discoveryTypes'; | ||
|
||
import * as bonjour from 'bonjour'; | ||
import { DiscoveryLogger } from './DiscoveryLogger'; | ||
import { Service } from 'bonjour'; | ||
|
||
export class mDNSSearch { | ||
|
||
private mdns: any; | ||
|
||
private browser: any; | ||
|
||
constructor() { | ||
this.mdns = bonjour.default({}) | ||
} | ||
|
||
search(timeout?: number): Promise<DiscoveryBridgeDefinition[]> { | ||
const self = this; | ||
|
||
// Queue up a search for services immediately | ||
this.browser = this.mdns.find({ | ||
type: 'hue', | ||
protocol: 'tcp', | ||
}); | ||
DiscoveryLogger.install('mDNS', this.browser); | ||
|
||
return new Promise((resolve) => { | ||
this.browser.start(); | ||
|
||
// Await our timeout before returning any results | ||
setTimeout(() => { | ||
const allServices = self.browser.services; | ||
|
||
let results: DiscoveryBridgeDefinition[] = []; | ||
if (allServices) { | ||
resolve(allServices.map((service: Service) => { | ||
if (service.addresses) { | ||
return { | ||
internalipaddress: service.addresses[0], | ||
id: service.fqdn, | ||
} | ||
} | ||
})) | ||
} | ||
|
||
self.browser.stop(); | ||
resolve(results); | ||
}, timeout || 5000); | ||
}); | ||
} | ||
|
||
finished() { | ||
if (this.mdns) { | ||
this.mdns.destroy(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters