Skip to content

Commit

Permalink
3.1.0 alexa-remote
Browse files Browse the repository at this point in the history
* fix cookie RefreshInterval parameters
* optimize getting Pushed activities
  • Loading branch information
Apollon77 committed Mar 16, 2020
1 parent 3c1e2f8 commit 01c93f4
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 70 deletions.
97 changes: 73 additions & 24 deletions alexa-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const querystring = require('querystring');
const os = require('os');
const extend = require('extend');
const AlexaWsMqtt = require('./alexa-wsmqtt.js');
const uuidv1 = require('uuid/v1');
const { v1: uuidv1 } = require('uuid');

const EventEmitter = require('events');

Expand Down Expand Up @@ -92,8 +92,12 @@ class AlexaRemote extends EventEmitter {
if (this._options.alexaServiceHost) this.baseUrl = this._options.alexaServiceHost;
this._options.logger && this._options.logger('Alexa-Remote: Use as Base-URL: ' + this.baseUrl);
this._options.alexaServiceHost = this.baseUrl;
if (this._options.refreshCookieInterval !== 0) {
this._options.refreshCookieInterval = this._options.refreshCookieInterval || 7*24*60*60*1000; // Auto Refresh after 7 days
if (this._options.refreshCookieInterval !== undefined && this._options.cookieRefreshInterval === undefined) {
this._options.cookieRefreshInterval = this._options.refreshCookieInterval;
delete this._options.refreshCookieInterval;
}
if (this._options.cookieRefreshInterval !== 0) {
this._options.cookieRefreshInterval = this._options.cookieRefreshInterval || 7*24*60*60*1000; // Auto Refresh after 7 days
}

const self = this;
Expand Down Expand Up @@ -264,7 +268,7 @@ class AlexaRemote extends EventEmitter {
this.names [name] = device;
this.names [name.toLowerCase()] = device;
}
device._orig = JSON.parse(JSON.stringify(device));
//device._orig = JSON.parse(JSON.stringify(device));
device._name = name;
device.sendCommand = this.sendCommand.bind(this, device);
device.setTunein = this.setTunein.bind(this, device);
Expand Down Expand Up @@ -330,6 +334,11 @@ class AlexaRemote extends EventEmitter {
this.alexaWsMqtt = new AlexaWsMqtt(this._options, this.cookie);
if (!this.alexaWsMqtt) return;

this.activityUpdateQueue = [];
this.activityUpdateNotFoundCounter = 0;
this.activityUpdateTimeout = null;
this.activityUpdateRunning = false;

this.alexaWsMqtt.on('disconnect', (retries, msg) => {
this.emit('ws-disconnect', retries, msg);
});
Expand Down Expand Up @@ -594,25 +603,15 @@ class AlexaRemote extends EventEmitter {
'version': 1
}
*/
this.getActivities({size: 3, filter: false}, (err, res) => {
if (err || !res) return;
let activity = null;
for (let i = 0; i < res.length; i++) {
if (res[i].data.id.endsWith('#' + payload.key.entryId) && res[i].data.registeredCustomerId === payload.key.registeredUserId) {
activity = res[i];
break;
}
}

if (!activity) {
this._options.logger && this._options.logger('Alexa-Remote: Activity for id ' + payload.key.entryId + ' not found');
return;
}
//this._options.logger && this._options.logger('Alexa-Remote: Activity found for id ' + payload.key.entryId + ': ' + JSON.stringify(activity));

activity.destinationUserId = payload.destinationUserId;
this.emit('ws-device-activity', activity);
});
this.activityUpdateQueue.push(payload);
if (this.activityUpdateTimeout) {
clearTimeout(this.activityUpdateTimeout);
this.activityUpdateTimeout = null;
}
this.activityUpdateTimeout = setTimeout(() => {
this.activityUpdateTimeout = null;
this.getPushedActivities();
}, 200);
return;

case 'PUSH_TODO_CHANGE': // does not exist?
Expand Down Expand Up @@ -648,6 +647,56 @@ class AlexaRemote extends EventEmitter {
this.alexaWsMqtt.connect();
}

getPushedActivities() {
if (this.activityUpdateRunning || !this.activityUpdateQueue.length) return;
this.activityUpdateRunning = true;
this.getActivities({size: this.activityUpdateQueue.length + 2, filter: false}, (err, res) => {
this.activityUpdateRunning = false;
if (!err && res) {

let lastFoundQueueIndex = -1;
this.activityUpdateQueue.forEach((entry, queueIndex) => {
const found = res.findIndex(activity => activity.data.id.endsWith('#' + entry.key.entryId) && activity.data.registeredCustomerId === entry.key.registeredUserId);

if (found === -1) {
this._options.logger && this._options.logger('Alexa-Remote: Activity for id ' + entry.key.entryId + ' not found');
}
else {
lastFoundQueueIndex = queueIndex;
const activity = res.splice(found, 1)[0];
this._options.logger && this._options.logger('Alexa-Remote: Activity found ' + found + ' for Activity ID ' + entry.key.entryId);
activity.destinationUserId = entry.destinationUserId;
this.emit('ws-device-activity', activity);
}
});

if (lastFoundQueueIndex === -1) {
this._options.logger && this._options.logger('Alexa-Remote: No activities from stored ' + this.activityUpdateQueue.length + ' entries found in queue (' + this.activityUpdateNotFoundCounter + ')');
this.activityUpdateNotFoundCounter++;
if (this.activityUpdateNotFoundCounter > 2) {
this._options.logger && this._options.logger('Alexa-Remote: Reset expected activities');
this.activityUpdateQueue = [];
this.activityUpdateNotFoundCounter = 0;
}
}
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');
}
}

if (this.activityUpdateQueue.length) {
this.activityUpdateTimeout = setTimeout(() => {
this.activityUpdateTimeout = null;
this.getPushedActivities();
}, 200);

}

});
}

stop() {
if (this.cookieRefreshTimeout) {
clearTimeout(this.cookieRefreshTimeout);
Expand Down Expand Up @@ -1501,7 +1550,7 @@ class AlexaRemote extends EventEmitter {
seqNode.type = 'Alexa.DeviceControls.Volume';
value = ~~value;
if (value < 0 || value > 100) {
return callback(new Error('Volume needs to be between 0 and 100'));
return callback && callback(new Error('Volume needs to be between 0 and 100'));
}
seqNode.operationPayload.value = value;
break;
Expand Down
2 changes: 1 addition & 1 deletion alexa-wsmqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class AlexaWsMqtt extends EventEmitter {
});

this.websocket.on('unexpected-response', (request, response) => {
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Unexpected Response: ' + JSON.stringify(response));
this._options.logger && this._options.logger('Alexa-Remote WS-MQTT: Unexpected Response: ' + response);
});

this.websocket.on('message', (data) => {
Expand Down
92 changes: 53 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "alexa-remote2",
"version": "3.0.3",
"version": "3.1.0",
"description": "Remote Control for amazon echo devices",
"author": {
"name": "Apollon77",
"email": "ingo@fischer-ka.de"
"email": "iobroker@fischer-ka.de"
},
"contributors": [
{
Expand All @@ -13,7 +13,7 @@
},
{
"name": "Apollon77",
"email": "ingo@fischer-ka.de"
"email": "iobroker@fischer-ka.de"
}
],
"homepage": "https://github.com/Apollon77/alexa-remote",
Expand All @@ -25,12 +25,12 @@
"layla.amazon.de"
],
"dependencies": {
"alexa-cookie2": "^3.0.2",
"alexa-cookie2": "^3.0.3",
"https": "^1.0.0",
"querystring": "^0.2.0",
"ws": "^7.2.1",
"ws": "^7.2.3",
"extend": "^3.0.2",
"uuid": "^3.3.3"
"uuid": "^7.0.2"
},
"devDependencies": {},
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Thank you for that work.

## Changelog:

### 3.1.0 (2019-12-30)
* (Apollon77) remove device._orig because really big objects happened and got exceptions on deep copy using JSION.stringify

### 3.0.3 (2019-12-28)
* (Apollon77) update cookie lib

Expand Down

0 comments on commit 01c93f4

Please sign in to comment.