Skip to content

Commit

Permalink
reduce 429, use name, payload with security device
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio Chimera authored and Claudio Chimera committed Oct 24, 2021
1 parent 6f9cff7 commit 73f028f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
12 changes: 9 additions & 3 deletions alexa/alexa-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = function (RED) {
const throttle = new Throttle({
active: true, // set false to pause queue
rate: 10, // how many requests can be sent every `ratePer`
ratePer: 1000, // number of ms in which `rate` requests may be sent
ratePer: 5000, // number of ms in which `rate` requests may be sent
concurrent: 2 // how many requests can be sent concurrently
});
const stoppable = require('stoppable');
Expand Down Expand Up @@ -1139,12 +1139,18 @@ module.exports = function (RED) {
//
//
//
get_id_from_name(names) {
get_devices_id_name(names, topics) {
var node = this;
let id_names = {};
if (names === undefined) {
names = [];
}
if (topics === undefined) {
topics = [];
}
Object.keys(node.devices).forEach(function (key) {
const device = node.devices[key];
if (names.includes(device.config.name)) {
if (names.includes(device.config.name) || topics.includes(device.config.topic)) {
id_names[key] = device.config.name;
}
});
Expand Down
22 changes: 13 additions & 9 deletions alexa/alexa-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,28 @@ module.exports = function (RED) {
const media_id_removed = media_to_remove.map(m => m.id);
if (media_id_removed.length > 0) node.alexa.send_media_deleted(node.id, media_id_removed);
} else if (topic === 'SETSECURITYDEVICENAMESINERROR') {
const names = Array.isArray(msg.payload) ? msg.payload : [msg.payload];
const topics = typeof msg.payload === 'string' ? [] : (Array.isArray(msg.payload || []) ? [] : msg.payload.topics);
const names = typeof msg.payload === 'string' ? [msg.payload] : (Array.isArray(msg.payload || []) ? msg.payload || [] : msg.payload.names);
node.security_device_names_in_error = [];
for (const [id, name] of Object.entries(node.alexa.get_id_from_name(msg.payload))) {
for (const [id, name] of Object.entries(node.alexa.get_devices_id_name(names, topics))) {
node.security_device_names_in_error.push(name);
}
node._debug("CCHI " + node.id + " security_device_names_in_error " + JSON.stringify(node.security_device_names_in_error));
} else if (topic === 'ADDSECURITYDEVICENAMESINERROR') {
let a_names = Array.isArray(msg.payload) ? msg.payload : [msg.payload];
a_names.forEach(name => node.security_device_names_in_error.push(name));
a_names = node.security_device_names_in_error;
const a_topics = typeof msg.payload === 'string' ? [] : (Array.isArray(msg.payload || []) ? [] : msg.payload.topics);
let a_names = typeof msg.payload === 'string' ? [msg.payload] : (Array.isArray(msg.payload || []) ? msg.payload || [] : msg.payload.names);
node.security_device_names_in_error.forEach(name => a_names.push(name));
node.security_device_names_in_error = [];
for (const [id, name] of Object.entries(node.alexa.get_id_from_name(msg.payload))) {
for (const [id, name] of Object.entries(node.alexa.get_devices_id_name(a_names, a_topics))) {
node.security_device_names_in_error.push(name);
}
node._debug("CCHI " + node.id + " security_device_names_in_error " + JSON.stringify(node.security_device_names_in_error));
} else if (topic === 'DELSECURITYDEVICENAMESINERROR') {
const r_names = Array.isArray(msg.payload) ? msg.payload : [msg.payload];
node.security_device_names_in_error = node.security_device_names_in_error.filter(name => !r_names.includes(name));
const r_topics = typeof msg.payload === 'string' ? [] : (Array.isArray(msg.payload || []) ? [] : msg.payload.topics);
let r_names = typeof msg.payload === 'string' ? [msg.payload] : (Array.isArray(msg.payload || []) ? msg.payload || [] : msg.payload.names);
for (const [id, name] of Object.entries(node.alexa.get_devices_id_name(r_names, r_topics))) {
node.security_device_names_in_error = node.security_device_names_in_error.filter(i_name => i_name != name);
}
node._debug("CCHI2 " + node.id + " security_device_names_in_error " + JSON.stringify(node.security_device_names_in_error));
} else if (topic === 'MEASUREMENTSREPORT') {
let other_data = {};
Expand Down Expand Up @@ -1081,7 +1085,7 @@ module.exports = function (RED) {
node._debug("CCHI event_payload security_device_names_in_error " + JSON.stringify(node.security_device_names_in_error));
if (node.security_device_names_in_error.length > 0) {
let security_device_in_error = [];
for (const [id, name] of Object.entries(node.alexa.get_id_from_name(node.security_device_names_in_error))) {
for (const [id, name] of Object.entries(node.alexa.get_devices_id_name(node.security_device_names_in_error))) {
security_device_in_error.push({
friendlyName: name,
endpointId: id
Expand Down

0 comments on commit 73f028f

Please sign in to comment.