Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate the prefix according to payload #11

Merged
merged 3 commits into from
Dec 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +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 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();
Expand All @@ -68,6 +73,8 @@ TuyaDevice.prototype.getStatus = function (callback) {
});
};



/**
* Sets the device's status.
* @param {boolean} on - `true` for on, `false` for off
Expand Down Expand Up @@ -106,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 => {
Expand Down