Skip to content

Commit

Permalink
handle command notification
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Jan 31, 2024
1 parent 5229908 commit 9dde3e2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
59 changes: 52 additions & 7 deletions lib/bindings/HTTPBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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],

Check failure on line 591 in lib/bindings/HTTPBindings.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript

'iotaUtils' is not defined
callback
);
}

exports.start = start;
Expand Down
26 changes: 22 additions & 4 deletions lib/iotagent-ul.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down

0 comments on commit 9dde3e2

Please sign in to comment.