-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdns.js
53 lines (44 loc) · 1.13 KB
/
mdns.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const EventEmitter = require('events');
const mdns = require('mdns');
const SERVICE = 'tivo-videos';
const TiVo = require('./tivo.js');
class TiVoDiscovery {
name;
id;
record;
host;
address;
mak;
constructor(data, mak) {
this.name = data.name;
this.id = data.fullname;
this.record = data.txtRecord;
this.host = data.host;
this.address = data.addresses[0],
this.addresses = data.addresses,
this.mak = mak;
}
connect(options, mak) {
const tivo = new TiVo(this.address, mak || this.mak, this.name, options);
return tivo;
}
toString() {
return `TiVoDiscovery Device ${this.name} [${this.address}]`
}
}
class TiVoBonjour extends EventEmitter {
constructor(mediaAccessKey) {
super();
this.mak = mediaAccessKey;
this.browser = mdns.createBrowser(mdns.tcp(SERVICE));
this.browser.on('serviceUp', s=>this.#discover(s));
this.devices = {};
this.browser.start();
}
#discover(servicedata) {
const device = new TiVoDiscovery(servicedata, this.mak);
this.devices[device.name] = device;
this.emit('update', device);
}
}
module.exports = TiVoBonjour;