Skip to content

Commit

Permalink
Merge pull request #797 from ioBroker/visu-matter
Browse files Browse the repository at this point in the history
added commands getInstances and sendToAdapter for visu endpoint
  • Loading branch information
GermanBluefox authored Aug 25, 2024
2 parents 8ac8e9f + ea7f565 commit 4424f84
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 37 additions & 0 deletions lib/visuApp.js
Original file line number Diff line number Diff line change
@@ -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<Record<string, any>>}
*/
async function handleSendToAdapter(visuData, adapter) {
const { instance, message, data } = visuData;

const resp = await adapter.sendToAsync(instance, message, data,{ timeout: 2_000 });
return { ...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<{ 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 { instances };
}

/**
* Handle Geofence data update from app
*
Expand Down Expand Up @@ -116,6 +151,8 @@ const commonMapping = {
}

module.exports = {
handleGetInstances,
handleSendToAdapter,
handleGeofenceData,
handleDevicesData
}
23 changes: 19 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -876,15 +876,30 @@ async function processMessage(type, request) {
}

try {
/** @type {{ presence?: Record<string, boolean>, devices?: Record<string, any> }} */
/** @type {{ command?: string; presence?: Record<string, boolean>, devices?: Record<string, any> }} */
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') {
const res = await handleGetInstances(visuData, adapter);
return { result: 'Ok', ...res };
}

if (visuData.command === 'sendToAdapter') {
const res = await handleSendToAdapter(visuData, adapter);

if (!res.error) {
res.result = 'Ok';
}

return res;
}
} catch (e) {
adapter.log.error(`Could not handle data "${request}" by Visu App: ${e.message}`)
Expand Down

0 comments on commit 4424f84

Please sign in to comment.