This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (75 loc) · 1.96 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import mdns from 'mdns-js';
import EventEmitter from 'events';
import Debug from 'debug';
const debug = Debug('chromecast-discover');
const MAPPINGS = {
id: 'id',
ve: 'version',
md: 'model',
ic: 'iconPath',
fn: 'friendlyName',
st: 'streaming',
rs: 'appName',
ca: 'certificateAuthority',
// TODO: We don't know what these are
// rm=A390B9102AC7C3A3
// bs=FA8FCA3C73B9
};
class ChromecastDiscover extends EventEmitter {
constructor() {
super();
this.ready = false;
const mdnsType = mdns.tcp('googlecast');
const browser = new mdns.createBrowser(mdnsType); // eslint-disable-line new-cap
this.browser = browser;
// Kick off a discovery when we start up
browser.on('ready', () => {
debug('ready');
this.ready = true;
});
browser.on('update', (data) => {
debug('update', data);
const type = data.type[0];
if (type.name !== 'googlecast' || type.protocol !== 'tcp') {
debug('skipped: not a chromecast');
return;
}
// Sometimes the chromecast sends out an unsolicited record
if (!data.txt) {
debug('skipped: no txt record');
return;
}
const device = {
host: data.host,
addresses: data.addresses,
port: data.port,
fullname: data.fullname,
};
const items = data.txt;
items.forEach((item) => {
const md = item.match(/^([^=]+)=(.*)$/);
const key = md[1];
const value = md[2];
const name = MAPPINGS[key];
if (!name) {
return;
}
device[name] = value;
});
debug(device);
this.emit('online', device);
});
}
start() {
const self = this;
if (!this.ready) {
debug('not ready');
setTimeout(() => self.start(), 250);
return;
}
debug('send discover');
this.browser.discover();
}
}
const chromecastDiscover = new ChromecastDiscover();
export default chromecastDiscover;