-
Notifications
You must be signed in to change notification settings - Fork 8
/
lookup.js
183 lines (149 loc) · 4.91 KB
/
lookup.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
const ssdp = require("node-ssdp");
const EventEmitter = require("events");
const portscanner = require("portscanner");
const os = require('os');
const arp = require('node-arp');
const Yeelight = require("./yeelight")
const SEARCH_INTERVAL = 2000;
const LOOKUP_INTERVAL = 60*1000;
const PORT_SCAN_TIMEOUT = 10000;
const YEELIGHT_SSDP_PORT = 1982;
const YEELIGHT_PORT = 55443;
//specs: http://www.yeelight.com/download/Yeelight_Inter-Operation_Spec.pdf
class Lookup extends EventEmitter
{
constructor()
{
super();
this.lights = [];
this.ssdp = null;
this.interval = null;
this.init();
}
init()
{
//start lookup
this.lookup();
//start lookup interval
this.interval = setInterval(() =>
{
this.lookup();
},LOOKUP_INTERVAL)
}
lookup()
{
if (this.ssdp)
this.ssdp.stop();
this.ssdp = new ssdp.Client({ ssdpPort: YEELIGHT_SSDP_PORT });
this.ssdp.on('response', (data) =>
{
let light = this.lights.find(light => light.id === data.ID);
if (!light)
{
light = new Yeelight(data);
this.lights.push(light);
//get mac (but it could be that there is no mac because of different (routed) net)
arp.getMAC(light.host, (err, mac) =>
{
light.mac = (!err) ? mac : "";
this.emit('detected', light);
});
}
else
light.updateBySSDPMessage(data);
});
this.ssdp.search('wifi_bulb');
}
/*
searchSSDP(maxRetries)
{
let amount = 0;
let interval = null;
let lightsAtStartup = this.lights.length;
interval = setInterval(() =>
{
if (amount > maxRetries || (lightsAtStartup != this.lights.length && this.lights.length > 0))
{
interval = clearInterval(interval);
return;
}
this.lookup();
//console.log("searching ("+amount+")...");
++amount;
},SEARCH_INTERVAL);
}
*/
findByPortscanning()
{
let interfaces = os.networkInterfaces();
//first: get all ip'S of all network interfaces
let ipAddresses = [];
for (let devName in interfaces)
{
let iface = interfaces[devName];
iface.forEach((alias) =>
{
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
ipAddresses.push(alias.address);
});
}
//remove the last number
for (let i in ipAddresses)
{
let ip = ipAddresses[i];
ip = ip.substr(0,ip.lastIndexOf(".")+1);
ipAddresses[i] = ip;
}
//filter doubles
ipAddresses = ipAddresses.filter((item, i, self) =>
{
return self.lastIndexOf(item) == i;
});
//loop over all Ip'S and check for opend ports (tinkerforge ports)
let checkAmount = ipAddresses.length * 254;
let checks = 0;
return new Promise((resolve,reject) =>
{
for(let i in ipAddresses)
{
for(let t=1;t<255;++t)
{
let ip = ipAddresses[i] + t;
portscanner.checkPortStatus(YEELIGHT_PORT, {host :ip, timeout: PORT_SCAN_TIMEOUT}, (error, status) =>
{
++checks;
if (status == "open")
{
//get mac (but it could be that there is no mac because of different (routed) net)
arp.getMAC(ip, (err, mac) =>
{
//check if already added
let light = null;
if (!err)
light = this.lights.find(light => light.mac === mac);
else
light = this.lights.find(light => light.host === ip);
if (!light)
{
light = new Yeelight();
light.init(ip,YEELIGHT_PORT,(!err) ? mac : "");
this.lights.push(light);
this.emit('detected', light);
}
});
}
//on done -> end
if (checks == checkAmount)
resolve();
});
}
}
});
}
getLights()
{
return this.lights;
}
}
module.exports = Lookup;