Skip to content

Commit 3086018

Browse files
committed
Now more stable
1 parent 9d2e055 commit 3086018

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

index.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
22

33
// Import packages
4-
const debug = require('debug')('TuyAPI');
54
const dgram = require('dgram');
6-
const forge = require('node-forge');
7-
const retryConnect = require('net-retry-connect');
5+
const net = require('net');
86
const stringOccurrence = require('string-occurrence');
7+
const timeout = require('p-timeout');
8+
const forge = require('node-forge');
9+
const retry = require('retry');
10+
const debug = require('debug')('TuyAPI');
911

1012
// Import requests for devices
1113
const requests = require('./requests.json');
@@ -84,9 +86,10 @@ TuyaDevice.prototype.resolveIds = function () {
8486
}
8587
}
8688

87-
// Todo: add timeout for when IP cannot be found, then reject(with error)
88-
// add IPs to devices in array and return true
89-
return new Promise(resolve => {
89+
debug('Finding IP for devices ' + needIP);
90+
91+
// Add IPs to devices in array
92+
return timeout(new Promise(resolve => { // Timeout
9093
this.listener.on('message', message => {
9194
debug('Received UDP message.');
9295

@@ -111,6 +114,11 @@ TuyaDevice.prototype.resolveIds = function () {
111114
resolve(true);
112115
}
113116
});
117+
}), 10000, () => {
118+
// Have to do this so we exit cleanly
119+
this.listener.close();
120+
this.listener.removeAllListeners();
121+
throw new Error('resolveIds() timed out. Is the device ID correct and is the device powered on?');
114122
});
115123
};
116124

@@ -230,7 +238,7 @@ TuyaDevice.prototype.set = function (options) {
230238
if (options.dps === undefined) {
231239
thisRequest.dps = {1: options.set};
232240
} else {
233-
thisRequest.dps[options.dps.toString] = options.set;
241+
thisRequest.dps[options.dps.toString()] = options.set;
234242
}
235243

236244
debug('Payload: ');
@@ -274,23 +282,34 @@ TuyaDevice.prototype._send = function (ip, buffer) {
274282
debug('Sending this data: ', buffer.toString('hex'));
275283

276284
return new Promise((resolve, reject) => {
277-
retryConnect.to({port: 6668, host: ip, retryOptions: {retries: 5}}, (error, client) => {
278-
if (error) {
285+
const client = new net.Socket();
286+
const connectOperation = retry.operation();
287+
288+
client.on('error', error => {
289+
if (!connectOperation.retry(error)) {
279290
reject(error);
280291
}
292+
});
281293

282-
client.write(buffer);
283-
284-
client.on('data', data => {
285-
client.destroy();
286-
287-
debug('Received data back.');
294+
connectOperation.attempt(() => {
295+
client.connect(6668, ip, () => {
296+
const writeOperation = retry.operation();
297+
writeOperation.attempt(() => {
298+
client.write(buffer);
288299

289-
resolve(data);
290-
});
291-
client.on('error', error => {
292-
error.message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.';
293-
reject(error);
300+
client.on('data', data => {
301+
client.destroy();
302+
debug('Received data back.');
303+
resolve(data);
304+
});
305+
client.on('error', error => {
306+
error.message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.';
307+
console.log('here');
308+
if (!writeOperation.retry(error)) {
309+
reject(error);
310+
}
311+
});
312+
});
294313
});
295314
});
296315
});

package-lock.json

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tuyapi",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "An easy-to-use API for devices that use Tuya's cloud services (currently only supports smart plugs)",
55
"main": "index.js",
66
"scripts": {
@@ -28,6 +28,8 @@
2828
"debug": "^3.1.0",
2929
"net-retry-connect": "^0.1.1",
3030
"node-forge": "^0.7.1",
31+
"p-timeout": "^2.0.1",
32+
"retry": "^0.10.1",
3133
"string-occurrence": "^1.2.0"
3234
},
3335
"devDependencies": {

0 commit comments

Comments
 (0)