From 50ea191c87f2cb9d5a41ba6bf3d6edffd15683ce Mon Sep 17 00:00:00 2001 From: Robert Jepson Date: Sun, 3 Dec 2017 01:50:06 +0000 Subject: [PATCH 1/2] Update index.js --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index b573e08..fd1df87 100644 --- a/index.js +++ b/index.js @@ -53,6 +53,7 @@ TuyaDevice.prototype.getStatus = function (callback) { // Create byte buffer from hex data const thisData = Buffer.from(JSON.stringify(requests[this.type].status.command)); + const prefixSum = thisData.toString('hex').length + requests[this.type].status.suffix.length; const buffer = Buffer.from(requests[this.type].status.prefix + thisData.toString('hex') + requests[this.type].status.suffix, 'hex'); this._send(buffer).then(data => { From a8a37784e8d5e0d324d90e7e2a6c96ee0ac82e33 Mon Sep 17 00:00:00 2001 From: Robert Jepson Date: Sun, 3 Dec 2017 16:56:10 +0000 Subject: [PATCH 2/2] Update index.js --- index.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index fd1df87..8ca76e7 100644 --- a/index.js +++ b/index.js @@ -49,14 +49,18 @@ TuyaDevice.prototype.getStatus = function (callback) { } if ('devId' in requests[this.type].status.command) { requests[this.type].status.command.devId = this.id; - } + } // Create byte buffer from hex data const thisData = Buffer.from(JSON.stringify(requests[this.type].status.command)); - const prefixSum = thisData.toString('hex').length + requests[this.type].status.suffix.length; - const buffer = Buffer.from(requests[this.type].status.prefix + thisData.toString('hex') + requests[this.type].status.suffix, 'hex'); + // Create data prefix + const prefixSum = (thisData.toString('hex').length + requests[this.type].status.suffix.length) / 2; + const commandType = '0a'; + const prefix = '000055aa0000006c000000' + commandType + '000000' + prefixSum.toString(16); + const buffer = Buffer.from(prefix + thisData.toString('hex') + requests[this.type].status.suffix, 'hex'); - this._send(buffer).then(data => { + +this._send(buffer).then(data => { // Extract returned JSON try { data = data.toString(); @@ -69,6 +73,8 @@ TuyaDevice.prototype.getStatus = function (callback) { }); }; + + /** * Sets the device's status. * @param {boolean} on - `true` for on, `false` for off @@ -107,7 +113,12 @@ TuyaDevice.prototype.setStatus = function (on, callback) { // Create byte buffer from hex data const thisData = Buffer.from(this.version + md5 + data); - const buffer = Buffer.from(thisRequest.prefix + thisData.toString('hex') + thisRequest.suffix, 'hex'); + // Create data prefix + const prefixSum = (thisData.toString('hex').length + requests[this.type].status.suffix.length) / 2; + const commandType = '07'; + const prefix = '000055aa0000006c000000' + commandType + '000000' + prefixSum.toString(16); + const buffer = Buffer.from(prefix + thisData.toString('hex') + thisRequest.suffix, 'hex'); + // Send request to change status this._send(buffer).then(data => { @@ -141,6 +152,7 @@ TuyaDevice.prototype._send = function (buffer) { }); }; + TuyaDevice.prototype._destroy = function () { this.client.end(); this.client.destroy();