Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "local_ip_address" option when discovering devices #2

Merged
merged 3 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare class Broadlink extends EventEmitter {
/**
* Start the discovery procedure
*/
discover(): void;
discover(local_ip_address?: string): void;
}

declare namespace Broadlink {
Expand Down Expand Up @@ -106,4 +106,4 @@ declare namespace Broadlink {
*/
on(event: 'rawData', listener: (data: Buffer) => void): this;
}
}
}
33 changes: 19 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,23 @@ Broadlink.prototype.genDevice = function(devtype, host, mac) {
}
}

Broadlink.prototype.discover = function() {
Broadlink.prototype.discover = function(local_ip_address) {
self = this;
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
if (local_ip_address) {
var address = local_ip_address;
} else {
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
var address = addresses[0];
}
var address = addresses[0].split('.');
var cs = dgram.createSocket({ type: 'udp4', reuseAddr: true });
cs.on('listening', function() {
cs.setBroadcast(true);
Expand Down Expand Up @@ -153,10 +157,11 @@ Broadlink.prototype.discover = function() {
packet[0x11] = now.getDay();
packet[0x12] = now.getDate();
packet[0x13] = now.getMonth();
packet[0x18] = parseInt(address[0]);
packet[0x19] = parseInt(address[1]);
packet[0x1a] = parseInt(address[2]);
packet[0x1b] = parseInt(address[3]);
var address_parts = address.split('.');
packet[0x18] = parseInt(address_parts[0]);
packet[0x19] = parseInt(address_parts[1]);
packet[0x1a] = parseInt(address_parts[2]);
packet[0x1b] = parseInt(address_parts[3]);
packet[0x1c] = port & 0xff;
packet[0x1d] = port >> 8;
packet[0x26] = 6;
Expand Down Expand Up @@ -203,7 +208,7 @@ Broadlink.prototype.discover = function() {
//console.log('===Server Closed');
});

cs.bind();
cs.bind(0, address);

setTimeout(function() {
cs.close();
Expand Down Expand Up @@ -679,4 +684,4 @@ device.prototype.rm = function() {
break;
}
});
}
}