Skip to content

Commit

Permalink
Now reuses connection between calls
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Nov 27, 2017
1 parent b11e306 commit 70759b4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 38 deletions.
65 changes: 35 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

// Import packages
const forge = require('node-forge');
const retryConnect = require('net-retry-connect');
const recon = require('@codetheweb/recon');
const waitUntil = require('wait-until');

// Import requests for devices
const requests = require('./requests.json');
Expand Down Expand Up @@ -31,6 +32,10 @@ function TuyaDevice(options) {

// Create cipher object
this.cipher = forge.cipher.createCipher('AES-ECB', this.key);

// Create connection
// this.client = new connect({host: this.ip, port: this.port});
this.client = recon(this.ip, this.port, {retryErrors: ['ECONNREFUSED', 'ECONNRESET']});
}

/**
Expand All @@ -50,17 +55,13 @@ TuyaDevice.prototype.getStatus = function (callback) {
const thisData = Buffer.from(JSON.stringify(requests[this.type].status.command));
const buffer = Buffer.from(requests[this.type].status.prefix + thisData.toString('hex') + requests[this.type].status.suffix, 'hex');

this._send(buffer, (error, result) => {
if (error) {
return callback(error, null);
}

this._send(buffer).then(data => {
// Extract returned JSON
try {
result = result.toString();
result = result.slice(result.indexOf('{'), result.lastIndexOf('}') + 1);
result = JSON.parse(result);
return callback(null, result.dps['1']);
data = data.toString();
data = data.slice(data.indexOf('{'), data.lastIndexOf('}') + 1);
data = JSON.parse(data);
return callback(null, data.dps['1']);
} catch (err) {
return callback(err, null);
}
Expand Down Expand Up @@ -108,37 +109,41 @@ TuyaDevice.prototype.setStatus = function (on, callback) {
const buffer = Buffer.from(thisRequest.prefix + thisData.toString('hex') + thisRequest.suffix, 'hex');

// Send request to change status
this._send(buffer, error => {
if (error) {
return callback(error, null);
}

this._send(buffer).then(data => {
return callback(null, true);
}).catch(err => {
return callback(err, null);
});
};

/**
* Sends a query to the device.
* @private
* @param {Buffer} buffer - buffer of data
* @param {function(error, result)} callback
* @returns {Promise<string>} - returned data
*/
TuyaDevice.prototype._send = function (buffer, callback) {
// The local services of devices seem to be a bit flakey, so we'll retry the connection a couple times
retryConnect.to({port: 6668, host: this.ip, retryOptions: {retries: 5}}, (error, client) => {
if (error) {
return callback(error, null);
}

client.write(buffer);

client.on('data', data => {
client.destroy();
return callback(null, data);
}).on('error', error => {
return callback(error, null);
TuyaDevice.prototype._send = function (buffer) {
const me = this;
return new Promise((resolve, reject) => {
// Wait for device to become available
waitUntil(500, 40, () => {
return me.client.writable;
}, result => {
if (result === false) {
return reject(new Error('timeout'));
}
me.client.write(buffer);
me.client.on('data', data => {
return resolve(data);
});
});
});
};

TuyaDevice.prototype._destroy = function () {
this.client.end();
this.client.destroy();
return true;
};

module.exports = TuyaDevice;
24 changes: 18 additions & 6 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tuyapi",
"version": "1.0.2",
"version": "1.1.0",
"description": "An easy-to-use API for devices that use Tuya's cloud services (currently only supports smart plugs)",
"main": "index.js",
"scripts": {
Expand All @@ -25,8 +25,10 @@
},
"homepage": "https://github.com/codetheweb/tuyapi#readme",
"dependencies": {
"@codetheweb/recon": "^1.0.0",
"net-retry-connect": "^0.1.1",
"node-forge": "^0.7.1"
"node-forge": "^0.7.1",
"wait-until": "0.0.2"
},
"devDependencies": {
"documentation": "^5.3.3",
Expand Down

0 comments on commit 70759b4

Please sign in to comment.