Skip to content

Commit

Permalink
Merge pull request #681 from codetheweb/set-fixes
Browse files Browse the repository at this point in the history
More stability fixes around "set" (and "get")
  • Loading branch information
Apollon77 authored Jan 6, 2025
2 parents 4f488ed + 960babe commit 3b29626
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,6 @@ class TuyaDevice extends EventEmitter {
delete payload.data.t;
}

if (options.shouldWaitForResponse && this._setResolver) {
throw new Error('A set command is already in progress. Can not issue a second one that also should return a response.');
}

debug('SET Payload:');
debug(payload);

Expand All @@ -409,10 +405,14 @@ class TuyaDevice extends EventEmitter {
sequenceN
});

// Make sure we only resolve or reject once
let resolvedOrRejected = false;

// Queue this request and limit concurrent set requests to one
return this._setQueue.add(() => pTimeout(new Promise((resolve, reject) => {
// Make sure we only resolve or reject once
let resolvedOrRejected = false;
if (options.shouldWaitForResponse && this._setResolver) {
throw new Error('A set command is already in progress. Can not issue a second one that also should return a response.');
}

// Send request and wait for response
try {
Expand All @@ -423,12 +423,14 @@ class TuyaDevice extends EventEmitter {
// Send request
this._send(buffer).catch(error => {
if (options.shouldWaitForResponse && !resolvedOrRejected) {
resolvedOrRejected = true;
reject(error);
}
});
if (options.shouldWaitForResponse) {
this._setResolver = data => {
if (!resolvedOrRejected) {
resolvedOrRejected = true;
resolve(data);
}
};
Expand All @@ -453,6 +455,10 @@ class TuyaDevice extends EventEmitter {
'error',
'Timeout waiting for status response from device id: ' + this.device.id
);
if (!resolvedOrRejected) {
resolvedOrRejected = true;
throw new Error('Timeout waiting for status response from device id: ' + this.device.id);
}
}));
}

Expand All @@ -462,7 +468,7 @@ class TuyaDevice extends EventEmitter {
* wraps the entire operation in a retry.
* @private
* @param {Buffer} buffer buffer of data
* @returns {Promise<Any>} returned data for request
* @returns {Promise<any>} returned data for request
*/
_send(buffer) {
const sequenceNo = this._currentSequenceN;
Expand Down Expand Up @@ -564,13 +570,19 @@ class TuyaDevice extends EventEmitter {
// Automatically ask for dp_refresh so we
// can emit a `dp_refresh` event as soon as possible
if (this.globalOptions.issueRefreshOnConnect) {
this.refresh();
this.refresh().catch(error => {
debug('Error refreshing on connect: ' + error);
this.emit('error', error);
});
}

// Automatically ask for current state so we
// can emit a `data` event as soon as possible
if (this.globalOptions.issueGetOnConnect) {
this.get();
this.get().catch(error => {
debug('Error getting on connect: ' + error);
this.emit('error', error);
});
}

// Resolve
Expand Down

0 comments on commit 3b29626

Please # to comment.