Skip to content

Commit d7839b1

Browse files
authored
Add an option bindAddr which can be passed to TuyaLinkWizard (#15)
Co-authored-by: Mark <[email protected]>
1 parent c9dc8db commit d7839b1

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const TuyaLink = require('./lib/link.js');
2525
* password: 'example-password'});
2626
*/
2727
class TuyaLinkWizard {
28-
constructor({email, password, region = 'us', timezone = 'America/Chicago', apiKey, apiSecret, schema} = {}) {
28+
constructor({email, password, region = 'us', timezone = 'America/Chicago', apiKey, apiSecret, schema, bindAddr} = {}) {
2929
if (!email || !password) {
3030
throw new Error('Both email and password must be provided');
3131
}
@@ -34,6 +34,7 @@ class TuyaLinkWizard {
3434
this.password = password;
3535
this.region = region;
3636
this.timezone = timezone;
37+
this.bindAddr = bindAddr;
3738

3839
this.api = new TuyaContext({
3940
baseUrl: `https://openapi.tuya${region}.com`,
@@ -123,7 +124,8 @@ class TuyaLinkWizard {
123124
token: token.token,
124125
secret: token.secret,
125126
ssid,
126-
wifiPassword});
127+
wifiPassword,
128+
bindAddr: this.bindAddr});
127129

128130
// While UDP packets are being sent, start polling for device
129131
debug('Polling cloud for details on token...');

lib/link.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const dgram = require('dgram');
22
const delay = require('delay');
33
const debug = require('debug')('@tuyapi/link:manual');
4+
const os = require('os');
45

56
/**
67
* A lower level option for linking
@@ -14,8 +15,10 @@ const debug = require('debug')('@tuyapi/link:manual');
1415
class TuyaLink {
1516
constructor() {
1617
this.abortBroadcasting = false;
18+
this.bindAddr = undefined;
1719
}
1820

21+
1922
/**
2023
* Thin wrapper for this.sendSmartLinkStart()
2124
* and this.sendSmartLinkData(). Unless you
@@ -66,6 +69,23 @@ class TuyaLink {
6669
throw new Error('Invalid WiFi password');
6770
}
6871

72+
if (options.bindAddr !== undefined)
73+
{
74+
if (typeof options.bindAddr !== 'string')
75+
{
76+
throw new Error('Invalid binding address');
77+
}
78+
79+
const interfaces = os.networkInterfaces();
80+
if (!Object.keys(interfaces).some(name =>
81+
!interfaces[name].some(assigned => assigned.address.toLowerCase() === options.bindAddr.toLowerCase())
82+
))
83+
{
84+
throw new Error('Invalid binding address');
85+
}
86+
this.bindAddr = options.bindAddr;
87+
}
88+
6989
debug('Sending SmartLink initialization packets');
7090

7191
await this.sendSmartLinkStart();
@@ -335,7 +355,7 @@ class TuyaLink {
335355
this.udpClient.on('listening', function () {
336356
this.setBroadcast(true);
337357
});
338-
this.udpClient.bind(0);
358+
this.udpClient.bind(0, this.bindAddr);
339359
}
340360

341361
// 0-filled buffer

0 commit comments

Comments
 (0)