From 73f028f7a765ce1114f192277a075119bdca3920 Mon Sep 17 00:00:00 2001 From: Claudio Chimera Date: Sun, 24 Oct 2021 15:58:08 +0200 Subject: [PATCH] reduce 429, use name, payload with security device --- alexa/alexa-adapter.js | 12 +++++++++--- alexa/alexa-device.js | 22 +++++++++++++--------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/alexa/alexa-adapter.js b/alexa/alexa-adapter.js index 5b4974a..5e5f16f 100644 --- a/alexa/alexa-adapter.js +++ b/alexa/alexa-adapter.js @@ -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'); @@ -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; } }); diff --git a/alexa/alexa-device.js b/alexa/alexa-device.js index 643a998..4d8243b 100644 --- a/alexa/alexa-device.js +++ b/alexa/alexa-device.js @@ -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 = {}; @@ -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