From 4763fd7c858f0cb19e431ff45ee3085b7e02f265 Mon Sep 17 00:00:00 2001 From: foxriver76 Date: Sun, 25 Aug 2024 10:02:51 +0200 Subject: [PATCH 1/7] added commands getInstances and sendToAdapter for visu endpoint - needed for matter --- README.md | 1 + lib/visuApp.js | 37 +++++++++++++++++++++++++++++++++++++ main.js | 16 ++++++++++++---- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a72d511b..16e2b70d 100644 --- a/README.md +++ b/README.md @@ -308,6 +308,7 @@ setState('iot.0.app.message', JSON.stringify({ ## Changelog ### **WORK IN PROGRESS** +* (@foxriver76) added new commands for the visu app * (bluefox) updated packages * (bluefox) Migrated GUI for admin v7 diff --git a/lib/visuApp.js b/lib/visuApp.js index 1a68aa4a..7c1bf5bc 100644 --- a/lib/visuApp.js +++ b/lib/visuApp.js @@ -1,3 +1,38 @@ +/** @typedef {{ command: 'sendToAdapter', instance: string, message: string, data: any }} SendToAdapterCommand */ + +/** + * Handles `sendToAdapter` command + * @param {SendToAdapterCommand} visuData the data sent by the app + * @param {ioBroker.Adapter} adapter the adapter instance + * @returns {Promise<{ result: 'Ok', message: ioBroker.Message }>} + */ +async function handleSendToAdapter(visuData, adapter) { + const { instance, command, message } = visuData; + + const resp = await adapter.sendToAsync(instance, command, message); + return { result: 'Ok', message: resp }; +} + +/** @typedef {{ command: 'getInstances', adapterName: string }} GetInstancesCommand */ + +/** + * Handles `sendToAdapter` command + * @param {GetInstancesCommand} visuData the data sent by the app + * @param {ioBroker.Adapter} adapter the adapter instance + * @returns {Promise<{ result: 'Ok', instances: string[] }>} + */ +async function handleGetInstances(visuData, adapter) { + const { adapterName } = visuData; + + const res = await adapter.getObjectViewAsync('system', 'instance', { + startkey: `system.adapter.${adapterName}.`, + endkey: `system.adapter.${adapterName}.\u9999` + }); + + const instances = res.rows.map(item => item.id.substring('system.adapter.'.length)); + return { result: 'Ok', instances }; +} + /** * Handle Geofence data update from app * @@ -116,6 +151,8 @@ const commonMapping = { } module.exports = { + handleGetInstances, + handleSendToAdapter, handleGeofenceData, handleDevicesData } \ No newline at end of file diff --git a/main.js b/main.js index f6145aa1..364926df 100644 --- a/main.js +++ b/main.js @@ -16,7 +16,7 @@ const GoogleHome = require('./lib/googleHome'); const YandexAlisa = require('./lib/alisa'); const Remote = require('./lib/remote'); const packageJSON = require('./package.json'); -const { handleGeofenceData, handleDevicesData } = require('./lib/visuApp.js') +const { handleGeofenceData, handleDevicesData, handleSendToAdapter, handleGetInstances } = require('./lib/visuApp.js') const version = packageJSON.version; // @ts-ignore @@ -876,15 +876,23 @@ async function processMessage(type, request) { } try { - /** @type {{ presence?: Record, devices?: Record }} */ + /** @type {{ command?: string; presence?: Record, devices?: Record }} */ const visuData = JSON.parse(request); if (visuData.presence) { - await handleGeofenceData(visuData, adapter) + await handleGeofenceData(visuData, adapter); } if (visuData.devices) { - await handleDevicesData(visuData, adapter) + await handleDevicesData(visuData, adapter); + } + + if (visuData.command === 'getInstances') { + return handleGetInstances(visuData, adapter); + } + + if (visuData.command === 'sendToAdapter') { + return handleSendToAdapter(visuData, adapter); } } catch (e) { adapter.log.error(`Could not handle data "${request}" by Visu App: ${e.message}`) From 533e9507f5ea070536e899256952df0340444bd8 Mon Sep 17 00:00:00 2001 From: foxriver76 Date: Sun, 25 Aug 2024 10:31:53 +0200 Subject: [PATCH 2/7] added timeout --- lib/visuApp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/visuApp.js b/lib/visuApp.js index 7c1bf5bc..49fbcdc0 100644 --- a/lib/visuApp.js +++ b/lib/visuApp.js @@ -9,7 +9,7 @@ async function handleSendToAdapter(visuData, adapter) { const { instance, command, message } = visuData; - const resp = await adapter.sendToAsync(instance, command, message); + const resp = await adapter.sendToAsync(instance, command, message, { timeout: 20_000 }); return { result: 'Ok', message: resp }; } From 27e5a3c3feb79b3b916fd9e944047a1c046b123a Mon Sep 17 00:00:00 2001 From: foxriver76 Date: Sun, 25 Aug 2024 10:46:06 +0200 Subject: [PATCH 3/7] propagate error to the correct log --- lib/visuApp.js | 8 ++++---- main.js | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/visuApp.js b/lib/visuApp.js index 49fbcdc0..913fccd1 100644 --- a/lib/visuApp.js +++ b/lib/visuApp.js @@ -4,13 +4,13 @@ * Handles `sendToAdapter` command * @param {SendToAdapterCommand} visuData the data sent by the app * @param {ioBroker.Adapter} adapter the adapter instance - * @returns {Promise<{ result: 'Ok', message: ioBroker.Message }>} + * @returns {Promise<{ message: ioBroker.Message }>} */ async function handleSendToAdapter(visuData, adapter) { const { instance, command, message } = visuData; const resp = await adapter.sendToAsync(instance, command, message, { timeout: 20_000 }); - return { result: 'Ok', message: resp }; + return { message: resp }; } /** @typedef {{ command: 'getInstances', adapterName: string }} GetInstancesCommand */ @@ -19,7 +19,7 @@ async function handleSendToAdapter(visuData, adapter) { * Handles `sendToAdapter` command * @param {GetInstancesCommand} visuData the data sent by the app * @param {ioBroker.Adapter} adapter the adapter instance - * @returns {Promise<{ result: 'Ok', instances: string[] }>} + * @returns {Promise<{ instances: string[] }>} */ async function handleGetInstances(visuData, adapter) { const { adapterName } = visuData; @@ -30,7 +30,7 @@ async function handleGetInstances(visuData, adapter) { }); const instances = res.rows.map(item => item.id.substring('system.adapter.'.length)); - return { result: 'Ok', instances }; + return { instances }; } /** diff --git a/main.js b/main.js index 364926df..97d6dd9a 100644 --- a/main.js +++ b/main.js @@ -888,11 +888,13 @@ async function processMessage(type, request) { } if (visuData.command === 'getInstances') { - return handleGetInstances(visuData, adapter); + const res = await handleGetInstances(visuData, adapter); + return { result: 'Ok', ...res } } if (visuData.command === 'sendToAdapter') { - return handleSendToAdapter(visuData, adapter); + const res = await handleSendToAdapter(visuData, adapter); + return { result: 'Ok', ...res } } } catch (e) { adapter.log.error(`Could not handle data "${request}" by Visu App: ${e.message}`) From 2c17dd14dd40843d9a121cd8d43a3f4869b752f6 Mon Sep 17 00:00:00 2001 From: foxriver76 Date: Sun, 25 Aug 2024 12:32:13 +0200 Subject: [PATCH 4/7] fix sendToAdapter --- lib/visuApp.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/visuApp.js b/lib/visuApp.js index 913fccd1..0c99f20b 100644 --- a/lib/visuApp.js +++ b/lib/visuApp.js @@ -7,9 +7,9 @@ * @returns {Promise<{ message: ioBroker.Message }>} */ async function handleSendToAdapter(visuData, adapter) { - const { instance, command, message } = visuData; + const { instance, message, data } = visuData; - const resp = await adapter.sendToAsync(instance, command, message, { timeout: 20_000 }); + const resp = await adapter.sendToAsync(instance, message, data,{ timeout: 20_000 }); return { message: resp }; } From 9c440e049ae9e62aad7289b6accb27de774f602e Mon Sep 17 00:00:00 2001 From: foxriver76 Date: Sun, 25 Aug 2024 12:37:39 +0200 Subject: [PATCH 5/7] propagate error on correct level --- lib/visuApp.js | 4 ++-- main.js | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/visuApp.js b/lib/visuApp.js index 0c99f20b..06b73d73 100644 --- a/lib/visuApp.js +++ b/lib/visuApp.js @@ -4,13 +4,13 @@ * Handles `sendToAdapter` command * @param {SendToAdapterCommand} visuData the data sent by the app * @param {ioBroker.Adapter} adapter the adapter instance - * @returns {Promise<{ message: ioBroker.Message }>} + * @returns {Promise>} */ async function handleSendToAdapter(visuData, adapter) { const { instance, message, data } = visuData; const resp = await adapter.sendToAsync(instance, message, data,{ timeout: 20_000 }); - return { message: resp }; + return { ...resp }; } /** @typedef {{ command: 'getInstances', adapterName: string }} GetInstancesCommand */ diff --git a/main.js b/main.js index 97d6dd9a..80bef1b2 100644 --- a/main.js +++ b/main.js @@ -894,7 +894,12 @@ async function processMessage(type, request) { if (visuData.command === 'sendToAdapter') { const res = await handleSendToAdapter(visuData, adapter); - return { result: 'Ok', ...res } + + if (!res.error) { + res.result = 'Ok'; + } + + return res; } } catch (e) { adapter.log.error(`Could not handle data "${request}" by Visu App: ${e.message}`) From aebc6a7bccaca799786eb8e5b0b8ad6780ee6a69 Mon Sep 17 00:00:00 2001 From: foxriver76 Date: Sun, 25 Aug 2024 12:38:32 +0200 Subject: [PATCH 6/7] decrease the timeout --- lib/visuApp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/visuApp.js b/lib/visuApp.js index 06b73d73..dc0f2990 100644 --- a/lib/visuApp.js +++ b/lib/visuApp.js @@ -9,7 +9,7 @@ async function handleSendToAdapter(visuData, adapter) { const { instance, message, data } = visuData; - const resp = await adapter.sendToAsync(instance, message, data,{ timeout: 20_000 }); + const resp = await adapter.sendToAsync(instance, message, data,{ timeout: 2_000 }); return { ...resp }; } From ea7f5652c8871e9128f0914da62dc96526d66f0e Mon Sep 17 00:00:00 2001 From: Bluefox Date: Mon, 26 Aug 2024 07:24:45 +0800 Subject: [PATCH 7/7] Added semicolons --- main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index 80bef1b2..013e642d 100644 --- a/main.js +++ b/main.js @@ -16,7 +16,7 @@ const GoogleHome = require('./lib/googleHome'); const YandexAlisa = require('./lib/alisa'); const Remote = require('./lib/remote'); const packageJSON = require('./package.json'); -const { handleGeofenceData, handleDevicesData, handleSendToAdapter, handleGetInstances } = require('./lib/visuApp.js') +const { handleGeofenceData, handleDevicesData, handleSendToAdapter, handleGetInstances } = require('./lib/visuApp.js'); const version = packageJSON.version; // @ts-ignore @@ -889,7 +889,7 @@ async function processMessage(type, request) { if (visuData.command === 'getInstances') { const res = await handleGetInstances(visuData, adapter); - return { result: 'Ok', ...res } + return { result: 'Ok', ...res }; } if (visuData.command === 'sendToAdapter') {