Comparing version 1.0.2 to 1.1.0
65
index.js
@@ -5,3 +5,4 @@ 'use strict'; | ||
const forge = require('node-forge'); | ||
const retryConnect = require('net-retry-connect'); | ||
const recon = require('@codetheweb/recon'); | ||
const waitUntil = require('wait-until'); | ||
@@ -35,2 +36,6 @@ // Import requests for devices | ||
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']}); | ||
} | ||
@@ -55,13 +60,9 @@ | ||
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) { | ||
@@ -114,8 +115,6 @@ return callback(err, null); | ||
// 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); | ||
}); | ||
@@ -128,18 +127,18 @@ }; | ||
* @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); | ||
}); | ||
}); | ||
@@ -149,2 +148,8 @@ }); | ||
TuyaDevice.prototype._destroy = function () { | ||
this.client.end(); | ||
this.client.destroy(); | ||
return true; | ||
}; | ||
module.exports = TuyaDevice; |
{ | ||
"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)", | ||
@@ -28,4 +28,6 @@ "main": "index.js", | ||
"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" | ||
}, | ||
@@ -32,0 +34,0 @@ "devDependencies": { |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
562122
150
0
4
+ Added@codetheweb/recon@^1.0.0
+ Addedwait-until@0.0.2
+ Added@codetheweb/recon@1.2.0(transitive)
+ Addedwait-until@0.0.2(transitive)