Skip to content

Commit

Permalink
Serialize command sequence to enable last pause command. (#528)
Browse files Browse the repository at this point in the history
Co-authored-by: banboobee <98196664+banboobee@users.noreply.github.com>

* Serialize command sequence to enable last pause command.
  • Loading branch information
banboobee committed Jun 16, 2023
1 parent 391c3a7 commit 7501cfc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 267 deletions.
251 changes: 0 additions & 251 deletions CHANGELOG.md

This file was deleted.

44 changes: 28 additions & 16 deletions accessories/accessory.js
Expand Up @@ -5,6 +5,7 @@ const { HomebridgeAccessory } = require('../base');
const sendData = require('../helpers/sendData');
const delayForDuration = require('../helpers/delayForDuration');
const catchDelayCancelError = require('../helpers/catchDelayCancelError');
const { getDevice } = require('../helpers/getDevice');

class BroadlinkRMAccessory extends HomebridgeAccessory {

Expand Down Expand Up @@ -66,15 +67,15 @@ class BroadlinkRMAccessory extends HomebridgeAccessory {

reset () {
// Clear Multi-hex timeouts
if (this.intervalTimeoutPromise) {
this.intervalTimeoutPromise.cancel();
this.intervalTimeoutPromise = null;
}

if (this.pauseTimeoutPromise) {
this.pauseTimeoutPromise.cancel();
this.pauseTimeoutPromise = null;
}
// if (this.intervalTimeoutPromise) {
// this.intervalTimeoutPromise.cancel();
// this.intervalTimeoutPromise = null;
// }

// if (this.pauseTimeoutPromise) {
// this.pauseTimeoutPromise.cancel();
// this.pauseTimeoutPromise = null;
// }
}

async performSend (data, actionCallback) {
Expand All @@ -83,22 +84,32 @@ class BroadlinkRMAccessory extends HomebridgeAccessory {
//Error catch
if(data === undefined){return}

if (typeof data === 'string') {
// Get the Broadlink device
const device = getDevice({ host, log });

if (!host || !device) { // Error reporting
sendData({ host, hexData: data, log, name, logLevel });

return;
}

await catchDelayCancelError(async () => {
await device.mutex.use(async () => { // Queue command sequence
if (typeof data === 'string') {
await sendData({ host, hexData: data, log, name, logLevel });

return;
}

// Itterate through each hex config in the array
for (let index = 0; index < data.length; index++) {
const { pause } = data[index];

await this.performRepeatSend(data[index], actionCallback);

if (pause) {
this.pauseTimeoutPromise = delayForDuration(pause);
await this.pauseTimeoutPromise;
// this.pauseTimeoutPromise = delayForDuration(pause);
// await this.pauseTimeoutPromise;
await new Promise(resolve => setTimeout(resolve, pause * 1000));
}
}
});
Expand All @@ -113,11 +124,12 @@ class BroadlinkRMAccessory extends HomebridgeAccessory {

// Itterate through each hex config in the array
for (let index = 0; index < sendCount; index++) {
sendData({ host, hexData: data, log, name, logLevel });
await sendData({ host, hexData: data, log, name, logLevel });

if (interval && index < sendCount - 1) {
this.intervalTimeoutPromise = delayForDuration(interval);
await this.intervalTimeoutPromise;
// this.intervalTimeoutPromise = delayForDuration(interval);
// await this.intervalTimeoutPromise;
await new Promise(resolve => setTimeout(resolve, interval * 1000));
}
}
}
Expand Down

0 comments on commit 7501cfc

Please sign in to comment.