-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscoverDevices.js
86 lines (66 loc) · 2.03 KB
/
discoverDevices.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
// @file discoverDevices.js
// start using `node discoverDevices.js`
// utility to list all local homeding devices using a mdns query.
import MDNS from 'multicast-dns';
const mDNS = MDNS();
mDNS.on('response', function (response) {
// console.log('response:', response);
const all = [...response.answers, ...response.additionals];
console.log("");
all
.forEach((a) => {
if (a.type === 'A') {
console.log(">A-" + a.name, a.data);
} else if (a.type === 'PTR') {
console.log(">PTR-", a.name, a.data);
} else if (a.type === 'SRV') {
console.log(">" + a.type, a.data.target + ':' + a.data.port);
} else if (a.type === 'TXT') {
console.log(">TXT-" + a.name, String(a.data));
} else if (a.type === 'NSEC') {
// not of interrest.
} else {
console.log(">" + a.type, a.data);
}
});
var isHomeDingDevice = all
.filter((a) => (a.type == 'PTR'))
.filter((a) => (a.name == '_homeding._tcp.local'))
.length > 0;
if (isHomeDingDevice) {
var hdd = {
host: '',
target: '',
room: '',
title: '',
path: ''
};
// console.log('HomeDing Device!');
// console.log('response:', response);
all
.filter((a) => (a.type == 'SRV'))
.forEach((a) => {
// console.log("SRV", a.data);
hdd.target = a.data.target;
hdd.host = hdd.target.replace(/\.local/, '');
});
all
.filter((a) => (a.type == 'TXT'))
// .filter((a) => (a.name == '_homeding._tcp.local'))
.forEach((a) => {
var d = '';
String(a.data)
.split(',')
.forEach(e => {
var p = e.split('=');
hdd[p[0]] = p[1];
});
});
console.log('http://' + hdd.host, JSON.stringify(hdd));
} // if
})
// query for homeding devices:
mDNS.query([{ name: '_homeding._tcp.local', type: 'PTR' }])
setTimeout(function () {
// mDNS.destroy();
}, 60*1000);