Skip to content
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
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const TuyaLink = require('./lib/link.js');
* password: 'example-password'});
*/
class TuyaLinkWizard {
constructor({email, password, region = 'us', timezone = 'America/Chicago', apiKey, apiSecret, schema} = {}) {
constructor({email, password, region = 'us', timezone = 'America/Chicago', apiKey, apiSecret, schema, bindAddr} = {}) {
if (!email || !password) {
throw new Error('Both email and password must be provided');
}
Expand All @@ -34,6 +34,7 @@ class TuyaLinkWizard {
this.password = password;
this.region = region;
this.timezone = timezone;
this.bindAddr = bindAddr;

this.api = new TuyaContext({
baseUrl: `https://openapi.tuya${region}.com`,
Expand Down Expand Up @@ -123,7 +124,8 @@ class TuyaLinkWizard {
token: token.token,
secret: token.secret,
ssid,
wifiPassword});
wifiPassword,
bindAddr: this.bindAddr});

// While UDP packets are being sent, start polling for device
debug('Polling cloud for details on token...');
Expand Down
22 changes: 21 additions & 1 deletion lib/link.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const dgram = require('dgram');
const delay = require('delay');
const debug = require('debug')('@tuyapi/link:manual');
const os = require('os');

/**
* A lower level option for linking
Expand All @@ -14,8 +15,10 @@ const debug = require('debug')('@tuyapi/link:manual');
class TuyaLink {
constructor() {
this.abortBroadcasting = false;
this.bindAddr = undefined;
}


/**
* Thin wrapper for this.sendSmartLinkStart()
* and this.sendSmartLinkData(). Unless you
Expand Down Expand Up @@ -66,6 +69,23 @@ class TuyaLink {
throw new Error('Invalid WiFi password');
}

if (options.bindAddr !== undefined)
{
if (typeof options.bindAddr !== 'string')
{
throw new Error('Invalid binding address');
}

const interfaces = os.networkInterfaces();
if (!Object.keys(interfaces).some(name =>
!interfaces[name].some(assigned => assigned.address.toLowerCase() === options.bindAddr.toLowerCase())
))
{
throw new Error('Invalid binding address');
}
this.bindAddr = options.bindAddr;
}

debug('Sending SmartLink initialization packets');

await this.sendSmartLinkStart();
Expand Down Expand Up @@ -335,7 +355,7 @@ class TuyaLink {
this.udpClient.on('listening', function () {
this.setBroadcast(true);
});
this.udpClient.bind(0);
this.udpClient.bind(0, this.bindAddr);
}

// 0-filled buffer
Expand Down