diff --git a/README.md b/README.md index 6e16ecc42..9032936e5 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Since v5.5.0 of the JavaScript adapter the following locations (relative to the * (klein0r) Day of week as number always returns 1 (monday) to 7 (sunday) * (klein0r) Fixed layout of script type selection +* (klein0r) Fixed sendto with multiple instances (for callback / timeout handling) ### 7.4.0 (2023-12-08) diff --git a/lib/sandbox.js b/lib/sandbox.js index 9d75eea67..2924737ba 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -2713,28 +2713,59 @@ function sandBox(script, name, verbose, debug, context) { try { callback.call(sandbox, {error: 'timeout'}, options, _adapter); } catch (e) { - errorInCallback(e); //adapter.log.error('Error in callback: ' + e) + errorInCallback(e); } callback = null; } }, timeoutDuration); } - sandbox.verbose && sandbox.log(`sendTo(adapter=${_adapter}, cmd=${cmd}, msg=${JSON.stringify(msg)})`, 'info'); - adapter.sendTo(_adapter, cmd, msg, timeout && function (result) { - timeout && clearTimeout(timeout); + // If specific instance + if (_adapter.match(/\.[0-9]+$/)) { + sandbox.verbose && sandbox.log(`sendTo(instance=${_adapter}, cmd=${cmd}, msg=${JSON.stringify(msg)}, hasCallback=${typeof callback === 'function'})`, 'info'); + adapter.sendTo(_adapter, cmd, msg, timeout && function (result) { + timeout && clearTimeout(timeout); - sandbox.verbose && result && sandbox.log(`sendTo => ${JSON.stringify(result)}`, 'debug'); + sandbox.verbose && result && sandbox.log(`sendTo => ${JSON.stringify(result)}`, 'debug'); - if (typeof callback === 'function') { - try { - callback.call(sandbox, result, options, _adapter); - } catch (e) { - errorInCallback(e); //adapter.log.error('Error in callback: ' + e) + if (typeof callback === 'function') { + try { + callback.call(sandbox, result, options, _adapter); + } catch (e) { + errorInCallback(e); + } + callback = null; } - callback = null; - } - }, options); + }, options); + } else { + // Send to all instances + context.adapter.getObjectView('system', 'instance', {startkey: `system.adapter.${_adapter}.`, endkey: `system.adapter.${_adapter}.\u9999`}, options, (err, res) => { + if (err || !res) { + sandbox.log(`sendTo failed: ${err.message}`, 'error'); + return; + } + + const instances = res.rows.map(item => item.id.substring('system.adapter.'.length)); + + instances.forEach(instance => { + sandbox.verbose && sandbox.log(`sendTo(instance=${instance}, cmd=${cmd}, msg=${JSON.stringify(msg)}, hasCallback=${typeof callback === 'function'})`, 'info'); + adapter.sendTo(instance, cmd, msg, timeout && function (result) { + timeout && clearTimeout(timeout); + + sandbox.verbose && result && sandbox.log(`sendTo => ${JSON.stringify(result)}`, 'debug'); + + if (typeof callback === 'function') { + try { + callback.call(sandbox, result, options, instance); + } catch (e) { + errorInCallback(e); + } + callback = null; + } + }, options); + }); + }); + } }, sendto: function (_adapter, cmd, msg, callback) { return sandbox.sendTo(_adapter, cmd, msg, callback); @@ -3324,9 +3355,8 @@ function sandBox(script, name, verbose, debug, context) { } return; } - let timeout = setTimeout(() => { - timeout = null; + const timeout = setTimeout(() => { sandbox.verbose && sandbox.log('getHistory => timeout', 'debug'); if (typeof callback === 'function') {