Skip to content

Commit

Permalink
ResolveId will resolve id/ip from the other. Added and removed some c…
Browse files Browse the repository at this point in the history
…hecking. (#141)

* resolveID will now also resolve ID if IP provided

* Added valid string check for id/ip/key. Remove throw error for id/key

* Fix lint issues
  • Loading branch information
unparagoned authored and codetheweb committed Feb 10, 2019
1 parent 3a758c2 commit 466e9bd
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function resolveId(device, options) {
const listener = dgram.createSocket('udp4');
listener.bind(6666);

debug('Finding IP for device ' + device.id);
debug(`Finding missing IP: ${device.ip} or Device ID: ${device.id}`);

// Find IP for device
return timeout(new Promise((resolve, reject) => { // Timeout
Expand All @@ -34,11 +34,19 @@ function resolveId(device, options) {
debug(dataRes.data);

const thisId = dataRes.data.gwId;

if (device.id === thisId && dataRes.data) {
const thisIp = dataRes.data.ip;
if ((device.id === thisId || device.ip === thisIp) && dataRes.data) {
// Add IP
device.ip = dataRes.data.ip;

// Add ID
device.id = dataRes.data.gwId;

// Update gwID if required
if (device.gwID === undefined) {
device.gwID = dataRes.data.gwId;
}

// Change product key if neccessary
device.productKey = dataRes.data.productKey;

Expand Down Expand Up @@ -73,6 +81,14 @@ function serialResolveId(device, options) {
return promise;
}

function checkIfValidString(input) {
if (input === undefined || typeof input !== typeof 'string' || input.length === 0) {
return false;
}

return true;
}

/**
* Represents a Tuya device.
* @class
Expand All @@ -99,16 +115,28 @@ class TuyaDevice extends EventEmitter {
this.device = options;

// Defaults
if (this.device.id === undefined) {
throw new Error('ID is missing from device.');
if (!(checkIfValidString(this.device.id) || checkIfValidString(this.device.ip))) {
throw new Error('ID and IP are missing from device.');
}

if (this.device.gwID === undefined) {
if (!checkIfValidString(this.device.id)) {
debug('ID is missing from device. Run resolveID() to get from IP');
} else if (this.device.gwID === undefined) {
this.device.gwID = this.device.id;
}

if (this.device.key === undefined) {
throw new Error('Encryption key is missing from device.');
if (!checkIfValidString(this.device.ip)) {
debug('IP is missing from device. Run resolveID() to get from ID');
}

if (checkIfValidString(this.device.key)) {
// Create cipher from key
this.device.cipher = new Cipher({
key: this.device.key,
version: this.device.version
});
} else {
debug('Encryption key is missing from device. Only get commands will work');
}

if (this.device.port === undefined) {
Expand All @@ -123,12 +151,6 @@ class TuyaDevice extends EventEmitter {
this.device.persistentConnection = false;
}

// Create cipher from key
this.device.cipher = new Cipher({
key: this.device.key,
version: this.device.version
});

// Private variables

// Socket connected state
Expand Down Expand Up @@ -161,8 +183,8 @@ class TuyaDevice extends EventEmitter {
options.timeout = 10;
}

if (this.device.ip !== undefined) {
debug('No IPs to search for');
if (checkIfValidString(this.device.id) && checkIfValidString(this.device.ip)) {
debug('No IPs or IDs to search for');
return Promise.resolve(true);
}

Expand Down

0 comments on commit 466e9bd

Please sign in to comment.