Skip to content

Commit 466e9bd

Browse files
unparagonedcodetheweb
authored andcommitted
ResolveId will resolve id/ip from the other. Added and removed some checking. (#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
1 parent 3a758c2 commit 466e9bd

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

index.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function resolveId(device, options) {
1515
const listener = dgram.createSocket('udp4');
1616
listener.bind(6666);
1717

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

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

3636
const thisId = dataRes.data.gwId;
37-
38-
if (device.id === thisId && dataRes.data) {
37+
const thisIp = dataRes.data.ip;
38+
if ((device.id === thisId || device.ip === thisIp) && dataRes.data) {
3939
// Add IP
4040
device.ip = dataRes.data.ip;
4141

42+
// Add ID
43+
device.id = dataRes.data.gwId;
44+
45+
// Update gwID if required
46+
if (device.gwID === undefined) {
47+
device.gwID = dataRes.data.gwId;
48+
}
49+
4250
// Change product key if neccessary
4351
device.productKey = dataRes.data.productKey;
4452

@@ -73,6 +81,14 @@ function serialResolveId(device, options) {
7381
return promise;
7482
}
7583

84+
function checkIfValidString(input) {
85+
if (input === undefined || typeof input !== typeof 'string' || input.length === 0) {
86+
return false;
87+
}
88+
89+
return true;
90+
}
91+
7692
/**
7793
* Represents a Tuya device.
7894
* @class
@@ -99,16 +115,28 @@ class TuyaDevice extends EventEmitter {
99115
this.device = options;
100116

101117
// Defaults
102-
if (this.device.id === undefined) {
103-
throw new Error('ID is missing from device.');
118+
if (!(checkIfValidString(this.device.id) || checkIfValidString(this.device.ip))) {
119+
throw new Error('ID and IP are missing from device.');
104120
}
105121

106-
if (this.device.gwID === undefined) {
122+
if (!checkIfValidString(this.device.id)) {
123+
debug('ID is missing from device. Run resolveID() to get from IP');
124+
} else if (this.device.gwID === undefined) {
107125
this.device.gwID = this.device.id;
108126
}
109127

110-
if (this.device.key === undefined) {
111-
throw new Error('Encryption key is missing from device.');
128+
if (!checkIfValidString(this.device.ip)) {
129+
debug('IP is missing from device. Run resolveID() to get from ID');
130+
}
131+
132+
if (checkIfValidString(this.device.key)) {
133+
// Create cipher from key
134+
this.device.cipher = new Cipher({
135+
key: this.device.key,
136+
version: this.device.version
137+
});
138+
} else {
139+
debug('Encryption key is missing from device. Only get commands will work');
112140
}
113141

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

126-
// Create cipher from key
127-
this.device.cipher = new Cipher({
128-
key: this.device.key,
129-
version: this.device.version
130-
});
131-
132154
// Private variables
133155

134156
// Socket connected state
@@ -161,8 +183,8 @@ class TuyaDevice extends EventEmitter {
161183
options.timeout = 10;
162184
}
163185

164-
if (this.device.ip !== undefined) {
165-
debug('No IPs to search for');
186+
if (checkIfValidString(this.device.id) && checkIfValidString(this.device.ip)) {
187+
debug('No IPs or IDs to search for');
166188
return Promise.resolve(true);
167189
}
168190

0 commit comments

Comments
 (0)