Skip to content

Commit 10f0464

Browse files
authored
Merge pull request #11 from jepsonrob/master
Generate the prefix according to payload
2 parents 1342e98 + f1d8c09 commit 10f0464

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@ TuyaDevice.prototype.getStatus = function (callback) {
4949
}
5050
if ('devId' in requests[this.type].status.command) {
5151
requests[this.type].status.command.devId = this.id;
52-
}
52+
}
5353

5454
// Create byte buffer from hex data
5555
const thisData = Buffer.from(JSON.stringify(requests[this.type].status.command));
56-
const buffer = Buffer.from(requests[this.type].status.prefix + thisData.toString('hex') + requests[this.type].status.suffix, 'hex');
56+
// Create data prefix
57+
const prefixSum = (thisData.toString('hex').length + requests[this.type].status.suffix.length) / 2;
58+
const commandType = '0a';
59+
const prefix = '000055aa0000006c000000' + commandType + '000000' + prefixSum.toString(16);
60+
const buffer = Buffer.from(prefix + thisData.toString('hex') + requests[this.type].status.suffix, 'hex');
5761

58-
this._send(buffer).then(data => {
62+
63+
this._send(buffer).then(data => {
5964
// Extract returned JSON
6065
try {
6166
data = data.toString();
@@ -68,6 +73,8 @@ TuyaDevice.prototype.getStatus = function (callback) {
6873
});
6974
};
7075

76+
77+
7178
/**
7279
* Sets the device's status.
7380
* @param {boolean} on - `true` for on, `false` for off
@@ -106,7 +113,12 @@ TuyaDevice.prototype.setStatus = function (on, callback) {
106113

107114
// Create byte buffer from hex data
108115
const thisData = Buffer.from(this.version + md5 + data);
109-
const buffer = Buffer.from(thisRequest.prefix + thisData.toString('hex') + thisRequest.suffix, 'hex');
116+
// Create data prefix
117+
const prefixSum = (thisData.toString('hex').length + requests[this.type].status.suffix.length) / 2;
118+
const commandType = '07';
119+
const prefix = '000055aa0000006c000000' + commandType + '000000' + prefixSum.toString(16);
120+
const buffer = Buffer.from(prefix + thisData.toString('hex') + thisRequest.suffix, 'hex');
121+
110122

111123
// Send request to change status
112124
this._send(buffer).then(data => {

0 commit comments

Comments
 (0)