Skip to content

Commit

Permalink
* (Apollon77) Optimize activity detection to process all relevant ent…
Browse files Browse the repository at this point in the history
…ries in all cases
  • Loading branch information
Apollon77 committed Oct 29, 2023
1 parent 53fdfa1 commit feb1dfc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Then it should work again
* docu docu docu (sorry ... will come)

## Changelog:

### __WORK IN PROGRESS__
* (Apollon77) Optimize activity detection to process all relevant entries in all cases

### 6.2.1 (2023-10-27)
* (Apollon77) Optimize activity detection to process all relevant entries and not just the last one

Expand Down
35 changes: 22 additions & 13 deletions alexa-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,28 +842,38 @@ class AlexaRemote extends EventEmitter {
this._options.logger && this._options.logger(`Alexa-Remote: Activity for id ${entry.key.entryId} not found`);
} else {
lastFoundQueueIndex = queueIndex;
this.activityUpdateQueue.splice(0, lastFoundQueueIndex + 1);
const activity = res.splice(found, 1)[0];
this._options.logger && this._options.logger(`Alexa-Remote: Activity found entry ${found} for Activity ID ${entry.key.entryId}`);
activity.destinationUserId = entry.destinationUserId;
this.emit('ws-device-activity', activity);
}
} else {
const lastPushedActivity = this.lastPushedActivity[entry.deviceSerialNumber] || Date.now() - 30_000;
const found = res.findIndex(activity => activity.data.recordKey.endsWith(`#${entry.deviceSerialNumber}`) && activity.data.customerId === entry.destinationUserId && activity.creationTimestamp >= entry.activityTimestamp - 10_000 && activity.creationTimestamp > lastPushedActivity); // Only if current stuff is found
const lastPushedActivity = this.lastPushedActivity[entry.deviceSerialNumber] || Date.now() - 30000;
const found = res.filter(activity => activity.data.recordKey.endsWith(`#${entry.deviceSerialNumber}`) && activity.data.customerId === entry.destinationUserId && activity.creationTimestamp >= entry.activityTimestamp - 10000 && activity.creationTimestamp > lastPushedActivity); // Only if current stuff is found

if (found === -1) {
if (found.length === 0) {
this._options.logger && this._options.logger(`Alexa-Remote: Activity for device ${entry.deviceSerialNumber} not found`);
} else {
const activity = res.splice(found, 1)[0];
this._options.logger && this._options.logger(`Alexa-Remote: Activity (ts=${activity.creationTimestamp}) found entry ${found} for device ${entry.deviceSerialNumber}`);
activity.destinationUserId = entry.destinationUserId;
this.emit('ws-device-activity', activity);
if (activity.data.utteranceType !== 'WAKE_WORD_ONLY') {
this.lastPushedActivity[entry.deviceSerialNumber] = activity.creationTimestamp;
let foundSomething = false;
found.forEach((activity, index) => {
if (activity.data.utteranceType === 'WAKE_WORD_ONLY' && index === 0 && this.activityUpdateNotFoundCounter > 0 && found.length > 1) return;
this._options.logger && this._options.logger(`Alexa-Remote: Activity (ts=${activity.creationTimestamp}) found for device ${entry.deviceSerialNumber}`);
activity.destinationUserId = entry.destinationUserId;
this.emit('ws-device-activity', activity);
if (activity.data.utteranceType !== 'WAKE_WORD_ONLY') {
this.lastPushedActivity[entry.deviceSerialNumber] = activity.creationTimestamp;
foundSomething = true;
} else {
this._options.logger && this._options.logger(`Alexa-Remote: Only Wakeword activity for device ${entry.deviceSerialNumber} found. try again in 2,5s`);
if (!foundSomething) {
lastFoundQueueIndex = -2;
}
}
});
if (foundSomething) {
lastFoundQueueIndex = queueIndex;
} else {
this._options.logger && this._options.logger(`Alexa-Remote: Only Wakeword activity for device ${entry.deviceSerialNumber} found. try again in 2,5s`);
lastFoundQueueIndex = -2;
this.activityUpdateQueue.splice(queueIndex, 1);
}
}
}
Expand All @@ -883,7 +893,6 @@ class AlexaRemote extends EventEmitter {
}
else {
this.activityUpdateNotFoundCounter = 0;
this.activityUpdateQueue.splice(0, lastFoundQueueIndex + 1);
this._options.logger && this._options.logger(`Alexa-Remote: ${this.activityUpdateQueue.length} entries left in activity queue`);
}
}
Expand Down

0 comments on commit feb1dfc

Please sign in to comment.