Skip to content

Commit

Permalink
Now more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Apr 14, 2018
1 parent 9d2e055 commit 3086018
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 deletions.
59 changes: 39 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

// Import packages
const debug = require('debug')('TuyAPI');
const dgram = require('dgram');
const forge = require('node-forge');
const retryConnect = require('net-retry-connect');
const net = require('net');
const stringOccurrence = require('string-occurrence');
const timeout = require('p-timeout');
const forge = require('node-forge');
const retry = require('retry');
const debug = require('debug')('TuyAPI');

// Import requests for devices
const requests = require('./requests.json');
Expand Down Expand Up @@ -84,9 +86,10 @@ TuyaDevice.prototype.resolveIds = function () {
}
}

// Todo: add timeout for when IP cannot be found, then reject(with error)
// add IPs to devices in array and return true
return new Promise(resolve => {
debug('Finding IP for devices ' + needIP);

// Add IPs to devices in array
return timeout(new Promise(resolve => { // Timeout
this.listener.on('message', message => {
debug('Received UDP message.');

Expand All @@ -111,6 +114,11 @@ TuyaDevice.prototype.resolveIds = function () {
resolve(true);
}
});
}), 10000, () => {
// Have to do this so we exit cleanly
this.listener.close();
this.listener.removeAllListeners();
throw new Error('resolveIds() timed out. Is the device ID correct and is the device powered on?');
});
};

Expand Down Expand Up @@ -230,7 +238,7 @@ TuyaDevice.prototype.set = function (options) {
if (options.dps === undefined) {
thisRequest.dps = {1: options.set};
} else {
thisRequest.dps[options.dps.toString] = options.set;
thisRequest.dps[options.dps.toString()] = options.set;
}

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

return new Promise((resolve, reject) => {
retryConnect.to({port: 6668, host: ip, retryOptions: {retries: 5}}, (error, client) => {
if (error) {
const client = new net.Socket();
const connectOperation = retry.operation();

client.on('error', error => {
if (!connectOperation.retry(error)) {
reject(error);
}
});

client.write(buffer);

client.on('data', data => {
client.destroy();

debug('Received data back.');
connectOperation.attempt(() => {
client.connect(6668, ip, () => {
const writeOperation = retry.operation();
writeOperation.attempt(() => {
client.write(buffer);

resolve(data);
});
client.on('error', error => {
error.message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.';
reject(error);
client.on('data', data => {
client.destroy();
debug('Received data back.');
resolve(data);
});
client.on('error', error => {
error.message = 'Error communicating with device. Make sure nothing else is trying to control it or connected to it.';
console.log('here');
if (!writeOperation.retry(error)) {
reject(error);
}
});
});
});
});
});
Expand Down
26 changes: 20 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuyapi",
"version": "2.0.1",
"version": "2.0.2",
"description": "An easy-to-use API for devices that use Tuya's cloud services (currently only supports smart plugs)",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -28,6 +28,8 @@
"debug": "^3.1.0",
"net-retry-connect": "^0.1.1",
"node-forge": "^0.7.1",
"p-timeout": "^2.0.1",
"retry": "^0.10.1",
"string-occurrence": "^1.2.0"
},
"devDependencies": {
Expand Down

0 comments on commit 3086018

Please sign in to comment.