diff --git a/lib/bindings/HTTPBindings.js b/lib/bindings/HTTPBindings.js index 115df2a8..d6f5a923 100644 --- a/lib/bindings/HTTPBindings.js +++ b/lib/bindings/HTTPBindings.js @@ -526,13 +526,17 @@ function stop(callback) { } } -function sendPushNotifications(device, values, callback) { - async.series(values.map(generateCommandExecution.bind(null, null, device)), function (error) { +function sendPushNotifications(device, group, values, callback) { + const executions = _.flatten( + values.map(commandHandler.generateCommandExecution.bind(null, group.apikey, device, group)) + ); + + async.series(executions, function (error) { callback(error); }); } -function storePollNotifications(device, values, callback) { +function storePollNotifications(device, group, values, callback) { function addPollNotification(item, innerCallback) { iotAgentLib.addCommand(device.service, device.subservice, device.id, item, innerCallback); } @@ -541,11 +545,52 @@ function storePollNotifications(device, values, callback) { } function notificationHandler(device, values, callback) { - if (device.endpoint) { - sendPushNotifications(device, values, callback); - } else { - storePollNotifications(device, values, callback); + config.getLogger().debug(context, 'values for command %j and device %j', values, device); + + function invokeWithConfiguration(apiKey, callback) { + let group = {}; + iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', apiKey, function ( + error, + foundGroup + ) { + if (!error) { + group = foundGroup; + } + var cmdValue = { type: 'command' }; + for (let val of values) { + if (val.name === 'cmd') { + cmdValue.name = val.value; + } else if (val.name === 'params') { + cmdValue.value = val.value; + } else { + // other fields like status, info, onDelivered, OnError + cmdValue[val.name] = val.value; + } + } + var cmdValues = [cmdValue]; + config.getLogger().debug(context, 'cmdValues %j', cmdValues); + iotAgentLib.executeUpdateSideEffects( + device, + device.id, + device.type, + device.service, + device.subservice, + cmdValues, + function () { + if (device.endpoint || group.endpoint) { + sendPushNotifications(device, group, cmdValues, callback); + } else { + storePollNotifications(device, group, cmdValues, callback); + } + } + ); + }); } + + async.waterfall( + [apply(iotaUtils.getEffectiveApiKey, device.service, device.subservice, device), invokeWithConfiguration], + callback + ); } exports.start = start; diff --git a/lib/iotagent-ul.js b/lib/iotagent-ul.js index baa363cd..baae9e36 100644 --- a/lib/iotagent-ul.js +++ b/lib/iotagent-ul.js @@ -165,10 +165,28 @@ function deviceUpdatingHandler(device, callback) { * @param {Array} values Values recieved in the notification. */ function notificationHandler(device, values, callback) { - transportSelector.applyFunctionFromBinding( - [device, values], - 'notificationHandler', - device.transport || config.getConfig().defaultTransport, + function invokeWithConfiguration(apiKey, callback) { + let group = {}; + iotAgentLib.getConfigurationSilently(config.getConfig().iota.defaultResource || '', apiKey, function ( + error, + foundGroup + ) { + if (!error) { + group = foundGroup; + } + transportSelector.applyFunctionFromBinding( + [device, values], + 'notificationHandler', + device.transport || + (group && group.transport ? group.transport : undefined) || + config.getConfig().defaultTransport, + callback + ); + }); + } + + async.waterfall( + [apply(iotaUtils.getEffectiveApiKey, device.service, device.subservice, device), invokeWithConfiguration], callback ); }